Spring Cloud Alibaba(5)Sentinel使用Apollo对存储规则持久化 带源码

15 篇文章 3 订阅
14 篇文章 0 订阅

目录

1. Apollo的介绍

2.源码地址:

3.博文参考地址:

4.Apollo的启动

1)到https://github.com/nobodyiam/apollo-build-scripts 网址下载apollo-build-scripts-master.zip包,解压缩到本地

2)创建数据库:ApolloPortalDB和ApolloConfigDB,在MySQL中执行sql文件夹下的两个文件

3)配置demo.sh 里的数据库连接信息

 4) 在apollo-build-scripts-master文件下打开git bash,执行 ./demo.sh start

 5) 打开http://localhost:8070 可以看到Apollo的登陆页面,账号密码是:apollo/admin

5.Sentinel的启动

6. 整合Apollo存储规则

1) 新建一个spring boot项目acelee-alibaba-sentinel-apollo,在pom.xml文件里添加sentinel、apollo依赖

2) 在Windows系统中创建C:\opt\settings\server.properties目录文件,内容如下:

3) 在resource目录下,application.properties文件里增加sentinel、apollo的配置 

 4)在Apollo配置中心,创建项目 acelee-alibaba-sentinel-apollo

 5)在项目中新建controller包,下面新建HelloController.java类

6)在Application启动类上添加@EnableApolloConfig

 7)启动应用:如果看到下图1是Apollo信息;下图2是sentinel信息,其中加载到1 FlowRule;证明sentinel整合apollo对限流规则持久化成功;

 7.验证sentinel限流

1) 访问http://localhost:8002/hello ,返回: aceleeyy.com

2)打开sentinel dashboard控制台,刷新页面,可以看到/hello接口的流控规则多了一条记录,对应Apollo配置sentinel.flowRules的值

3)快速调用/hello接口,大概5次后返回: Blocked by Sentinel (flow limiting)

8.sentinel规则修改 


 

1. Apollo的介绍

参考官方文档

2.源码地址:

https://gitee.com/acelee723/acelee-alibaba-sentinel-apollo

3.博文参考地址:

http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-2/

4.Apollo的启动

1)到https://github.com/nobodyiam/apollo-build-scripts 网址下载apollo-build-scripts-master.zip包,解压缩到本地

 

2)创建数据库:ApolloPortalDBApolloConfigDB,在MySQL中执行sql文件夹下的两个文件

3)配置demo.sh 里的数据库连接信息

注意:填入的用户需要具备对ApolloPortalDB和ApolloConfigDB数据的读写权限。

#apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=用户名
apollo_config_db_password=密码(如果没有密码,留空即可)

# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=用户名
apollo_portal_db_password=密码(如果没有密码,留空即可)

注意:不要修改demo.sh的其它部分

 4) 在apollo-build-scripts-master文件下打开git bash,执行 ./demo.sh start

./demo.sh start     启动服务                    ./demo.sh stop  停止服务

分别使用8070, 8080, 8090端口,请确保这3个端口当前没有被使用

 5) 打开http://localhost:8070 可以看到Apollo的登陆页面,账号密码是:apollo/admin

5.Sentinel的启动

可参考博文 Spring Cloud Alibaba(3)Sentinel接口限流整合入门 带源码

 在sentinel-dashboard-1.6.0.jar所在文件夹下cmd,执行java -jar sentinel-dashboard-1.6.0.jar --server.port=8081

输入http://localhost:8081 

 

6. 整合Apollo存储规则

1) 新建一个spring boot项目acelee-alibaba-sentinel-apollo,在pom.xml文件里添加sentinel、apollo依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aceleeyy</groupId>
    <artifactId>acelee-alibaba-sentinel-apollo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>acelee-alibaba-sentinel-apollo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-apollo</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2) 在Windows系统中创建C:\opt\settings\server.properties目录文件,内容如下:

这里需要了解Apollo对多环境的配置,这里设置的是每个环境不同的配置服务地址,读者需要根据自己的实际情况修改。

