001 SpringCloudAlibaba整合 - Nacos注册配置中心、Sentinel流控、Zipkin链路追踪、Admin监控

发布于:2025-02-15 ⋅ 阅读:(26) ⋅ 点赞:(0)

SpringCloudAlibaba

1.版本依赖关系

官方地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

由于 Spring Boot 3.0,Spring Boot 2.7~2.4 和 2.4 以下版本之间变化较大,目前企业级客户老项目相关 Spring Boot 版本仍停留在 Spring Boot 2.4 以下,为了同时满足存量用户和新用户不同需求,社区以 Spring Boot 3.0 和 2.4 分别为分界线,同时维护 2022.x、2021.x、2.2.x 三个分支迭代。如果不想跨分支升级,如需使用新特性,请升级为对应分支的新版本。

2022.x 分支

适配 Spring Boot 3.0,Spring Cloud 2022.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记): (注意,该分支 Spring Cloud Alibaba 版本命名方式进行了调整,未来将对应 Spring Cloud 版本,前三位为 Spring Cloud 版本,最后一位为扩展版本,比如适配 Spring Cloud 2022.0.0 版本对应的 Spring Cloud Alibaba 第一个版本为:2022.0.0.0,第个二版本为:2022.0.0.1,依此类推)

Spring Cloud Alibaba Version Spring Cloud Version Spring Boot Version
2022.0.0.0* Spring Cloud 2022.0.0 3.0.2
2022.0.0.0-RC2 Spring Cloud 2022.0.0 3.0.2
2022.0.0.0-RC1 Spring Cloud 2022.0.0 3.0.0
2021.x 分支

适配 Spring Boot 2.4,Spring Cloud 2021.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):

Spring Cloud Alibaba Version Spring Cloud Version Spring Boot Version
2021.0.5.0* Spring Cloud 2021.0.5 2.6.13
2021.0.4.0 Spring Cloud 2021.0.4 2.6.11
2021.0.1.0 Spring Cloud 2021.0.1 2.6.3
2021.1 Spring Cloud 2020.0.1 2.4.2
2.2.x 分支

适配 Spring Boot 为 2.4,Spring Cloud Hoxton 版本及以下的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):

Spring Cloud Alibaba Version Spring Cloud Version Spring Boot Version
2.2.10-RC1* Spring Cloud Hoxton.SR12 2.3.12.RELEASE
2.2.9.RELEASE Spring Cloud Hoxton.SR12 2.3.12.RELEASE
2.2.8.RELEASE Spring Cloud Hoxton.SR12 2.3.12.RELEASE
2.2.7.RELEASE Spring Cloud Hoxton.SR12 2.3.12.RELEASE
2.2.6.RELEASE Spring Cloud Hoxton.SR9 2.3.2.RELEASE
2.2.1.RELEASE Spring Cloud Hoxton.SR3 2.2.5.RELEASE
2.2.0.RELEASE Spring Cloud Hoxton.RELEASE 2.2.X.RELEASE
2.1.4.RELEASE Spring Cloud Greenwich.SR6 2.1.13.RELEASE
2.1.2.RELEASE Spring Cloud Greenwich 2.1.X.RELEASE
2.0.4.RELEASE(停止维护,建议升级) Spring Cloud Finchley 2.0.X.RELEASE
1.5.1.RELEASE(停止维护,建议升级) Spring Cloud Edgware 1.5.X.RELEASE

组件版本关系

每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本如下表所示(注意,Spring Cloud Dubbo 从 2021.0.1.0 起已被移除出主干,不再随主干演进):

Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Version Dubbo Version Seata Version
2022.0.0.0 1.8.6 2.2.1 4.9.4 ~ 1.7.0
2022.0.0.0-RC2 1.8.6 2.2.1 4.9.4 ~ 1.7.0-native-rc2
2021.0.5.0 1.8.6 2.2.0 4.9.4 ~ 1.6.1
2.2.10-RC1 1.8.6 2.2.0 4.9.4 ~ 1.6.1
2022.0.0.0-RC1 1.8.6 2.2.1-RC 4.9.4 ~ 1.6.1
2.2.9.RELEASE 1.8.5 2.1.0 4.9.4 ~ 1.5.2
2021.0.4.0 1.8.5 2.0.4 4.9.4 ~ 1.5.2
2.2.8.RELEASE 1.8.4 2.1.0 4.9.3 ~ 1.5.1
2021.0.1.0 1.8.3 1.4.2 4.9.2 ~ 1.4.2
2.2.7.RELEASE 1.8.1 2.0.3 4.6.1 2.7.13 1.3.0
2.2.6.RELEASE 1.8.1 1.4.2 4.4.0 2.7.8 1.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE 1.8.0 1.4.1 4.4.0 2.7.8 1.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE 1.8.0 1.3.3 4.4.0 2.7.8 1.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE 1.7.1 1.2.1 4.4.0 2.7.6 1.2.0
2.2.0.RELEASE 1.7.1 1.1.4 4.4.0 2.7.4.1 1.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE 1.7.0 1.1.4 4.4.0 2.7.3 0.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE 1.6.3 1.1.1 4.4.0 2.7.3 0.7.1

