@SpringBootApplication
@EntityScan("com.zjk.order.entity")
@EnableEurekaClient
public class OrderApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
@LoadBalanced二,服务消费者使用服务提供者的服务名称请求
@GetMapping(value = "/findByIdByRibbon/{Id}")
public TbProduct findByIdByRibbon(@PathVariable Long Id){
try {
List instances = discoveryClient.getInstances("service-product");
ServiceInstance serviceInstance = instances.get(0);
TbProduct tbProduct = restTemplate.getForEntity("http://service-product/product/{Id}", TbProduct.class,Id).getBody();
return tbProduct;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
TbProduct tbProduct = restTemplate.getForEntity("http://service-product/product/{Id}", TbProduct.class,Id).getBody();
大家可以看出来,我们可以直接使用服务提供者的服务名称请求
其实,ribbon 最重要的作用是负载均衡



