sentinel 端口_Sentinel整合Apollo进行规则持久化(二)

阅读文本大概需要3分钟。

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

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

主要启动如下三个服务:

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

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

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

6726d0bcd138c639e9c49d0a5d05e2e9.png

0x02:sentinel-dashboard代码改动

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

配置类设置参数

389eb1d40cf37b9a094ecfe5b2df61b9.png

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

d27ca6d19f77c56ad5f1bbb4266751b0.png

Apollo管理后台地址

349744ef143fa0944eb09b46b4e7104f.png

token

FlowRuleApolloProvider修改

57cd32a6a494b607fefea15fc2802c8d.png

FlowRuleApolloPublisher修改:

a310d05777b5702c00cb7f0d329efad9.png

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

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

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

添加配置并发布

e7c48c0b4300661086e98f5871759fd8.png

源码

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文件

<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.0modelVersion><groupId>com.sentinelgroupId><artifactId>olive-apollo-sentinel-datasourceartifactId><version>0.0.1-SNAPSHOTversion><packaging>jarpackaging><parent><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-parentartifactId><version>2.1.3.RELEASEversion><relativePath/> parent><name>olive-nacos-sentinel-datasourcename><url>http://maven.apache.orgurl><properties><project.build.sourceEncoding>UTF-8project.build.sourceEncoding><java.version>1.8java.version>properties><dependencies><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-sentinelartifactId>dependency><dependency><groupId>com.alibaba.cspgroupId><artifactId>sentinel-datasource-apolloartifactId>dependency>dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-dependenciesartifactId><version>Greenwich.SR3version><type>pomtype><scope>importscope>dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-alibaba-dependenciesartifactId><version>2.1.0.RELEASE version><type>pomtype><scope>importscope>dependency>dependencies>dependencyManagement>project>
  • 修改配置文件

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-flow
app: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<String, Object> getUser() {Map<String, Object> 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

ed5d9eeebce0f087881919f679bcea9d.png

  • 访问Sentinel

edeee2115e5167415f820b8c36cf3640.png

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

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

参考:

往期精彩

01 Sentinel如何进行流量监控

02 Nacos源码编译

03 基于Apache Curator框架的ZooKeeper使用详解

04 spring boot项目整合xxl-job

05 互联网支付系统整体架构详解

关注我

每天进步一点点

22098b9804374ca53f4268b5e48e27d5.png

喜欢!在看☟
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值