项目使用的Spring Cloud为Hoxton版本,Spring Boot为2.2.2.RELEASE版本
序号 | 内容 | 链接地址 |
---|---|---|
1 | Spring Cloud入门-十分钟了解Spring Cloud | https://blog.csdn.net/ThinkWon/article/details/103715146 |
2 | Spring Cloud入门-Eureka服务注册与发现(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103726655 |
3 | Spring Cloud入门-Ribbon服务消费者(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103729080 |
4 | Spring Cloud入门-Hystrix断路器(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103732497 |
5 | Spring Cloud入门-Hystrix Dashboard与Turbine断路器监控(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103734664 |
6 | Spring Cloud入门-OpenFeign服务消费者(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103735751 |
7 | Spring Cloud入门-Zuul服务网关(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103738851 |
8 | Spring Cloud入门-Config分布式配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103739628 |
9 | Spring Cloud入门-Bus消息总线(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103753372 |
10 | Spring Cloud入门-Sleuth服务链路跟踪(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103753896 |
11 | Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103756139 |
12 | Spring Cloud入门-Gateway服务网关(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103757927 |
13 | Spring Cloud入门-Admin服务监控中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103758697 |
14 | Spring Cloud入门-Oauth2授权的使用(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103761687 |
15 | Spring Cloud入门-Oauth2授权之JWT集成(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103763364 |
16 | Spring Cloud入门-Oauth2授权之基于JWT完成单点登录(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103766368 |
17 | Spring Cloud入门-Nacos实现注册和配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103769680 |
18 | Spring Cloud入门-Sentinel实现服务限流、熔断与降级(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103770879 |
19 | Spring Cloud入门-Seata处理分布式事务问题(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103786102 |
20 | Spring Cloud入门-汇总篇(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103786588 |
Spring Cloud OpenFeign 是声明式的服务调用工具,它整合了Ribbon和Hystrix,拥有负载均衡和服务容错功能,本文将对其用法进行详细介绍。
Feign是声明式的服务调用工具,我们只需创建一个接口并用注解的方式来配置它,就可以实现对某个服务接口的调用,简化了直接使用RestTemplate来调用服务接口的开发量。Feign具备可插拔的注解支持,同时支持Feign注解、JAX-RS注解及SpringMvc注解。当使用Feign时,Spring Cloud集成了Ribbon和Eureka以提供负载均衡的服务调用及基于Hystrix的服务容错保护功能。
这里我们创建一个feign-service模块来演示feign的常用功能。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
server:
port: 8701
spring:
application:
name: feign-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServiceApplication.class, args);
}
}
我们通过@FeignClient注解实现了一个Feign客户端,其中的value为user-service表示这是对user-service服务的接口调用客户端。我们可以回想下user-service中的UserController,只需将其改为接口,保留原来的SpringMvc注释即可。
@FeignClient(value = "user-service", fallback = UserFallbackService.class)
public interface UserService {
@PostMapping("/user/insert")
Result insert(@RequestBody User user);
@GetMapping("/user/{id}")
Result<User> getUser(@PathVariable Long id);
@GetMapping("/user/listUsersByIds")
Result<List<User>> listUsersByIds(@RequestParam List<Long> ids);
@GetMapping("/user/getByUsername")
Result<User> getByUsername(@RequestParam String username);
@PostMapping("/user/update")
Result update(@RequestBody User user);
@PostMapping("/user/delete/{id}")
Result delete(@PathVariable Long id);
}
@RestController
@RequestMapping("/user")
public class UserFeignController {
@Autowired
private UserService userService;
@PostMapping("/insert")
public Result insert(@RequestBody User user) {
return userService.insert(user);
}
@GetMapping("/{id}")
public Result<User> getUser(@PathVariable Long id) {
return userService.getUser(id);
}
@GetMapping("/listUsersByIds")
public Result<List<User>> listUsersByIds(@RequestParam List<Long> ids) {
return userService.listUsersByIds(ids);
}
@GetMapping("/getByUsername")
public Result<User> getByUsername(@RequestParam String username) {
return userService.getByUsername(username);
}
@PostMapping("/update")
public Result update(@RequestBody User user) {
return userService.update(user);
}
@PostMapping("/delete/{id}")
public Result delete(@PathVariable Long id) {
return userService.delete(id);
}
}
启动eureka-service,两个user-service,feign-service服务,启动后注册中心显示如下:
多次调用http://localhost:8701/user/1进行测试,可以发现运行在8201和8202的user-service服务交替打印如下信息:
2019-12-27 17:11:57.416 INFO 18096 --- [nio-8202-exec-1] c.j.s.controller.UserController : 根据id获取用户信息,用户名称为:jourwon
2019-12-27 17:12:04.897 INFO 18096 --- [nio-8202-exec-2] c.j.s.controller.UserController : 根据id获取用户信息,用户名称为:jourwon
2019-12-27 17:12:05.262 INFO 18096 --- [nio-8202-exec-3] c.j.s.controller.UserController : 根据id获取用户信息,用户名称为:jourwon
Feign中的服务降级使用起来非常方便,只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可,下面我们为UserService接口添加一个服务降级实现类。
需要注意的是它实现了UserService接口,并且对接口中的每个实现方法进行了服务降级逻辑的实现。
@Component
public class UserFallbackService implements UserService {
@Override
public Result insert(User user) {
return new Result("调用失败,服务被降级",500);
}
@Override
public Result<User> getUser(Long id) {
return new Result("调用失败,服务被降级",500);
}
@Override
public Result<List<User>> listUsersByIds(List<Long> ids) {
return new Result("调用失败,服务被降级",500);
}
@Override
public Result<User> getByUsername(String username) {
return new Result("调用失败,服务被降级",500);
}
@Override
public Result update(User user) {
return new Result("调用失败,服务被降级",500);
}
@Override
public Result delete(Long id) {
return new Result("调用失败,服务被降级",500);
}
}
修改@FeignClient注解中的参数,设置fallback为UserFallbackService.class即可。
@FeignClient(value = "user-service",fallback = UserFallbackService.class)
public interface UserService {
}
#在Feign中开启Hystrix
feign:
hystrix:
enabled: true
关闭两个user-service服务,重新启动feign-service;
调用http://localhost:8701/user/1进行测试,可以发现返回了服务降级信息。
Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。
我们通过java配置来使Feign打印最详细的Http请求日志信息。
@Configuration
public class FeignConfig {
@Bean
Logger.Level level() {
return Logger.Level.FULL;
}
}
配置UserService的日志级别为debug。
logging:
level:
com.jourwon.springcloud.service: debug
调用http://localhost:8701/user/1进行测试,可以看到以下日志。
2019-12-27 17:16:32.188 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] ---> GET http://user-service/user/1 HTTP/1.1
2019-12-27 17:16:32.188 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] ---> END HTTP (0-byte body)
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] <--- HTTP/1.1 200 (3ms)
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] connection: keep-alive
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] content-type: application/json
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] date: Fri, 27 Dec 2019 09:16:32 GMT
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] keep-alive: timeout=60
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] transfer-encoding: chunked
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser]
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] {"data":{"id":1,"username":"jourwon","password":"123456"},"message":"操作成功","code":200}
2019-12-27 17:16:32.192 DEBUG 18424 --- [-user-service-9] c.j.springcloud.service.UserService : [UserService#getUser] <--- END HTTP (94-byte body)
feign:
hystrix:
enabled: true #在Feign中开启Hystrix
compression:
request:
enabled: false #是否对请求进行GZIP压缩
mime-types: text/xml,application/xml,application/json #指定压缩的请求数据类型
min-request-size: 2048 #超过该大小的请求会被压缩
response:
enabled: false #是否对响应进行GZIP压缩
logging:
level: #修改日志级别
com.jourwon.springcloud.service: debug
在Feign中配置Ribbon可以直接使用Ribbon的配置,具体可以参考Spring Cloud Netflix Ribbon:服务消费者之服务调用(Hoxton版本)
在Feign中配置Hystrix可以直接使用Hystrix的配置,具体可以参考Spring Cloud Netflix Hystrix:断路器-服务容错保护(Hoxton版本)
springcloud-learning
├── eureka-server -- eureka注册中心
├── user-service -- 提供User对象CRUD接口的服务
└── feign-service -- feign服务调用测试服务
GitHub项目源码地址