feign调用spring clound eureka 注册中心服务

@RestController
public class TestService {

    private TestApi computeClient;
    
    private static final String SERVER_NAME = "eureka.client";

 @Autowired
 private DiscoveryClient discoveryClient;
 
 @RequestMapping(value = "/gethello/{name}", method = RequestMethod.GET)
    public String getHello(@PathVariable("name") String name) {
  
  Builder feignBuilder= Feign.builder().decoder(new ResponseEntityDecoder(new SpringDecoder(new ObjectFactory<HttpMessageConverters>(){
   public HttpMessageConverters getObject() throws BeansException {
    return new HttpMessageConverters();
   }
  }))).client(new ApacheHttpClient()).contract(new SpringMvcContract());
  computeClient = feignBuilder.target(TestApi.class,discoveryClient.getInstances(SERVER_NAME).get(0).getUri().toString());
        return computeClient.testHello(name);
    }
}

主要是通过discoveryClient.getInstances(SERVER_NAME).get(0).getUri()获取注册中心实例地址。

posted @ 2018-03-29 08:47  Gyoung  阅读(1618)  评论(0编辑  收藏  举报