专业的编程技术博客社区

网站首页 > 博客文章 正文

干货:SpringBoot集成Nacos,填坑篇

baijin 2024-10-01 07:16:47 博客文章 8 ℃ 0 评论

集成环境说明

  • SpringBoot 2.2.13.RELEASE
  • Nacos 1.4.1
  • Nacos简介

    Nacos是阿里巴巴集团开源的平台,是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理的平台。

    Nacos 提供了一组简单易用的特性集,帮助快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos是构建以“服务”为中心的现代应用架构的服务基础设施。

    具体可以参考Nacos官方文档:https://nacos.io/zh-cn/docs/what-is-nacos.html

    Nacos下载安装

  • 从 https://github.com/alibaba/nacos/releases 下载nacos-server-1.4.1.zip安装包
  • unzip nacos-server-1.4.1.zip解压Nacos安装包
  • sh nacos/bin/startup.sh -m standalone,启动Nacos服务
  • 登录Nacos管理平台,账号、密码均默认为 nacos
  • SpringBoot集成Nacos

  • 创建SpringBoot项目,并追加相关依赖,包括Nacos,本文只集成了Nacos的配置中心功能,pom.xml文件如下:
  • <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            
            <version>2.2.13.RELEASE</version> <!-- 此处一定要注册Spring boot的版本号,需要nacos支持的版本才行  -->
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.luandy</groupId>
        <artifactId>nacos</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <name>nacos</name>
        <description>nacos project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- Nacos配置中心需要引入的依赖  -->
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>nacos-config-spring-boot-starter</artifactId>
                <version>0.2.7</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
  • 在application.properties配置文件中,追加Nacos server地址的配置
  • # Springboot服务端口号
    server.port=9990
    
    # nacos server服务地址
    nacos.config.server-addr=127.0.0.1:8848
  • SpringBoot启动类追加注解,此处的dataId需要在Nacos管理平台进行创建
  • /**
     * @author luandy
     */
    @SpringBootApplication
    @NacosPropertySource(dataId = "nacos-service", autoRefreshed = true)
    public class NacosApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosApplication.class, args);
        }
    
    }
  • 创建测试controller,通过@NacosValue的方式获取Nacos配置中心配置的值
  • /**
     * @author luandy
     * @create 2021/01/24 10:31
     */
    @RestController
    public class NacosController {
    
        @NacosValue(value = "${test.username}", autoRefreshed = true)
        private String userName;
    
        @GetMapping(value = "/getuname")
        public String getUserName() {
            System.out.println("userName is :" + userName);
            return userName;
        }
    }
  • 启动NacosApplication项目,访问http://localhost:9990/getuname,数据正常返回。
  • Nacos配置中心的配置如下:
  • 遇到的小坑儿

  • SpringBoot2.4.2版本集成Nacos1.4.1版本,总是报Error creating bean with name 'nacosConfigurationPropertiesBinder' 的错误。
  • org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nacosConfigurationPropertiesBinder': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError:

    解决办法:Nacos1.4.1好像还不支持SpringBoot2.3以上版本。请修改SpringBoot版本集成Nacos,Requires Spring Boot >= 2.2.0.RELEASE and < 2.3.0.M1


  • Nacos1.4.1管理平台,新创建用户,配置完用户角色和权限后,权限却未生效
  • 解决办法:Nacos默认未开启权限控制需要修改Nacos的/nacos/conf/application.properties文件,将nacos.core.auth.enabled属性修改为true,重启Nacos即可。

    # SpringBoot启动类上追加此配置(示例)
    @NacosPropertySource(dataId = "nacos-service", autoRefreshed = true)
    
    # 使用Nacos配置参数注解上也要追加此配置(示例)
    @NacosValue(value = "${test.username}", autoRefreshed = true)
    private String userName;


  • 配置了autoRefreshed属性却不生效
  • 解决办法:启动类和具体注解上都要追加autoRefreshed=true配置

    # SpringBoot启动类上追加此配置(示例)
    @NacosPropertySource(dataId = "nacos-service", autoRefreshed = true)
    
    # 使用Nacos配置参数注解上也要追加此配置(示例)
    @NacosValue(value = "${test.username}", autoRefreshed = true)
    private String userName;


  • 是否有必要使用mysql数据库
  • 解决办法:Nacos的0.7版本开始增加了支持mysql数据源能力,如果需要将配置持久化到数据库中,就使用mysql数据库,如果不需要持久化,可以不使用mysql。

    使用mysql数据库的,具体的操作步骤大概如下:

    1、安装ysql数据库,版本要求:5.6.5+

    2、初始化mysql数据库,数据库初始化文件:/nacos/conf/nacos-mysql.sql

    3、修改/nacos/conf/application.properties文件,增加支持mysql数据源配置即可。

    #*************** 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://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
    db.user.0=root
    db.password.0=root

    本文暂时没有评论,来添加一个吧(●'◡'●)

    欢迎 发表评论:

    最近发表
    标签列表