nacos学习笔记

1.nacos安装配置

  • 在github官网下载:https://github.com/alibaba/nacos

  • 将文件包解压

  • 在bin中使用startup文件进行启动,需要java8+环境

  • 默认以集群方式启动,单机启动命令:

1
$ ./startup.sh -m standalone
  • 配置授权token
1
2
3
4
5
6
nacos.core.auth.enabled=true
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.core.auth.server.identity.key=admin
nacos.core.auth.server.identity.value=admin123
nacos.console.ui.enabled=true

2.nacos集成springboot

  • 直接获取nacos中值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  <parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.6.5</version>
</parent>

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

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.3.3</version>
</dependency>

  • JAVA配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
String serverAddr = "192.168.10.128:8848";
String dataId = "test";
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
properties.put("username", "nacos");
properties.put("password", "nacos");
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);

//配置监听器,当该配置项被更改时促发
configService.addListener(dataId, String.valueOf(group), new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("receive:" + configInfo);
}

@Override
public Executor getExecutor() {
return null;
}
});

System.in.read();
  • 通过注解的方式注入

    添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
    <dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.12</version>
</dependency>
</dependencies>

​ 配置文件application.properties中添加连接配置

1
2
3
4
5
6
nacos.config.server-addr=192.168.10.128:8848
nacos.config.data-id=test
nacos.config.auto-refresh=true
nacos.config.bootstrap.enable=true
nacos.config.username=nacos
nacos.config.password=nacos

​ 添加springboot启动项

1
2
3
4
5
6
7
@SpringBootApplication
@NacosPropertySource(dataId = "test", autoRefreshed = true)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

​ 通过controller测试

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class UserController {
//@Value("${spring.datasource.username}") 这个配置无法实时更新,下面配置可实时更新数据 autoRefreshed = true
@NacosValue(value = "${spring.datasource.username}", autoRefreshed = true)
private String username;
@GetMapping("/test")
public String test(){
return username;
}
}

3.nacos继承springcloud

  • pom替换

    需要删除nacos-config-spring-boot-starter

  • 同时需要更改springboot的版本号,根据官方介绍,springcloud需要使用其对应的springcloud版本号,否则会出现版本冲突。

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

image-20240715211231843

​ 因此springboot的版本号需要更改为2.3.12.RELEASE

1
2
3
4
5
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
  • 使用springcloud需要删除配置文件application.properties,而使用springcloud的配置文件bootstrap.properties
  • 系统会将application的名字和dataid进行匹配拉去对应的配置
1
2
3
4
spring.application.name=test
spring.cloud.nacos.server-addr=192.168.10.128:8848
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=nacos
  • 使用springcloud的时不能使用**@NacosValue**注解来获取值而是得使用直接使用@Value来获取值,同时在controller上添加@RefreshScope注解,通过这种方式也是可以自动刷新值的。
1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RefreshScope
public class UserController {
@Value("${spring.datasource.username}")
//@NacosValue(value = "${spring.datasource.username}", autoRefreshed = true)
private String username;
@GetMapping("/test")
public String test(){
return username;
}
}

  • 也可以通过一个公共的配置CommonConfig类来专门用来加载值,而不用每次使用到@Value都需要@RefreshScope注解。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
@RefreshScope
public class CommonConfig {
@Value("${spring.datasource.username}")
private String username;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}

1
2
3
4
5
//自动注入
@Autowired
private CommonConfig config;
//获取
config.getUsername();

4.JAVA SDK服务注册

  • 引入jar包
1
2
3
4
5
6
7
8
9
10
11
      <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<!-- 或者只引入下面的jar包-->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.3.3</version>
</dependency>
  • SDK服务注册代码
1
2
3
4
5
6
7
8
9
10
// 创建包含用户名和密码的 Properties 对象
Properties properties = new Properties();
properties.put("serverAddr", "192.168.10.128:8848");
properties.put("username", "nacos");
properties.put("password", "nacos");
NamingService namingService = NamingFactory.createNamingService(properties);
// 若没有身份验证
// NamingService namingService = NamingFactory.createNamingService("192.168.10.128:8848");
namingService.registerInstance("user", "1.1.1.1", 8888);
System.in.read();
  • 注册多个实例和不同的集群
1
2
3
4
5
6
7
8
9
10
NamingService namingService = NamingFactory.createNamingService(properties);
// 若没有身份验证
// NamingService namingService = NamingFactory.createNamingService("192.168.10.128:8848");
namingService.registerInstance("user", "1.1.1.1", 8888, "bj");
//注册不同集群
NamingService namingService2 = NamingFactory.createNamingService(properties);
namingService2.registerInstance("user", "2.2.2.2", 8888,"bj");
//注册不同集群
NamingService namingService3 = NamingFactory.createNamingService(properties);
namingService3.registerInstance("user", "3.3.3.3", 8888,"sh");

image-20240715225341337

image-20240715225404664

  • 使用另一种的方式注册服务

5.JAVA SDK 服务发现

  • 服务发现代码
1
2
3
4
5
6
7
8
9
10
// 创建包含用户名和密码的 Properties 对象
Properties properties = new Properties();
properties.put("serverAddr", "192.168.10.128:8848");
properties.put("username", "nacos");
properties.put("password", "nacos");
NamingService namingService = NamingFactory.createNamingService(properties);
//true 代表 是否健康的实例
for (Instance instance : namingService.selectInstances("user", true)) {
System.out.println(instance);
}

image-20240715231259719

6.springcloud的服务注册与发现

  • 新建服务发现项目

  • 必要依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.12.RELEASE</version>
</parent>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<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-discovery</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
</dependencies>
  • 新建启动类
1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
public class MyApplication {
@Bean
@LoadBalanced //需要配置负载均衡才能发现服务
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

  • Controller请求其他服务
1
2
3
4
5
6
7
8
9
10
11
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/test")
public String echo() {
//其中provider为其他生产者服务名,即spring.application.name=provider
///id为服务接口地址
return restTemplate.getForObject("http://provider/id", String.class);
}
}

image-20240716205850817