Nacos是阿里生态下, 用于微服务注册与发现、配置管理实时刷新的微服务平台. 默认使用嵌入式持久化, 可支持MySQL数据库持久化

安装Nacos服务

1.下载nacos服务 版本 1.3.2 Windows下下载zip包并解压

2.可使用默认方式持久化, 跳过此步骤. 如果使用mysql, 需要创建数据库nacos, 执行conf/nacos-mysql.sql, 并修改application.properties, 配置MySQL连接信息

### 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=root
db.password=root

3.双击bin/startup.cmd, 启动nacos, 如果是单节点启动, 需要修改startup.cmd, 否则会报错

4.访问地址 ​​http://localhost:8848/nacos/index.html​​ username: nacos password: nacos

set MODE="standalone" // 默认为cluster
set FUNCTION_MODE="all"
set SERVER=nacos-server
set MODE_INDEX=-1
set FUNCTION_MODE_INDEX=-1
set SERVER_INDEX=-1
set EMBEDDED_STORAGE_INDEX=-1
set EMBEDDED_STORAGE=""
调整provider

1.调整pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 如果使用配置中心, 需要依赖此jar -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>

2.配置bootstrap.yml, 注意名称一定要是bootstrap

server:
port: 8079

spring:
application:
name: provider

cloud:
nacos:
config:
enable: true
server-addr: localhost:8848
discovery:
enabled: true
server-addr: localhost:8848

3.设置Applicaiton

@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

}

4.添加controller

@RestController
public class IndexController {
@GetMapping("/index")
private String index() {
return "success";
}
}
调整consumer

1.调整pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

</dependencies>

2.配置bootstrap.yml, 注意名称一定要是bootstrap

server:
port: 8081

spring:
application:
name: consumer

cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml # 此处为配置使用的后缀名
discovery:
server-addr: localhost:8848

3.设置Applicaiton

@EnableFeignClients
@SpringBootApplication
public class ConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

4.添加FeginClient

@FeignClient(name = "provider")
public interface ProviderFeignClient {

@GetMapping("/index")
String index();

}

5.添加配置映射

@Component
@RefreshScope // 添加此注解表示会实时更新value 另外需要给属性添加get set方法
public class NacosConfig {
@Value("${is_test:true}") // 此处true为默认值
private boolean isTest;
public boolean isTest() { return isTest; }
public void setIsTest(boolean isTest) { this.isTest = isTest; }
}

6.添加controller, controller处不能添加@RefreshScope注解, 否则使用@Value获取不到配置信息

@RestController
public class IndexController {
@Autowired
private NacosConfig config;
@Autowired
private ProviderFeignClient providerFeignClient;
@GetMapping("/index")
private String index() {
return config.isTest() + "--" + providerFeignClient.index();
}
}

7.nacos管理后台添加配置信息 配置管理->配置列表->右侧"+", 注意Data ID格式

${prefix}-${spring.profiles.active}.${file-extension}

SpringCloud怎么使用Nacos做注册中心+配置中心?_SpringCloud


8.启动provider, consumer, 可在nacos管理平台查看服务状态

SpringCloud怎么使用Nacos做注册中心+配置中心?_SpringCloud_02


9.consumer启动关键日志, consumer已获取到配置信息

2020-10-25 09:47:22.928  WARN 4032 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[consumer] & group[DEFAULT_GROUP]
2020-10-25 09:47:22.942 INFO 4032 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'consumer.yaml', group: 'DEFAULT_GROUP', data: is_test: false
2020-10-25 09:47:22.949 INFO 4032 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='consumer.yaml'}, NacosPropertySource {name='consumer'}]}
2020-10-25 09:47:22.954 INFO 4032 --- [ main] c.c.demo.consumer.ConsumerApplication : No active profile set, falling back to default profiles: default

10.访问​​http://localhost:8081/index​​, 如果将配置删除, 找不到is_test, 会变为默认值true. 测试

SpringCloud怎么使用Nacos做注册中心+配置中心?_Java_03

11.在nacos管理平台修改配置is_test值, 可以看到consumer日志变化

2020-10-25 10:01:51.091  INFO 4032 --- [-localhost_8848] o.s.c.e.event.RefreshEventListener       : Refresh keys chan