参考上一篇中所述,2020.0.0版本的SpringCloud对Hystrix,Ribbon等进行了清洗,不在支持相关组件,本篇是使用Hoxton版本(SpringBoot 2.3.8)进行演示。
1. 创建feign-service服务
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
配置文件 applicaiton.yml
spring:
application:
name: feign-service
server:
port: 8701
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true
feign:
hystrix:
enabled: true #开启熔断器hystrix
Feign 采用接口式访问服务,不需要像
Ribbon
的用显示的声明RestTemplate
调用
UserFeignController
@RestController
@RequestMapping("/user")
public class UserFeignController {
//@Qualifier("cn.iminqin.springboot.springcloud.learn.feignservice.service.UserService")
@Autowired
private UserService userService;
@GetMapping("/{id}")
public CommonResult getUser(@PathVariable Long id) {
return userService.getUser(id);
}
@GetMapping("/getByUsername")
public CommonResult getByUsername(@RequestParam String username) {
return userService.getByUsername(username);
}
@PostMapping("/create")
public CommonResult create(@RequestBody User user) {
return userService.create(user);
}
@PostMapping("/update")
public CommonResult update(@RequestBody User user) {
return userService.update(user);
}
@PostMapping("/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
return userService.delete(id);
}
}
UserService
@FeignClient(value = "user-service",fallback = UserFallbackService.class,path = "/user")
public interface UserService {
@PostMapping("/create")
CommonResult create(@RequestBody User user);
@GetMapping("/{id}")
CommonResult<User> getUser(@PathVariable Long id);
@GetMapping("/getByUsername")
CommonResult<List<User>> getByUsername(@RequestParam String username);
@GetMapping("/getUserByIds")
CommonResult<List<User>> getUserByIds(@RequestParam List<Long> ids);
@PostMapping("/update")
CommonResult update(@RequestBody User user);
@PostMapping("/delete/{id}")
CommonResult delete(@PathVariable Long id);
}
@FeignClient(value="",fallback=)
value表示该服务对接的service中在注册中心申明的instance-id
fallback指明熔断的类,该类必须要实现@FeignClient标明的接口,且需要标上@Component
。
UserFallbackService
@Component
public class UserFallbackService implements UserService {
public CommonResult create(User user) {
return new CommonResult("服务失败,服务被降级",500);
}
public CommonResult<User> getUser(Long id) {
return new CommonResult<>(new User(-1L,"failUsername","failPassword"),"调用失败,服务被降级",500);
}
public CommonResult<List<User>> getByUsername(String username) {
return new CommonResult<>(new ArrayList<>(),"调用失败,服务被降级",500);
}
public CommonResult<List<User>> getUserByIds(List<Long> ids) {
return new CommonResult<>(new ArrayList<>(),"调用失败,服务被降级",500);
}
public CommonResult update(User user) {
return new CommonResult("调用失败,服务被降级",500);
}
public CommonResult delete(Long id) {
return new CommonResult("调用失败,服务被降级",500);
}
}
2. 使用
- 开启eureka-service注册中心
- 不同端口上开启两个user-service服务。
- 开启feign-service服务,自动实现负载均衡
- down掉user-service服务,会使用服务降级。
3. 注意点(后续再补充)
- 配置中的
feign.hystrix.enabled
项,在2020.0.x版本后,使用feign.circuitbreaker.enabled
且需要配置别的熔断器。 - SpringBoot启动类使用
@EnableFeignClients
Annotation。