标签:eve 必须 pen new conf product package host display
1.添加依赖,低版本的依赖后面没有open
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2在启动加配置
3.创建接口,product为要调用的服务器的名字,getmapping必须和服务器的getmapping相同(即表示要调用的方法)
package com.staryeah.order.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; /** * @program: com.staryeah.order.client * @description: * @author: staryeah * @create: 2019-06-07 23:17 **/ @FeignClient(name = "product") public interface ProductClient { @GetMapping("/test") public void getTest(); }
4.在controller层中注入接口
@Slf4j @RestController public class ClientController { @Autowired private ProductClient productClient; // @Autowired // private LoadBalancerClient loadBalancerClient; // @Autowired // private RestTemplate restTemplate; @GetMapping("/get") public String get(){ //第一中方法 直接url写死 // RestTemplate restTemplate = new RestTemplate(); // String response = restTemplate.getForObject("http://localhost:9087/test", String.class); // log.info("response",response); // 第二种方法利用loadbalancerclient获取url,在使用resttemplate // RestTemplate restTemplate = new RestTemplate(); // ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT"); // String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort()); // String response = restTemplate.getForObject(url, String.class); // log.info("response",response); // System.out.println(response); // 第三种方法,通过配置config // String response = restTemplate.getForObject("http://PRODUCT/test", String.class); // 用feign获取 String response= productClient.getTest(); return response; } }
标签:eve 必须 pen new conf product package host display
原文地址:https://www.cnblogs.com/gaoqiaoliangjie/p/10989602.html