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

JAVA乐园

共 5035字,需浏览 11分钟

 · 2020-08-11

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

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


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

主要启动如下三个服务:

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

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

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


0x02:sentinel-dashboard代码改动

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

配置类设置参数

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

Apollo管理后台地址


token

FlowRuleApolloProvider修改

FlowRuleApolloPublisher修改

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

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


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

添加配置并发布

源码

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中FlowRuleApolloProviderFlowRuleApolloProvider类定义的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<StringObject> getUser() {
        Map<StringObject> 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

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

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

参考:
https://www.cnblogs.com/kiwifly/p/11569190.html
https://blog.csdn.net/qq_29553063/article/details/90228487
https://segmentfault.com/a/1190000019094867



往期精彩



01 Sentinel如何进行流量监控

02 Nacos源码编译

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

04 spring boot项目整合xxl-job

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

关注我

每天进步一点点

喜欢!在看☟
浏览 31
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报