开发中间件笔记nacos学习笔记
Takake1.nacos安装配置
1
| $ ./startup.sh -m standalone
|
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
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>
|
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 { @NacosValue(value = "${spring.datasource.username}", autoRefreshed = true) private String username; @GetMapping("/test") public String test(){ return username; } }
|
3.nacos继承springcloud
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
因此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}") 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服务注册
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>
<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>2.3.3</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10
| 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.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.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");
|
5.JAVA SDK 服务发现
1 2 3 4 5 6 7 8 9 10
| 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); for (Instance instance : namingService.selectInstances("user", true)) { System.out.println(instance); }
|
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); } }
|
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() { return restTemplate.getForObject("http://provider/id", String.class); } }
|