首页 > 其他分享 >Spring Cloud Alibaba

Spring Cloud Alibaba

时间:2022-12-12 02:11:24浏览次数:36  
标签:spring boot nacos Alibaba Cloud Spring org alibaba cloud

依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

一、Nacos 服务注册中心

Nacos 支持AP和CP模式的切换

截屏20221211 164431png

  • 客户端

关键注解

@SpringBootApplication @EnableDiscoveryClient

依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.kk</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

配置

server:
  port: 8080

spring:
  application:
    name: nacos-payment-service
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.109:8848

management:
  endpoints:
    web:
      exposure:
        include: '*'

二、Nacos 作为配置中心

同spring-cloud-config一样,在项目初始化的时候,要保证先从配置中心进行配置拉取,完成后才能保证项目的正常启动。

2.1 基础配置

pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.kk</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>    

需要两个yml配置文件: application.ymlbootstrap.yml

bootstrap.yml

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.109:8848
      config:
        server-addr: 192.168.101.109:8848
        file-extension: yaml

management:
  endpoints:
    web:
      exposure:
        include: '*'

application.yml

spring:
  profiles:
    active: dev

2.2 分类配置

标签:spring,boot,nacos,Alibaba,Cloud,Spring,org,alibaba,cloud
From: https://www.cnblogs.com/qqkkOvO/p/16975086.html

相关文章