SpringCloudAlibaba之OpenFeign

OpenFeign是对Feign的进一步封装,支持SpringMVC的标准注解。

OpenFeign,可简单总结为以下几点:

1、OpenFeign 只涉及 Consumer 与 Provider 无关。因为其是用于 Consumer 调用 Provider 的;

2、OpenFeign 仅仅就是一个伪客户端,其不会对请求做任务的处理;

3、OpenFeign 是通过注解的方式实现 RESTful 请求的;

1、OpenFeign的使用
1.1、引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1.2、配置文件
spring:
  application:
    name: consumer-openfeign-8084
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        group: weilong
        namespace: fkasjfkasjsj
    openfeign:
      client:
        config:
          default: # 全局设置
            connect-timeout: 1000 # ms
            read-timeout: 5000    # ms
          provider-nacos:  # 针对某个 @FeignClient 进行设置
            connect-timeout: 10
            read-timeout: 5000    # ms
      compression:
        request:
          enabled: true  # 开启请求压缩功能
          mime-types: ["text/html","application/xml","/application/json","video/mp4"]
        response: # 开启响应压缩功能
          enabled: true
   # 开启feign 支持sentinel降级
feign:
  sentinel:
    enabled: true
1.3、代码编写
1.3.1、openfeign接口编写
@FeignClient(value = "provider-8081", path = "/test", fallback = FallbackImpl.class)
public interface FeignFallback {
}
// @FeignClient 中的value可以使用配置文件中配置的值(该值变量不能为关键字,需要自定义)
1.3.2、降级兜底类编写
@Component
public class FallbackImpl implements FeignFallback {

}
1.3.3、主启动类
@SpringBootApplication
@EnableFeignClients  // 添加启动OpenFeign接口调用功能
public class ApplicationConsumerOpenfeign8084 {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConsumerOpenfeign8084.class, args);
    }

}
2、负载均衡
2.1、引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
2.2、编写配置类
@SpringBootConfiguration
public class LoadbalenceConfig {
    @Bean
    public ReactorLoadBalancer<ServiceInstance> loadBalancer(Environment env, LoadBalancerClientFactory loadBalancerClientFactory){
        String name = env.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
        
    }
}
2.3、主启动类添加负载均衡注解
@SpringBootApplication
@EnableFeignClients
@LoadBalancerClients(defaultConfiguration = {LoadbalenceConfig.class}) // 自定义负载均衡方法
public class ApplicationConsumerOpenfeign8084 {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConsumerOpenfeign8084.class, args);
    }
}

总结:本文介绍了openfeign使用以及负载均衡用法。

        本人是一个从小白自学计算机技术,对运维、后端、各种中间件技术、大数据等有一定的学习心得,想获取自学总结资料(pdf版本)或者希望共同学习,关注微信公众号:上了年纪的小男孩。后台回复相应技术名称/技术点即可获得。(本人学习宗旨:学会了就要免费分享)

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值