目录
1)到https://github.com/nobodyiam/apollo-build-scripts 网址下载apollo-build-scripts-master.zip包,解压缩到本地
2)创建数据库:ApolloPortalDB和ApolloConfigDB,在MySQL中执行sql文件夹下的两个文件
4) 在apollo-build-scripts-master文件下打开git bash,执行 ./demo.sh start
5) 打开http://localhost:8070 可以看到Apollo的登陆页面,账号密码是:apollo/admin
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对限流规则持久化成功;
1) 访问http://localhost:8002/hello ,返回: aceleeyy.com
2)打开sentinel dashboard控制台,刷新页面,可以看到/hello接口的流控规则多了一条记录,对应Apollo配置sentinel.flowRules的值
3)快速调用/hello接口,大概5次后返回: Blocked by Sentinel (flow limiting)
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)创建数据库:ApolloPortalDB
和ApolloConfigDB
,在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个端口当前没有被使用
- http://localhost:8070 Portal客户端的服务
- http://localhost:8080 ConfigService、MetaServer、Eureka服务
- http://localhost:8090 可能是AdminService服务
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
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
- 点击创建项目:
- 部门: 随便选一个,这里选择样例部门2
- 应用id:输入项目的应用名acelee-alibaba-sentinel-apollo
- 应用名称:输入项目的应用名acelee-alibaba-sentinel-apollo
- 应用负责人:只能选择apollo,这个可以参考Apollo的配置用户来增加用户
点击右上角的“新增配置”:
- Key:输入sentinel.flowRules
- Value:配置规则是一个数组类型,数组中的每个对象是针对每一个保护资源的配置对象,每个对象中的属性解释如下:
- resource:资源名,即限流规则的作用对象
- limitApp:流控针对的调用来源,若为 default 则不区分调用来源
- grade:限流阈值类型(QPS 或并发线程数);
0
代表根据并发数量来限流,1
代表根据QPS来进行流量控制- count:限流阈值
- strategy:调用关系限流策略
- controlBehavior:流量控制效果(直接拒绝、Warm Up、匀速排队)
- clusterMode:是否为集群模式
[ { "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 系列文章,敬请期待!