Springboot 集成nacos:配置中心&注册中心

1、版本信息

Springboot 2.2.7.RELEASE
spring-cloud Hoxton.RELEASE
Nacos 1.3.2
Spring Cloud Alibaba Version 2.2.6.RELEASE

2、 主要依赖

		<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
       </dependency>
		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
		<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
      </dependency>

3、启动类用的到注解为:

@EnableDiscoveryClient(import org.springframework.cloud.client.discovery.EnableDiscoveryClient;)

4、修改以前的yml配置为:bootstrap.yml

spring:
  profiles:
    active: dev
  application:
    name: api
  cloud:
    nacos:
      config:
        # 配置中心配置文件分组,默认为DEFAULT_GROUP
        # group: TEST_GROUP
        # 配置中心配置文件后缀,默认为properties
        file-extension: yml
        # 配置中心地址
        server-addr: xx.xx.xx.xx:8848
        # 配置中心命名空间ID,默认为public
        namespace: xxxxx-xxxxx-xxxx-xxxx-xxxxxxxx
        # 配置中心配置文件Data Id 的格式是 ${prefix}-${spring.profile.active}.${file-extension},会同时加载配置文件
        # ${prefix}-${spring.profile.active}.${file-extension}和${prefix}.${file-extension}的属性,若有相同属性
        # 则以${prefix}-${spring.profile.active}.${file-extension}的属性为准
        shared-configs:
          - data-id: application.yml
            refresh: true
          - data-id: application-${spring.profiles.active}.yml
            refresh: true #是否支持自动刷新
      discovery:
        # 注册中心的地址
        server-addr: xx.xx.xx.xx:8848
        # 配置中心命名空间ID,默认为public
        namespace: 6ba5609b-xxxx-xxx-xxxx-xxxx

5、nacos配置yml

在这里插入图片描述

在这里插入图片描述

3、 注意事项

1、检查nacos的配置yml内容&格式是否正取,注册中心是否启动
2、检查各组件的版本是是配套的(springboot不同的版本需要的依赖的其他组件的版本也不同,若需要其他版本请自行研究,欢迎一起探讨)
3、yml格式检查是否正确。
4、…

Spring Boot 集成 Nacos 配置中心是为了提供一个动态和分布式的应用配置管理解决方案。Nacos 是阿里巴巴开源的一个配置中心,用于存储、管理和分发配置信息。以下是集成 Nacos 配置中心的基本步骤: 1. 添加依赖:在你的 Maven 或 Gradle 项目中添加 Spring Cloud Config 和 Nacos 的依赖。 Maven: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-config</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-config' implementation 'com.alibaba.cloud:spring-cloud-alibaba-nacos-config' ``` 2. 配置客户端:在 `application.yml` 或 `application.properties` 文件中配置 Nacos 配置中心的信息,包括地址、命名空间(namespace)等。 ```yaml spring: cloud: config: server: nacos: uri: http://localhost:8848 name-spaces: default username: admin password: admin ``` 3. 配置 Nacos 配置源:使用 `@EnableConfigServer` 注解启用配置服务器功能,并且可以配置是否扫描特定目录或包下的配置文件。 4. 使用 ConfigClient 注解:在需要注入配置的地方使用 `@ConfigurationProperties` 注解从 Nacos 中获取配置。 例如: ```java @Configuration @ConfigurationProperties(prefix = "app") public class AppConfig { private String property1; // getters and setters } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qing_wyt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值