apollo持久化sentinel_Sentinel整合Apollo进行规则持久化(二)

本文详细介绍了如何将Sentinel与Apollo配置中心整合,实现规则持久化。包括Apollo配置中心的启动、sentinel-dashboard的代码改动、在Apollo配置中心添加配置、微服务改造以及测试验证过程。
摘要由CSDN通过智能技术生成

【Sentinel整合Apollo进行规则持久化】对Sentinel的代码进行了改造,【Apollo配置中心管理后台的详解】对Apollo管理后台进行了讲解。今天就最终实现:Sentinel整合Apollo进行规则持久化

0x01:先把Apollo配置中心启动

主要启动如下三个服务:

apollo-configservice:提供配置获取接口,提供配置更新推送接口,接口服务对象为Apollo客户端

apollo-adminservice:提供配置管理接口,提供配置修改、发布等接口,接口服务对象为Portal,以及Eureka

apollo-portal:提供Web界面供用户管理配置

image

0x02:sentinel-dashboard代码改动

【Sentinel整合Apollo进行规则持久化】整体只是把相关代码移动了一些位置,现在还需做些许改动。

配置类设置参数

image

根据实际情况修改Apollo管理后台地址和开发平台授权token

image

Apollo管理后台地址

image

token

FlowRuleApolloProvider修改

image

FlowRuleApolloPublisher修改:

image

备注:这里要注意如果使用ApolloConfigUtil.getFlowDataId()方法获取flowDataId,则在Apollo配置中心需要按照约定建立配置项的Key。

改造完成后,并可以启动sentinel-dashboard

0x03:Apollo配置中心添加配置

添加配置并发布

image

源码

flowRules = [{"app":"pay-service","clusterMode":false,"controlBehavior":0,"count":300,"gmtModified":1596855658214,"grade":1,"id":1,"limitApp":"default","resource":"/getUser","strategy":0}]degrades = [{"resource": "/getUser","count": 50,"timeWindow": 5,"grade": 0},{"resource": "/getUser","count": 5,"timeWindow": 8,"grade": 2},{"resource": "/erro","count": 0.5,"timeWindow": 5,"grade": 1}]authoritys = [{"resource": "/getUser","limitApp": "192.168.12.215","strategy": 1}]paramflows = [{"resource": "/getUser","grade": 1,"paramIdx": 1,"count": 10,"paramFlowItemList": []}]systems = [{"qps": 20}]

0x04:微服务改造

新建项目olive-apollo-sentinel-datasource,对应的pom.xml文件

4.0.0

com.sentinel

olive-apollo-sentinel-datasource

0.0.1-SNAPSHOT

jar

org.springframework.boot

spring-boot-starter-parent

2.1.3.RELEASE

olive-nacos-sentinel-datasource

http://maven.apache.org

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

com.alibaba.cloud

spring-cloud-starter-alibaba-sentinel

com.alibaba.csp

sentinel-datasource-apollo

org.springframework.cloud

spring-cloud-dependencies

Greenwich.SR3

pom

import

com.alibaba.cloud

spring-cloud-alibaba-dependencies

2.1.0.RELEASE

pom

import

修改配置文件

server:

port: 8866

spring:

application:

name: pay-service #olive-apollo-sentinel-datasource

cloud:

sentinel:

transport:

port: 8719 # 向sentinel-dashboard传输数据的端口 默认:8719

dashboard: 127.0.0.1:8081 # sentinel-dashboard

log:

dir: ./logs # 默认值${home}/logs/csp/

switch-pid: true # 日志带上线程id

datasource: flow: # 流控规则

apollo:

namespaceName: application

flowRulesKey: flowRules

rule-type: flow #flow,degrade,authority,system, param-flow

degrade: # 熔断降级规则

apollo:

namespaceName: application

flowRulesKey: degrades

rule-type: degrade

authority: # 授权规则 未验证,官方不推荐 apollo:

namespaceName: application

flowRulesKey: authoritys

rule-type: authority

system: # 系统规则 apollo:

namespaceName: application

flowRulesKey: systems

rule-type: system

param-flow: # 热点规则

apollo:

namespaceName: application

flowRulesKey: paramflows

rule-type: param-flowapp:

id: payservice # 指定规则项目在 apollo 的app.id,要与 sentinel 控制台启动参数一致

apollo:

bootstrap:

enabled: true # 开启 apollo

namespaces: application

meta: http://192.168.56.1:8080

cacheDir: ./apolloconfig # 缓存文件位置

备注:注意这里的flowRuleKey的值,需要跟Apollo配置中心配置的Key一致,同时跟Sentinel中FlowRuleApolloProvider和FlowRuleApolloProvider类定义的flowDataId一致。另外,apollo.mata配置项的值为配置中心Eureka的地址

新建控制器

package com.olive.controller;

import java.util.HashMap;import java.util.Map;

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

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

@RestController

public class UserController {

@GetMapping("/getUser")

public Map getUser() {

Map result = new HashMap<>();

result.put("code", "000000");

result.put("message", "ok");

return result;

}

}

新建SpringBoot启动类

package com.olive;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublic class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

启动微服务

0x05:测试验证

访问接口

http://localhost:8866/getUser

image

访问Sentinel

image

备注:可以通过修改相关配置来验证,Apollo和Sentinel是否相互同步,即在Apollo修改后,是否Sentinel能获取最新配置;或者在Sentinel修改后,是否Apollo能获取最新配置。

最后,这里只改造了流控规则,其他规则的改造与以上改造一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值