nacos安装配置以及配合SpringBoot踩坑步骤。坑爹!!!

本文详细描述了如何在Windows上安装Nacos2.2.3,包括配置数据库连接、SpringBoot集成、版本选择以及启动过程中遇到的问题和解决方案。特别提到了SpringBoot版本兼容性及NoClassDefFoundError错误的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装运行Nacos

  1. 安装(官方提供的地址,官网实例版本用的是2.2.3

  2. 为了方便,我是用的是nacos-server-2.2.3.zip。安装在window电脑上。
    在这里插入图片描述

  3. 安装解压,修改配置文件(/nacos/conf/application.properties)。

### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.

 spring.datasource.platform=mysql
 ### Count of DB:
 db.num=1
### Connect URL of DB:
 db.url.0=jdbc:mysql://192.168.204.130:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
 db.user.0=root
 db.password.0=root
 
### If turn on auth system:
nacos.core.auth.enabled=true
 ### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=test
nacos.core.auth.server.identity.value=test
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

  1. 在使用startup.cmd启动的时候,必须在本地jdk的环境变量配置JAVA_HOME才行。
  2. 在启动的时候可能存在闪退,建议在startup.cmd文件中的MODE改成。set MODE="standalone"
  3. 至此,运行startup.cmd。nacos的默认登录用户nacos、密码:nacos。

spring boot+nacos

注意选择版本,Nacos0.1.+对应的是spring boot1.+版本。nacos0.2.+对应的是spring boot2.+版本。

  1. spring boot版本。原本选择的2.7.0版本,结果报错
2024-03-27 13:48:50.833 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.834 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.837 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.837 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.838 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.838 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.838 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.838 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.840 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.840 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.841 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.841 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.843 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.843 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.844 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.844 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.845 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.845 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.846 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.846 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.847 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:48:50.847 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:49:01.316 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:49:01.316 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:49:01.340 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
2024-03-27 13:49:01.340 ERROR 18216 --- [           main] com.alibaba.spring.util.BeanUtils        : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null can't be resolved bean type!
java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
  1. 至此,退回2.3.9.RELEASE。
  2. 配置spring boot项目的配置文件(application.yml)
spring:
  application:
    name: nacos_test
nacos:
  config:
    namespace: bc92d44e-d648-43f8-af74-05da8c8787a7
    server-addr: 192.168.153.1:8848
    data-id: test.yaml
    group: DEFAULT_GROUP
    type: yaml
    username: nacos
    password: nacos

注意:namespace是nacos命名空间的Id、server-addr是IP地址以及对应的端口号。
千万注意,这是一个坑爹的data-id。这个要加配置文件的后缀,列入yaml。
3. 在spring boot的主启动类添加注解

@NacosPropertySource(dataId = "test",groupId = "DEFAULT_GROUP", autoRefreshed = true)

千万注意:这特么是一个坑爹的位置,这个里边的dataId不加文件后缀,是test就填写test。
4. 在pom.xml添加依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>
        <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>
    </dependencies>
  1. 测试一下,在spring boot中添加一个controller
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@RequestMapping("config")
public class NacosController {

    @NacosValue(value = "${name}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
}
  1. 测试之前,在nacos的文件中添加属性
    在这里插入图片描述
  2. 使用postman工具测试返回结果
    在这里插入图片描述

nacos服务注册

  1. pom.xml文件添加依赖
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>
  1. application.yml文件添加配置
nacos:
  config:
  discovery:
    auto-register: true
    server-addr: 192.168.204.130:8848
    register:
      enabled: true
      service-name: testServer
      group-name: DEFAULT_GROUP
    namespace: 63cfebe6-c63a-41a7-ba2e-1e3e82dc3812

server-addr填写自己的地址和端口,service-name设置服务名字。namespace设置命名空间ID。

  1. 测试
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值