1. 程式人生 > 其它 >alibaba cloud+seata 分散式事務整合,logback+kafka+elk 日誌收集

alibaba cloud+seata 分散式事務整合,logback+kafka+elk 日誌收集

技術標籤:後端java

         alibaba cloud+seata 分散式事務整合,logback+kafka+elk 日誌收集
一、搭建Sentinel控制檯
   1、下載sentienl的jar包,本例使用:sentinel-dashboard-1.7.2.jar,地址:https://github.com/alibaba/Sentinel/releases
   2、啟動命令:java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
   3、訪問地址:http://localhost:8080/,8080為Sentinel的預設埠
   4、輸入預設使用者名稱/密碼:sentinel/sentinel,進入首頁
二、搭建Sentinel客戶端
   1、引入依賴
     <!-- alibaba nacos sentinel -->
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
     </dependency>
   2、編輯application.yml檔案【注意:如果不需要安裝 sentinel 監控服務 可以不用配置】
     spring:
       cloud:
         sentinel:
           transport:
             # 指定控制檯服務的地址
             dashboard: localhost:8080
             # 應用與Sentinel控制檯互動的埠,應用本地會起一個該端口占用的HttpServer
             # 預設8719埠,假如埠被佔用,依次+1,直到找到未被佔用埠
             port: 8719
三、流量控制、熔斷降級 自定義異常
   @Component
   public class SentinelExceptionConfig implements BlockExceptionHandler {
       @Override
       public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) {
       if (e instanceof FlowException) {
           throw  new MyException(ErrorCode.MYB_111002);
       } else if (e instanceof DegradeException) {
           throw  new MyException(ErrorCode.MYB_111004);
       } else if (e instanceof ParamFlowException) {
           throw  new MyException(ErrorCode.MYB_200001);
       } else if (e instanceof AuthorityException) {
           throw  new MyException(ErrorCode.MYB_200003);
       } else if (e instanceof SystemBlockException) {
           throw  new MyException(ErrorCode.MYB_200004);
       }
       }
   }
四、feign 和 sentinel 整合
    1、引入 feign 依賴
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    2、開啟 feign 的熔斷功能,需要在配置檔案新增如下配置:
      feign:
        sentinel:
          enabled: true
     3、啟動類添加註解 @EnableFeignClients

五、整合 nacos
    1、引入依賴
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
     2、新增配置
         spring:
           application:
             name: shop-product
           cloud:
             nacos:
               discovery:
                 server-addr: 127.0.0.1:8848
     3、啟動類添加註解
     @EnableDiscoveryClient

