《Spring Microservices in Action》 第四章笔记。
首先需要打开这两个
@EnableDiscoveryClient
@EnableFeignClients
方法一
@Autowired
private DiscoveryClient discoveryClient;
然后可以的得到服务的list:
List<ServiceInstance> instances = discoveryClient.getInstances("hello");
方法二
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
restTemplate.getForEntity("http://HELLO/hello", String.class).getBody();
方法三
@FeignClient(value = "HELLO", fallback = FeignServiceFallback.class)
public interface FeignService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam(value = "name") String name);
}
Spring Cloud会自动创建代理类。其他地方直接注入 FeignService 使用即可。