基础配置
开发工具 :IDEA
MAVEN :3.6
JDK : 1.8
Spring boot :1.5.9.RELEASE
Spring cloud : Edgware.RELEASE
pox配置信息
哪个客户端要消费其他客户端,哪个就加上以下配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
前提条件:
在上一篇的基础上写一个服务端和两个客户端.
cloud_eureka,cloud_client1,clud_client2(切记application.properties中应用名不能是下划线)
端口:8761,8081,8082
cloud_client1:
cloud_client1中写一个HelloController:
package com.cloud.client1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/show")
public class HelloController {
@GetMapping("/hello")
public String toHello(){
return "show say Hello!";
}
@GetMapping("/noHello")
public String toNoHello(){
return "show say no Hello!";
}
}
cloud_client2:
cloud_client2的pox.xml中添加上面feign配置。
启动文件中添加:
@EnableFeignClients
注意:导入包为:
import org.springframework.cloud.openfeign.EnableFeignClients;
application.properties配置:
spring.application.name=cloud-client2
server.port=8082
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.thymeleaf.cache=false
feign.client.config.feignName.connectTimeout= 5000
feign.client.config.feignName.readTimeout=5000
feign.hystrix.enabled=false
创建一个Feign接口
(切记是value不是name,不然会报错
*“Exception with Eureka client Expected authority at index 7: http://”*或者feign.FeignException: status 404 reading CloudClient1Feign#showNoHel
)
package com.cloud.client2.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "cloud-client1")
public interface CloudClient1Feign {
@RequestMapping(value = "/show/hello",method = RequestMethod.GET)
String showHello();
@RequestMapping(value = "/show/noHello",method = RequestMethod.GET)
String showNoHello();
}
创建一个IndexController
package com.cloud.client2.controller;
import com.cloud.client2.feign.CloudClient1Feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/to")
public class IndexController {
@Autowired
private CloudClient1Feign cloudClient1Feign;
@GetMapping("/hello")
public String sayHello(){
return "to say Hello!";
}
@GetMapping("/noHello")
public String sayNOHello(){
return "to say no Hello!";
}
@GetMapping("/getShow")
public String GetShow(){
return cloudClient1Feign.showHello();
}
@GetMapping("/getNoShow")
public String GetNoShow(){
return cloudClient1Feign.showNoHello();
}
}
启动cloud_eureka,cloud_client1,cloud_client2
访问http://localhost:8761/
访问http://localhost:8081/show/hello
访问http://localhost:8081/show/hello
cloud_client1上是show,cloud_client2是to
cloud_client2通过feign得到了show