Nacos安装
下载
下载地址:github.com/alibaba/nac…
其他版本的可自行下载:github.com/alibaba/nac…
配置
- 下载完成后解压
- 进入conf目录,找到nacos-mysql.sql文件
- 打开mysql,创建一个名为nacos的数据库,然后导入上一步的sql文件
- 回到conf目录,找到application.properties文件
- 找到数据库配置,文件里面默认是用#号注释的,去掉#号后配置自己的数据库地址、账号及密码,
启动
- 打开cmd并进入进入bin目录
- 输入命令
startup.cmd -m standalone
- 浏览器打开http://127.0.0.1:8848/nacos/index.html
- 账号密码默认都是nacos
服务注册
pom文件配置
参考一下就好,直接copy可能会出错。注意:经过测试,如果不导入web依赖,无法注册到nacos。
<?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>
<artifactId>core</artifactId>
<description>核心代码</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
复制代码
启动类注解
加上@SpringCloudApplication注解
@SpringCloudApplication
public class CoreApplication {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(CoreApplication.class, args);
}
}
复制代码
yml配置
spring:
application.name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
复制代码
启动项目
打开nacos后台,可以看到服务已经注册成功
配置中心
添加bootstrap.properties文件
spring.application.name=nacos-provider
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=yaml
spring.profiles.active=prod
# 禁用自动刷新配置(默认启用)
#spring.cloud.nacos.config.refresh.enabled = false
复制代码
进入nacos后台添加配置
- Data Id的格式为
${spring.application.name}-${profile}. ${file-extension:properties}
或${spring.application.name}. ${file-extension:properties}
- 由bootstrap.properties的配置可得出Data Id为nacos-provider-prod.yaml
- 如果我们没有配置
spring.profiles.active
,可以写成nacos-provider.yaml
- 我们添加了两个配置文件,一个是nacos-provider-dev.yaml,一个nacos-provider-prod.yaml。修改bootstrap.properties中的
spring.profiles.active
就可以切换到不同配置。
配置中心总结
以往我们把配置信息写在项目的yml文件中,如果修改了配置文件就需要重启项目,微服务架构下我们会有多个服务,假如因为一次升级,需要修改所有服务的配置文件,这就非常麻烦了。这时候可以把配置写到nacos配置中心,在nacos后台统一修改配置,服务不需要重启就能读取到最新的配置。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END