feign-demo
本模块主要演示集成了Feign、Hystrix的Eureka客户端。
FeignClient方式调用接口
url | desc |
---|---|
http://localhost:8181/hello | 输出hello信息 [eureka-client] |
http://localhost:8181/user-feign/1 | 根据ID获取User [db-rest] |
http://localhost:8181/user-feign/getUserByName/张三 | 根据name获取User [db-rest] |
http://localhost:8181/user-feign/getUserByAddress/test | 根据地址获取User [db-rest] |
RestTemplate方式调用接口
url | desc |
---|---|
http://localhost:8181/user-rest/1 | 根据ID获取User [db-rest] |
Hystrix Dashboard监控
url | desc |
---|---|
http://localhost:8181/hystrix | 查看仪表盘 |
http://localhost:8181/hystrix.stream | 在仪表盘中增加监控 |
配置FeignClient
- 引入Maven依赖
org.springframework.cloud
spring-cloud-starter-feign
- 启用FeignClient
spring boot启动类增加@EnableFeignClients注解,使其自动扫描@FeignClient
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientFeignApplication.class, args);
}
}
定义需要通过FeignClient访问的接口列表,如下:
// value为服务名,对应spring.application.name。注意:此服务名必须已注册进Eureka服务中心
@FeignClient(value = "db-rest", fallback = UserServiceFeignClientFallback.class)
public interface UserServiceFeignClient extends UserService {
}
// 定义FeignClient接口访问列表。注意:地址一定要正确
public interface UserService {
@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
User getUser(@PathVariable("id") int id);
@RequestMapping(value = "/api/user/search/findByName?name={name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
User findUserByName(@PathVariable("name") String name);
@RequestMapping(value = "/api/user/search/findByAddress?address={address}", method = RequestMethod.GET)
String findUserByAddress(@PathVariable("address") String address);
}
// 定义各接口对应的fallback方法
@Component
public class UserServiceFeignClientFallback implements UserServiceFeignClient {
@Override
public User getUser(int id) {
return new User("getUser.Fallback", "feignClient return");
}
@Override
public User findUserByName(String name) {
return new User("findUserByName.Fallback", "feignClient return");
}
@Override
public String findUserByAddress(String address) {
return "fallback";
}
}
以下是RestTemplate调用方式:
public User getUser(int id) {
User user = restTemplate.exchange( "http://db-rest/api/user/{id}", HttpMethod.GET, null, new ParameterizedTypeReference<User>() { }, id).getBody();
return user;
}
配置Hystrix
- 引入Maven依赖
org.springframework.cloud
spring-cloud-starter-hystrix
- 增加Hystrix配置
以下仅配置了启用超时及超时时间
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
@HystrixProperty(name = "execution.timeout.enabled", value = "false") })
@RequestMapping(value = "/user-feign/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable int id) {
return userServiceFeignClient.getUser(id);
}
配置Hystrix Dashboard
- 引入Maven依赖
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
* 启用Hystrix Dashboard
spring boot启动类增加@EnableHystrixDashboard和@EnableCircuitBreaker注解,启用Hystrix Dashboard
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientFeignApplication.class, args);
}
}
运行截图
- Hystrix Dashboard监控
输入监控地址:http://localhost:8181/hystrix.stream
点击Monitor Stream,进入监控界面
当访问相关接口时,Hystrix仪表板将会显示每个断路器的健康情况。
- FeignClient方式,根据ID获取User
- RestTemplate方式,根据ID获取User
- FeignClient方式,根据地址返回User列表
- FeignClient方式,根据name获取User
- FeignClient方式,正常输出hello信息
- FeingClient + Hystrix方式,当服务异常停止后,输出hello方法对应的fallback信息