安装运行Nacos
-
为了方便,我是用的是nacos-server-2.2.3.zip。安装在window电脑上。
-
安装解压,修改配置文件(/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
- 在使用startup.cmd启动的时候,必须在本地jdk的环境变量配置JAVA_HOME才行。
- 在启动的时候可能存在闪退,建议在startup.cmd文件中的MODE改成。
set MODE="standalone"
- 至此,运行startup.cmd。nacos的默认登录用户nacos、密码:nacos。
spring boot+nacos
注意选择版本,Nacos0.1.+对应的是spring boot1.+版本。nacos0.2.+对应的是spring boot2.+版本。
- 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
- 至此,退回2.3.9.RELEASE。
- 配置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>
- 测试一下,在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;
}
}
- 测试之前,在nacos的文件中添加属性
- 使用postman工具测试返回结果
nacos服务注册
- pom.xml文件添加依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
- 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。
- 测试