4-CloudAlibaba-Sentinel(整合OpenFeign)学习笔记2020.10.24

36 篇文章 2 订阅
13 篇文章 0 订阅

4-CloudAlibaba-Sentinel(整合OpenFeign)学习笔记2020.10.24

前言: (Cloud官网GitHub官网)

Sentinel与OpenFeign组件兼容。要使用它,除了引入sentinel-starter依赖关系之外,还需要完成以下两个步骤:

  • 在属性文件中启用伪装的Sentinel支持。 feign.sentinel.enabled=true
  • 添加openfeign starter依赖项以触发并启用sentinel starter

1.0 引入spring-cloud-starter-openfeign依赖

在原有学习Sentinel工程模块下增加openfeign依赖

		<!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

1.1.1 配置文件application.yml 添加, 打开 Sentinel 对 Feign 的支持

server:
  port: 8080

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: 119.xx.xxx.xxx:8848 # Nacos服务注册中心地址
    sentinel:
      transport:
      	port: 8719
        dashboard:  119.xx.xxx.xxx:8090 # 配置Sentinel dashboard地址
   
# 暴露监控
management:
  endpoints:
    web:
      exposure:
        include: '*'
        
# 打开Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true 

1.1.2 在启动类加上@EnableFeignClients注解,开启Feign的功能

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class SentinelService
{
    public static void main(String[] args) {
        SpringApplication.run(SentinelService.class, args);
    }
}

1.1.3 简单的用法FeignClient

//注解要指定服务名 _不能有下划线
@FeignClient(value = "nacos-provider",fallback = FeginClientImpl.class) 
public interface FeginClient {

    //nacos-provider服务中名字为"/getPort/nacos/{id}"的接口
    @GetMapping(value = "/getPort/nacos/{id}")
    String getPort(@PathVariable("id") Integer id);
}

//-------------------------------分割线---------------------------------------------
/**
 * @Author: ZhiHao
 * @Date: 2020/10/23 17:50
 * @Description: 降级方法实现
 * @Versions 1.0
 **/
@Component //别忘记注册入框架管理
public class FeginClientImpl implements FeginClient {
    @Override
    public String getPort(Integer id) {
        return "调用失败了!!!!!!!!!"+id;
    }
}

调用之前学习nacos的提供者接口。

1.1.4 增加api接口进行启动测试

	@Autowired
    private FeginClient feginClient;

    @GetMapping("/testFeginClient")
    public CommonResult testFeginClient() {
        String feginClientPort = feginClient.getPort(666);
        return new CommonResult(HttpStatus.HTTP_OK, "正常响应", feginClientPort);
    }

PS: 如果启动遇到了 如下异常Caused by: java.lang.AbstractMethodError: com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidatateMetadata(Ljava/lang/Class;)Ljava/util/List;

那是因为fegin.context接口的定义为parseAndValidateMetadata。就是之前版本中定义的方法名拼写错误。

解决方法: 看这里

1.1.5 测试结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6wRjoTLI-1603506775253)(https://s1.ax1x.com/2020/10/23/BESNZT.jpg)]

1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懵懵懂懂程序员

如果节省了你的时间, 请鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值