微服务负载均衡

@LoadBalanced 负载均衡
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
@LoadBalanced //具有负载均衡的请求
RestTemplate restTemplate() {
return new RestTemplate();
}
}
客服端服务consumer
@Autowired
@Qualifier("restTemplate")
RestTemplate restTemplate; //具有负载均衡的
@GetMapping("/hello3")
public void hello3() throws IOException {
String s = restTemplate.getForObject("http://provider/hello", String.class); //服务端的名和接口地址.
System.out.println(s); //打印服务端hello注册2000s
}
启动
http://localhost:3000/hello3
![]
RestTemplate 类上加@LoadBalanced注解,发起请求调用服务端,请求就被拦截具有负载均衡