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

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

 

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

主要启动如下三个服务:

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

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

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

Sentinel整合Apollo进行规则持久化(二)_第1张图片

 

0x02:sentinel-dashboard代码改动

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

配置类设置参数

Sentinel整合Apollo进行规则持久化(二)_第2张图片

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

Sentinel整合Apollo进行规则持久化(二)_第3张图片

Apollo管理后台地址

 

Sentinel整合Apollo进行规则持久化(二)_第4张图片

token

FlowRuleApolloProvider修改

Sentinel整合Apollo进行规则持久化(二)_第5张图片

FlowRuleApolloPublisher修改:

Sentinel整合Apollo进行规则持久化(二)_第6张图片

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

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

 

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

添加配置并发布

Sentinel整合Apollo进行规则持久化(二)_第7张图片

源码

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-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 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;

@SpringBootApplication
public class Application {

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

}
  • 启动微服务

 

0x05:测试验证

  • 访问接口

http://localhost:8866/getUser

  • 访问Sentinel

Sentinel整合Apollo进行规则持久化(二)_第8张图片

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

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

你可能感兴趣的:(spring,java乐园)