Sentinel 适配了 Feign组件。如果想使用,除了引入 spring-cloud-starter-alibaba-sentinel 的依赖外还需要 2个步骤:
- 配置文件打开 Sentinel 对 Feign 的支持:feign.sentinel.enabled=true
- 加入 spring-cloud-starter-openfeign 依赖使 Sentinel starter 中的自动化配置类生效;
案例
需求如下:
实现sentinel_feign_client微服务通过Feign访问sentinel_feign_provider微服务的流量控制
创建sentinel_parent、eureka_server、sentinel_feign_provider、sentinel_feign_client工程,并在sentinel_feign_client中使用feign访问sentinel_feign_provider服务。
提供已经搭建好的微服务代码,地址如下:
https://download.csdn.net/download/u013938578/87767363
1 创建父工程sentinel_parent
在父工程的pom.xml文件中对SpringCloud依赖的进行管理
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
2 创建子工程eureka_server工程,作为注册中心
2.1 在pom.xml文件中引入依赖
<dependencies><!--eureka服务端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
</dependencies>
2.2 引导类中添加Eureka的服务注解
//声明当前应用是Eureka注册中心服务
@EnableEurekaServer
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
2.3 配置文件配置eureka
#设置服务端口
server:port: 9010#设置eureka服务的名称
spring:application:name: eureka-server#eureka配置
eureka:client:service-url:# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址defaultZone: http://127.0.0.1:9010/eureka# 不注册自己register-with-eureka: false# 不拉取服务fetch-registry: false
2.4 运行测试
启动项目,通过浏览器访问http://127.0.0.1:9010/,如果能看到如下界面就说明eureka注册中心已经配置成功
3 创建子工程sentinel_feign_provider,作为服务的提供方
3.1 在pom.xml文件中引入依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--eureka客户端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
3.2 引导类中添加Eureka客户端注解
@EnableEurekaClient //开启Eureka客户端发现功能
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
3.3 在application.yml文件中进行配置
#tomcat端口
server:port: 9011#应用的名称
spring:application:name: sentinel-feign-provider#eureka的配置
eureka:client:service-url:defaultZone: http://127.0.0.1:9010/eureka #要注册到的注册中心的地址
3.4 创建controller,方便sentinel_feign_client进行调用
package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ProviderController {@GetMapping("hello")public String hello(){return "Hello Sentinel!";}}
3.5 运行测试
启动项目,查看项目是否注册到eureka注册中心中
浏览器输入http://localhost:9011/hello,查看是否能访问成功。
4 创建子工程sentinel_feign_client
4.1 在pom.xml文件中引入依赖
项目中要使用Fegin来进行访问sentinel_fegin_provider微服务,所以需要额外引入spring-cloud-starter-openfeign依赖,整合sentinel还需要引入spring-cloud-starter-alibaba-sentinel依赖。
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--eureka客户端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--feign的起步依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
4.2 引导类中添加Eureka客户端注解
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@EnableEurekaClient //开启Eureka客户端发现功能
@SpringBootApplication
@EnableFeignClients
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
4.3 在application.yml文件中进行配置
在application.yml文件中要专门开启 Sentinel 对 Feign 的支持。
#tomcat端口
server:port: 9012#应用的名称
spring:application:name: sentinel-feign-clientcloud:sentinel:transport:dashboard: localhost:9000#eureka的配置
eureka:client:service-url:defaultZone: http://127.0.0.1:9010/eureka #要注册到的注册中心的地址
4.4 创建Feign代理接口
package com.example.demo.agent;import com.example.demo.service.FallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value="sentinel-feign-provider")
public interface FeignAgent {@GetMapping("/hello")String hello();
}
4.5 创建controller,使用Feign访问sentinel_feign_provider微服务。
package com.example.demo.controller;import com.example.demo.agent.FeignAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@Autowiredprivate FeignAgent feignAgent;@GetMapping("hello")public String hello(){return feignAgent.hello();}
}
4.6.运行测试
启动项目,查看项目是否注册到eureka注册中心中。
浏览器输入http://localhost:9012/hello,查看是否能访问成功。
5 整合sentinel
5.1 在sentinel_feign_client工程的pom.xml中引入Spring Cloud Alibaba Sentinel依赖
<!--Spring Cloud Alibaba Sentinel依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.1.0.RELEASE</version></dependency>
5.2 在application.yml中开启Sentinel对Feign的支持
#应用的名称
spring:cloud:sentinel:transport:dashboard: localhost:9000# sentinel对feign支持操作
feign:sentinel:enabled: true
5.3 创建FallbackService,作为流控降级回调类,并在FeignAgent进行流控降级回调配置
package com.example.demo.service;import com.example.demo.agent.FeignAgent;
import org.springframework.stereotype.Component;//限流或者降级的回调类
@Component
public class FallbackService implements FeignAgent {//限流和降级的处理@Overridepublic String hello() {return "系统繁忙,请稍候";}
}
修改FeignAgent
package com.example.demo.agent;import com.example.demo.service.FallbackService;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value="sentinel-feign-provider",fallback = FallbackService.class)
public interface FeignAgent {@GetMapping("/hello")String hello();
}
5.4 运行测试
启动项目,在Sentinel控制中增加关于资源的流控规则,Sentinel和Feign整合时,流控规则的编写形式为:http请求方式:协议://服务名/请求路径跟参数,例如:GET:http://sentinel-feign-provider/hello。
通过浏览器输入http://localhost:9012/hello,慢速刷新,则持续显示”Hello Sentinel”;快速刷新则会交替出现”Hello Sentinel”和“系统繁忙,请稍候”。这说明对资源限流成功。