alibaba cloud+seata 分布式事务整合,logback+kafka+elk 日志收集

         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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值