SpringCloud H系列 alibaba 2.1.1 (一) sentinel 限流熔断(基于dubbo)

1.nacos安装

请参考 : docker 安装 nacos 1.X

2.项目父工程

请参考 : SpringCloud H系列 alibaba 2.1.1 (一) nacos1.X 注册中心 服务端

3.项目结构 

4.创建Dubbo api 子工程(存放公共类)

pom.xml 

    <parent>
        <groupId>com.alibaba</groupId>
        <artifactId>spring-cloud-hoxton</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>alibaba-sentinel-dubbo-api</artifactId>
    <packaging>jar</packaging>

TestService.java  创建测试接口

/**
 * @author Lion Li
 */
public interface TestService {
	String test(String name);
}

 5.创建Dubbo 服务提供者

首先在nacos创建配置文件 application-sentinel-dubbo-server.yml

 application-sentinel-dubbo-server.yml

dubbo:
  protocol:
    # 使用dubbo协议通信
    name: dubbo
    # dubbo 协议端口(-1表示自增端口,从20880开始)
    port: -1
  # 挂载到 Spring Cloud 注册中心
  registry:
    address: spring-cloud://localhost
  scan:
    # 指定 Dubbo 服务实现类的扫描基准包
    base-packages: com.alibaba.sentinel.dubbo.server.service
server:
  port: 8000

创建服务提供者子工程

pom.xml 

    <parent>
        <groupId>com.alibaba</groupId>
        <artifactId>spring-cloud-hoxton</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>alibaba-sentinel-dubbo-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>alibaba-sentinel-dubbo-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>

        <!-- sentinel 依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- sentinel dubbo 适配器 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-apache-dubbo-adapter</artifactId>
        </dependency>
    </dependencies>

 SentinelDubboServerApplication.java 启动类

/**
 * @author Lion Li
 */
@EnableDiscoveryClient // 开启注册中心客户端
@SpringBootApplication
public class SentinelDubboServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(SentinelDubboServerApplication.class, args);
	}
}

TestServiceImpl.java api实现类 

/**
 * @author Lion Li
 */
@Service // 这里使用的是dubbo的 @Service 注解 将注册到注册中心
@RefreshScope // nacos 配置自动刷新
public class TestServiceImpl implements TestService {

	@Value("${server.port:}")
	private String port;

	@Override
	public String test(String name) {
		return "服务器::端口:"+port+"::返回值 => " + name;
	}

}

 bootstrap.yml 配置文件

spring:
  application:
    name: alibaba-sentinel-dubbo-server
  cloud:
    # Nacos 服务发现与注册配置
    nacos:
      discovery:
        server-addr: 192.168.101.11:8848
      config:
        # 配置中心地址
        server-addr: 192.168.101.11:8848
        # 文件后缀
        file-extension: yml
        # 文件前缀
        prefix: application-sentinel-dubbo-server
        # 命名空间ID
        namespace: f799e2c7-3ab0-4e95-a7db-2150a91ec744

 启动服务 查看nacos

启动成功

6.创建Dubbo 服务消费者

首先在nacos创建配置文件 application-sentinel-dubbo-client.yml

dubbo:
  cloud:
    # 订阅服务名
    subscribed-services: alibaba-sentinel-dubbo-server
  protocol:
    # 使用dubbo协议通信
    name: dubbo
    # dubbo 协议端口(-1表示自增端口,从20880开始)
    port: -1
  # 挂载到 Spring Cloud 注册中心
  registry:
    address: spring-cloud://localhost
server:
  port: 8002

pom.xml

    <parent>
        <groupId>com.alibaba</groupId>
        <artifactId>spring-cloud-hoxton</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>alibaba-sentinel-dubbo-client</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>alibaba-sentinel-dubbo-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- sentinel 依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- sentinel dubbo 适配器 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-apache-dubbo-adapter</artifactId>
        </dependency>
    </dependencies>

 SentinelDubboClientApplication.java 启动类

/**
 * @author Lion Li
 */
@EnableDiscoveryClient  // 开启注册中心客户端
@SpringBootApplication
public class SentinelDubboClientApplication {
	public static void main(String[] args) {
		SpringApplication.run(SentinelDubboClientApplication.class, args);
	}
}

TestController.java  消费接口 

/**
 * @author Lion Li
 */
@RestController
public class TestController {

	@Reference // dubbo注入接口 从nacos注册中心获取服务器地址
	private TestService testService;

	@RequestMapping("/test")
	public String test(String name) {
		return testService.test(name);
	}
}

FlowRuleConfig.java  代码配置限流规则  将接口使用QPS规则限流为1(QPS代表查询量)  查询数量大于1则限流 

/**
 * @author Lion Li
 */
@Configuration
public class FlowRuleConfig {

    public FlowRuleConfig(){
        // 代码配置限流
        FlowRule flowRule = new FlowRule();
        // 限流资源接口
        flowRule.setResource("com.alibaba.sentinel.dubbo.api.TestService:test(java.lang.String)");
        // 限流数峰值
        flowRule.setCount(1);
        // QPS限流
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setLimitApp("default");
        FlowRuleManager.loadRules(Collections.singletonList(flowRule));
    }

}

bootstrap.yml 配置文件 

spring:
  application:
    name: alibaba-sentinel-dubbo-client
  cloud:
    # Nacos 服务发现与注册配置
    nacos:
      discovery:
        server-addr: 192.168.101.11:8848
      config:
        # 配置中心地址
        server-addr: 192.168.101.11:8848
        # 文件后缀
        file-extension: yml
        # 文件前缀
        prefix: application-sentinel-dubbo-client
        # 命名空间ID
        namespace: f799e2c7-3ab0-4e95-a7db-2150a91ec744

启动消费端服务 并查看nacos

启动成功

7.测试 功能 

请求消费端接口  localhost:8002/test?name=Lion Li

服务响应正常

8.测试 限流 

连续两次快速请求消费端接口  localhost:8002/test?name=Lion Li

第一次返回结果

第二次返回结果

限流成功 

项目已上传到gitee

地址: spring-cloud-alibaba-H-demo

如果帮到您了,请帮忙点个star

猜你喜欢

转载自blog.csdn.net/weixin_40461281/article/details/104105450