OpenFeign-负载均衡与熔断

参考上一篇中所述,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. 使用

  1. 开启eureka-service注册中心
  2. 不同端口上开启两个user-service服务。
  3. 开启feign-service服务,自动实现负载均衡
  4. down掉user-service服务,会使用服务降级。

3. 注意点(后续再补充)

  1. 配置中的 feign.hystrix.enabled项,在2020.0.x版本后,使用feign.circuitbreaker.enabled且需要配置别的熔断器。
  2. SpringBoot启动类使用@EnableFeignClientsAnnotation。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值