六、閘道器專案
    1、不需要引入web相關,只需要引入下面的依賴即可:
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>

            <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
   2、啟動類添加註解
      @EnableDiscoveryClient
   3、新增配置:
      spring:
        cloud:
          nacos:
            discovery:
              server-addr: 127.0.0.1:8848
          gateway:
            discovery:
              locator:
                enabled: true #開啟從註冊中心動態建立路由的功能,利用微服務名進行路由
            routes:
            - id: shop_order
              uri: lb://shop-order
              predicates:
              - Path=/model_order/**
              filters:
              - StripPrefix=1
            - id: shop_product
              uri: lb://shop-product
              predicates:
              - Path=/model_product/**
              filters:
              - StripPrefix=1
        【注意】
          1、StripPrefix 的作用是在拼接訪問地址時。去掉斷言中的 model_order ,否則會訪問 lb://shop-order/model_order/**,報404錯誤
          2、自定義斷言和過濾器配置時,前面需要加 “-”
    4、重點:
       閘道器自定義全域性異常統一處理,事例:GlobalGatewayExceptionHandler

七、整合鏈路追蹤 sleuth
    1、引入依賴【每個微服務都要引入,包括閘道器】
       <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-sleuth</artifactId>
       </dependency>
八、整合kafka,收集日誌
      1、引入依賴
          <dependency>
               <groupId>com.github.danielwegener</groupId>
               <artifactId>logback-kafka-appender</artifactId>
               <version>0.2.0-RC2</version>
           </dependency>
           <dependency>
               <groupId>net.logstash.logback</groupId>
               <artifactId>logstash-logback-encoder</artifactId>
               <version>6.1</version>
           </dependency>
           <dependency>
               <groupId>ch.qos.logback</groupId>
               <artifactId>logback-classic</artifactId>
               <version>1.2.3</version>
           </dependency>
       2、新增logback-spring.xml配置檔案新增配置:
           <!-- This is the kafkaAppender -->
              <appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
                  <encoder>
                      <pattern>${myFilePattern}</pattern>
                  </encoder>
                  <topic>tjgx-log</topic>
                  <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy" />
                  <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />
                  <!-- <partition>0</partition> -->
                  <producerConfig>bootstrap.servers=localhost:9092</producerConfig>
              </appender>
              <root level="info">
                  <appender-ref ref="console" />
              </root>
九、引入nacos 配置中心功能
     1、依賴
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
     2、新增bootstrap.xml 配置,
            spring:
              profiles:
                active: dev
              application:
                name: shop-order
              cloud:
                nacos:
                  config:
                    server-addr: 127.0.0.1:8848
                    prefix: ${spring.application.name}
                    file-extension: yml
                    namespace: efff8d49-4265-4694-8bc0-4a59e53b826c
                    group: baohongjian-springcloudAlibaba
     3、自動刷需要在類上加  @RefreshScope
十、seate 分散式事務
    1、Seata術語
       TC (Transaction Coordinator) - 事務協調者
       維護全域性和分支事務的狀態,驅動全域性事務提交或回滾。
       TM (Transaction Manager) - 事務管理器
       定義全域性事務的範圍:開始全域性事務、提交或回滾全域性事務。
       RM (Resource Manager) - 資源管理器
       管理分支事務處理的資源,與TC交談以註冊分支事務和報告分支事務的狀態,並驅動分支事務提交或回滾
    2、引入依賴
            <!--引入seata-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
                <!--排除依賴,調整到和seata-server版本一致-->
                <exclusions>
                    <exclusion>
                        <groupId>io.seata</groupId>
                        <artifactId>seata-spring-boot-starter</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>io.seata</groupId>
                        <artifactId>seata-all</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--與seata-server 版本保持一致-->
            <dependency>
                <groupId>io.seata</groupId>
                <artifactId>seata-spring-boot-starter</artifactId>
                <version>1.4.0</version>
            </dependency>
    3、安裝seata-server,初始化nacos配置時,需要新增:
        service.vgroupMapping.shop-user=default
        service.vgroupMapping.shop-order=default
        service.vgroupMapping.shop-product=default
    4、配置每個微服務專案
       spring:
         application:
           name: shop-order
         cloud:
           alibaba:
             seata:
               tx-service-group: ${spring.application.name}
       seata:
         config:
           type: nacos
           nacos:
             server-addr: 127.0.0.1:8848
             group: SEATA_GROUP
             namespace: 1d75ab91-2bba-4bdc-8479-7c63c5a30321
         registry:
           type: nacos
           nacos:
             server-addr: 127.0.0.1:8848
             group : SEATA_GROUP
             namespace: 1d75ab91-2bba-4bdc-8479-7c63c5a30321
    5、如果專案做了異常統一處理,在呼叫微服務後,需要判斷返回結果,如果失敗,需要丟擲異常,才能回滾

細節:
  1、只有 get 請求 才會請求重試,post 請求則不會進行請求重試
  2、解決本地呼叫遠端微服務問題,讓本地服務只訂閱 不註冊成為服務,讓其他應用呼叫即可,配置如下:
     spring:
       cloud:
         nacos:
             register-enabled: false

demo

連結:https://pan.baidu.com/s/12aKQ0LZM_ITN1pUUz-b0Jw
提取碼:fkdy