sentinel-apollo-推模式

前言

我不知道你有沒有看上一篇sentinel-apollo拉模式,相信看過的同學對於那麼複雜結構的數據很頭疼,起碼我們運維小哥明確表態了不會用的,那麼有沒有通過在sentinel-dashboard直接修改然後同步到apollo上呢,也就是推模式.我也是經過了一番痛苦折騰後才知道有.改起來也不是很複雜.

相關文章

參考文檔

sentinel-dashboard 修改步驟

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

如果你看過sentinel的源碼就會發現其實人家已經實現了一小部分推模式的代碼

前端

如果你很熟悉mvc並且做過mvc模式的開發那麼看前端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

這個時候發現注入了一個 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

打開後你會發現裏面都是跟後端通信的邏輯是不是很簡單.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'
        });
    };

後端

後端部分由於我修改的太多不可能都寫出來,我就總結下我在修改的時候遇到的問題吧

數據push到了apollo,sentinel-dashboard界面也能看到數據,但是配置的規則就是不生效

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

僞代碼 @JSONField(serialize = false)後數據就不會到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傳輸數據的端口 默認:8719
        dashboard: localhost:8100 # sentinel-dashboard
      log:
        dir: ./logs # 默認值${home}/logs/csp/
        switch-pid: true # 日誌帶上線程id
      datasource:
        flow: # 流控規則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.flowRules
            rule-type: flow #flow,degrade,authority,system, param-flow
        degrade: # 熔斷降級規則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.degradeRules
            rule-type: degrade
        authority: # 授權規則  未驗證,官方不推薦
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.authorityRules
            rule-type: authority
        system: # 系統規則
          apollo:
            namespaceName: application
            flowRulesKey: sentinel.systemRules
            rule-type: system
        param-flow: # 熱點規則
          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參數配置

-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

最後

如果你想了解更多的文章可以微信搜索zhaoyx92,或者掃碼關注,7*24的技術支持
zhaoyx92

文章同步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章