3) 在resource目录下,application.properties文件里增加sentinel、apollo的配置 

  • app.id:Apollo中的创建的项目名称,这里采用spring.application.name参数的引用,从而达到服务名与配置项目名一致的效果
  • apollo.meta:Apollo Meta Server环境信息的地址,根据上面启动MetaServer配置
  • spring.cloud.sentinel.transport.dashboard:sentinel dashboard的访问地址,根据上面准备工作中启动的实例配置
  • spring.cloud.sentinel.datasource.ds.apollo.namespaceName:Apollo的空间名
  • spring.cloud.sentinel.datasource.ds.apollo.flowRulesKey:配置规则的key名称
  • spring.cloud.sentinel.datasource.ds.apollo.rule-type:该参数是spring cloud alibaba升级到0.2.2之后增加的配置,用来定义存储的规则类型。所有的规则类型可查看枚举类:org.springframework.cloud.alibaba.sentinel.datasource.RuleType,每种规则的定义格式可以通过各枚举值中定义的规则对象来查看,比如限流规则可查看:com.alibaba.csp.sentinel.slots.block.flow.FlowRule
spring.application.name=acelee-alibaba-sentinel-apollo
server.port=8002

# apollo config
app.id=${spring.application.name}
apollo.meta=http://localhost:8080

# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8081

# sentinel datasource apollo
spring.cloud.sentinel.datasource.ds.apollo.namespaceName=application
spring.cloud.sentinel.datasource.ds.apollo.flowRulesKey=sentinel.flowRules
spring.cloud.sentinel.datasource.ds.apollo.rule-type=flow

 4)在Apollo配置中心,创建项目 acelee-alibaba-sentinel-apollo

  • 点击创建项目:
  1. 部门: 随便选一个,这里选择样例部门2
  2. 应用id:输入项目的应用名acelee-alibaba-sentinel-apollo
  3. 应用名称:输入项目的应用名acelee-alibaba-sentinel-apollo
  4. 应用负责人:只能选择apollo,这个可以参考Apollo的配置用户来增加用户

点击右上角的“新增配置”:

  1.  Key:输入sentinel.flowRules
  2. Value:配置规则是一个数组类型,数组中的每个对象是针对每一个保护资源的配置对象,每个对象中的属性解释如下:
  • resource:资源名,即限流规则的作用对象
  • limitApp:流控针对的调用来源,若为 default 则不区分调用来源
  • grade:限流阈值类型(QPS 或并发线程数);0代表根据并发数量来限流,1代表根据QPS来进行流量控制
  • count:限流阈值
  • strategy:调用关系限流策略
  • controlBehavior:流量控制效果(直接拒绝、Warm Up、匀速排队)
  • clusterMode:是否为集群模式
  1. [
        {
            "resource": "/hello",
            "limitApp": "default",
            "grade": 1,
            "count": 5,
            "strategy": 0,
            "controlBehavior": 0,
            "clusterMode": false
        }
    ]

 

点击提交后,可看到如下图配置记录,现在还未生效,需要发布;关于Apollo配置与application.properties的对应关系如下: 

点击发布,可以看到要发布的属性值,点击发布,看到配置记录的发布状态变为“已发布”; 

 

 5)在项目中新建controller包,下面新建HelloController.java类

package com.aceleeyy.aceleealibabasentinelapollo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "aceleeyy.com";
    }

}

6)在Application启动类上添加@EnableApolloConfig

package com.aceleeyy.aceleealibabasentinelapollo;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableApolloConfig
@SpringBootApplication
public class AceleeAlibabaSentinelApolloApplication {

    public static void main(String[] args) {
        SpringApplication.run(AceleeAlibabaSentinelApolloApplication.class, args);
    }

}

 7)启动应用:如果看到下图1是Apollo信息;下图2是sentinel信息,其中加载到1 FlowRule;证明sentinel整合apollo对限流规则持久化成功;

 7.验证sentinel限流

1) 访问http://localhost:8002/hello ,返回: aceleeyy.com

2)打开sentinel dashboard控制台,刷新页面,可以看到/hello接口的流控规则多了一条记录,对应Apollo配置sentinel.flowRules的值

3)快速调用/hello接口,大概5次后返回: Blocked by Sentinel (flow limiting)

8.sentinel规则修改 

在使用Apollo存储规则配置的时候与Nacos存储一样,对于Sentinel控制台这些数据是只读的,也就是说:

  • Sentinel控制台中修改规则:仅存在于服务的内存中,不会修改Apollo中的配置值,重启后恢复原来的值。
  • Apollo控制台中修改规则:服务的内存中规则会更新,Apollo中持久化规则也会更新,重启后依然保持。

欢迎关注博主博客,后期博主会持续更新spring cloud alibaba 系列文章,敬请期待!   

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值