2.基础项目构建

1.引入全局pom文件


   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.11</version>
        <relativePath/>
    </parent>
    <groupId>com.li</groupId>
    <artifactId>spring-cloud-alibaba-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>cloud-common</module>
        <module>cloud-picture</module>
        <module>cloud-login</module>
        <module>cloud-gateway</module>
        <module>cloud-data-clean</module>
        <module>cloud-data-production</module>
        <module>cloud-admin</module>
        <module>cloud-auth</module>
    </modules>

    <dependencies>
        <!--由于2021版本去除了-bootstrap.yml支持、所以我们要加上去-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
  
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.11</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.4.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!--maven插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 取消查找本项目下的Main方法:为了解决Unable to find main class的问题 -->
                    <mainClass>none</mainClass>
                     <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
                    <classifier>execute</classifier> 
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.创建对应的模块

目前创建了这些,测试用

        <module>cloud-common</module> <!--公共模块-->
        <module>cloud-picture</module><!--图片模块-->
        <module>cloud-login</module><!--登录模块-->
        <module>cloud-gateway</module><!--网关模块-->
        <module>cloud-data-clean</module>
        <module>cloud-data-production</module>
        <module>cloud-admin</module> <!--监控模块-->
        <module>cloud-auth</module><!--认证模块-->

创建启动类和yml配置文件,微服务项目yml文件名建议使用bootstrap.yml,加载优先级大于application

配置各个模块yml

server:
  port: 10000
spring:
  profiles:
    active: dev
  application:
    name: cloud-admin
    
#信息断点
info:
  tags:
    environment: cloud-admin
#日志收集
logging:
  file:
    name: ./log/admin-application.log
  pattern:
    file: '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} 
      %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} 
      %m%n%wEx'
    

