sentinel-apollo-推模式

前言

我不知道你有沒有看上一篇sentinel-apollo拉模式,相信看過的同學對于那么復雜結(jié)構的數(shù)據(jù)很頭疼,起碼我們運維小哥明確表態(tài)了不會用的,那么有沒有通過在sentinel-dashboard直接修改然后同步到apollo上呢,也就是推模式.我也是經(jīng)過了一番痛苦折騰后才知道有.改起來也不是很復雜.

相關文章

參考文檔

sentinel-dashboard 修改步驟

修改好的代碼地址: https://github.com/zhaoyunxing92/sentinel-dashboard-apollo
如果你對需改代碼不感興趣可以直接跳過到整合部分,這個代碼你需要編譯一份可以運行就可以了

如果你看過sentinel的源碼就會發(fā)現(xiàn)其實人家已經(jīng)實現(xiàn)了一小部分推模式的代碼

前端

如果你很熟悉mvc并且做過mvc模式的開發(fā)那么看前端angular代碼我敢保證你僅僅需要半個小時就能掌握它甚至更少時間

app.js文件 resources/app/scripts/app.js

這個文件默認就是angular的入口文件,就如vue使用main.js ,路由都在app.js里面

.state('dashboard.flow', {
            templateUrl: 'app/views/flow_v2.html',
            url: '/v2/flow/:app', // 這對應的地址欄的url
            controller: 'FlowControllerV2', // 這個說明用的那個控制器,你只需要找到對應的文件就可以
            resolve: {
                loadMyFiles: ['$ocLazyLoad', function ($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'sentinelDashboardApp',
                        files: [
                            'app/scripts/controllers/flow_v2.js'  // 打開這個文件
                        ]
                    });
                }]
            }
        })

flow_v2.js resources/app/scripts/controllers/flow_v2.js

這個時候發(fā)現(xiàn)注入了一個 FlowServiceV2服務,可以打開FlowServiceV2服務的文件了

app.controller('FlowControllerV2', ['$scope', '$stateParams', 'FlowServiceV2', 'ngDialog',
  'MachineService',
  function ($scope, $stateParams, FlowService, ngDialog,
    MachineService) {
    $scope.app = $stateParams.app;

    $scope.rulesPageConfig = {
      pageSize: 10,
      currentPageIndex: 1,
      totalPage: 1,
      totalCount: 0,
    };

FlowServiceV2 resources/app/scripts/services/flow_service_v2.js

打開后你會發(fā)現(xiàn)里面都是跟后端通信的邏輯是不是很簡單.app.js > controller > service 三步驟

app.service('FlowServiceV2', ['$http', function ($http) {
    this.queryMachineRules = function (app, ip, port) {
        var param = {
            app: app,
            ip: ip,
            port: port
        };
        return $http({
            url: '/v2/flow/rules',
            params: param,
            method: 'GET'
        });
    };

后端

后端部分由于我修改的太多不可能都寫出來,我就總結(jié)下我在修改的時候遇到的問題吧

數(shù)據(jù)push到了apollo,sentinel-dashboard界面也能看到數(shù)據(jù),但是配置的規(guī)則就是不生效

這個問題是由于我們給apollo注入了不必要的屬性,可以參考issues

偽代碼 @JSONField(serialize = false)后數(shù)據(jù)就不會到apollo了

public class FlowRuleEntity implements RuleEntity {
    @JSONField(serialize = false)
    private Long id;
    @JSONField(serialize = false)
    private String app;
    @JSONField(serialize = false)
    private String ip;
    @JSONField(serialize = false)
    private Integer port;
}

整合流程

pom.xml 如果看過拉模式可以跳過

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

application.yml

注意里面的key不用隨意修改,除非你很清楚你在干什么

spring:
  application:
    name: spring-boot-sentinel-apollo
  cloud:
    sentinel:
      transport:
        port: 8719 # 向sentinel-dashboard傳輸數(shù)據(jù)的端口 默認:8719
        dashboard: localhost:8100 # sentinel-dashboard
      log:
        dir: ./logs # 默認值${home}/logs/csp/
        switch-pid: true # 日志帶上線程id
      datasource:
        flow: # 流控規(guī)則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.flowRules
            rule-type: flow #flow,degrade,authority,system, param-flow
        degrade: # 熔斷降級規(guī)則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.degradeRules
            rule-type: degrade
        authority: # 授權規(guī)則  未驗證,官方不推薦
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.authorityRules
            rule-type: authority
        system: # 系統(tǒng)規(guī)則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.systemRules
            rule-type: system
        param-flow: # 熱點規(guī)則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.paramFlowRules
            rule-type: param-flow
app:
  id: ${spring.application.name}
apollo:
  meta: http://127.0.0.1:8080
  cacheDir: ./apolloconfig  # 緩存文件位置

java

@SpringBootApplication
@EnableApolloConfig // 開啟apollo
public class SpringSentinelApolloServer {
    public static void main(String[] args) {
        SpringApplication.run(SpringSentinelApolloServer.class, args);
    }
}

jvm參數(shù)配置

-Denv=DEV

apollo申請token

apollo-token

sentinel-dashboard 設置token

java -jar sentinel-dashboard.jar --apollo.portal.token= apollo申請的token

測試接口

http://localhost:7853/sentinel/hello

apollo效果圖

sentinel-dashboard-push
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容