引入各模块基础公共依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${org.projectlombok.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!--开启自定义 配置 需要提示功能-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

3.SpringBootAdmin监控服务整合

1.cloud-admin服务搭建

1.导入服务端依赖
       <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

2.主启动类添加@EnableAdminServer注解启用监控

2.客户端配置

1.引入客户端和端点监控依赖
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.配置yml
spring:
  boot:
    admin:
      client:
        # Admin Server的URL,若是要注册到多个admin服务端上,用逗号分隔就可以
        # url: http://localhost:8080,http://localhost:8081
        url: http://127.0.0.1:10000
        instance:
          # 使用IP的方式
          prefer-ip: true
          #按实例显示标签
          metadata:
            tags:
              environment: cloud-auth
          
management:
  endpoint:
    health:
      show-details: always  # 显示health指标的详细信息,否则只会显示 status
      
  #暴露所有端点,生产环境不建议
  endpoints:
    web:
      exposure:
        include: ["*"]                 

3.启动测试

访问服务端:http://localhost:10000/applications/cloud-admin

4.Nacos注册中心、配置中心整合

1.下载、安装

官方文档及下载地址:https://nacos.io/download/nacos-server/?spm=5238cd80.2ef5001f.0.0.3f613b7ctaHA8j

下载完后解压

修改启动模式

如果直接启动则默认是集群方式启动会报错,需进入binstartup.cmd配置文件修改modestandalone,单结点启动

保存后双击启动即可

访问 http://localhost:8848/nacos/ ,用户名密码默认nacos,默认端口号8848

创建命名空间

点击创建即可

2.注册中心整合

1.引入pom服务注册发现依赖

对需要注册的服务模块引入

       <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

2.yml配置
spring:
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 127.0.0.1:8848
        #命名空间id,不配置默认为public
        # namespace: 5bd397d3-505f-44f5-95fa-a62e631b9b41
3.主启动类添加@EnableDiscoveryClient注解
4.启动测试

如果配置了命名空间则需要切换对应的空间

3.配置中心整合

1.引入pom依赖
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

2.yml配置
spring:
  cloud:
    nacos:
      config:
        # 配置中心地址
        server-addr: 127.0.0.1:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
        #命名空间配置
        namespace: 5bd397d3-505f-44f5-95fa-a62e631b9b41
        #组配置
        #group: dev
3.配置nacos数据库连接

进入nacos目录下conf下的application.properties文件进行编辑

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
 db.url.0=jdbc:mysql://xxx:3306/xxx?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
 db.user.0=root
 db.password.0=xxx

### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2

数据库执行该sql文件,重启

4.配置中心创建yml文件

注意:

1.文件名必须带.yml

2.组配置可以不给,默认default_group,如果给了则需要配置group

3.后缀名根据spring的profiles的active切换

例如:有配置中心有四个文件配置,本地dev和prod配置相同

cloud-admin-dev.yml 组配置dev 配置 test:1

cloud-admin-prod.yml 组配置dev 配置 test:2

cloud-admin-prod.yml 组配置prod 配置 test:3

cloud-admin-dev.yml 组配置prod 配置 test:4

测试结果:

本地yml组配置 本地spring.profiles.active配置 获取test结果
dev dev 1
dev prod 2
prod prod 3
prod dev 4

5.Gateway网关整合

1.添加pom依赖

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

注意:由于网关已经包含了spring-boot-starter-web依赖,如果重复引入需排除掉

2.配置跨域

@Configuration
public class ApiCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}

3.yml路由配置

spring:
  cloud:
    gateway:
      #路由配置:转发规则
      routes:
        # id:唯一标识。默认是一个UUID
        # uri:转发路径
        # predicates:条件,用于请求网关路径的匹配规则
        - id: test_route
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu

        - id: picture_route
          uri: lb://cloud-picture
          predicates:
            - Path=/api/picture/**,/hello
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: login_route
          uri: lb://cloud-login
          predicates:
            - Path=/api/login/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: gateway-route
          uri: lb://cloud-gateway
          predicates:
            - Path=/api/gateway/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: admin-route
          uri: lb://cloud-admin
          predicates:
            - Path=/api/admin/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: production-route
          uri: lb://cloud-production
          predicates:
            - Path=/api/production/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

详细配置可参考另一篇文章: SpringCloudNetflix - Feign 调用、Hystix 熔断和 Gateway 网关

https://blog.csdn.net/weixin_54158370/article/details/125165136

6.Sentinel流控整合

1.下载、安装

下载地址:https://github.com/alibaba/Sentinel/tags

本地使用版本1.8.5

下载后通过java -jar启动,输入http://localhost:8080/访问,默认账号密码sentinel

2.整合

1.引入pom依赖
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

2.yml配置
spring: 
  cloud:
    #监控限流
    sentinel:
      # 是否初始化时就全部加载 默认关闭,如果关闭启动时候可能会看不到服务
      eager: true
      transport:
        #通信端口 假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口,默认8719
        port: 8719 
        #客户端地址,默认8080
        dashboard: 127.0.0.1:8080 
3.启动测试

7.Sleuth、Zipkin 链路追踪整合

1.下载、安装

下载地址:https://repo1.maven.org/maven2/io/zipkin/zipkin-server/

1.安装启动zipkin java -jar zipkin.jar

2.访问zipkin web界面。http://localhost:9411/

2.整合

1.引入pom依赖
  <!--sleuth-zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

(zipkin中已经依赖了sleuth,所以可以不导)

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

2.配置yml
spring:
  sleuth:
    web:
      client:
        # 开启采集链路
        enabled: true
    sampler:
      # 默认采集是 0.1(百分之十),生产环境采用默认,测试环境可以修改为1.0
      probability: 1.0

  # zipkin服务所在地址
  zipkin:
    base-url: http://127.0.0.1:9411/
    discovery-client-enabled: false #让nacos把它当成一个URL,而不要当做服务名
3.启动测试


网站公告

今日签到

点亮在社区的每一天
去签到