Nacos概述:
全称Dynamic Naming and Configuration Service,是一个Nacos,全称Dynamic Naming and Configuration Service,是一个易于构建云原生应用的动态服务发现、配置管理和服务管理平台。其核心功能主要包括服务发现、健康检查、动态配置服务和动态DNS服务等。这些特性使得微服务的发现、管理、共享和组合更加容易。
具体来说,Nacos支持基于DNS和RPC的服务发现,可以实时检查服务的健康状态,实现动态配置服务,并提供了动态DNS服务等特性。此外,Nacos还与Spring Cloud Alibaba紧密集成,可以作为注册中心,实现服务的注册与发现。
官网:Releases · alibaba/nacos (github.com)
这里,我就用Windows版本。
解压缩即可。
当然nacos也是用java写的,我们也可以直接运行这个jar包
是不是特别像springboot的配置文件。
startup.cmd -m standalone
当然,我这个是改了端口号的,我改回去即可。
正常运行(没改的情况下)的是8848端口号。
启动成功。访问Nacoshttp://localhost:8848/nacos/#/login
默认用户名和密码都是nacos
然后发布即可
pom文件
com.alibaba.nacos
nacos-client
2.2.1
com.google.sitebricks
slf4j
0.8.3
package org.cyl;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
try {
String serverAddr="localhost:8848";
String dataId="test";
String group="DEFAULT_GROUP";
Properties properties=new Properties();
properties.put("serverAddr",serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String config = configService.getConfig(dataId, group, 5000);
System.out.println(config);
} catch (NacosException e) {
throw new RuntimeException(e);
}
}
}
package org.cyl;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;
public class Main {
public static void main(String[] args) {
try {
String serverAddr="localhost:8848";
String dataId="test";
String group="DEFAULT_GROUP";
Properties properties=new Properties();
properties.put("serverAddr",serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String config = configService.getConfig(dataId, group, 5000);
System.out.println(config);
configService.addListener(dataId, group, new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String s) {
System.out.println(s);
}
});
System.in.read();
} catch (NacosException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
之后看结果,已经监测到了:
pom文件
4.0.0
org.cyl
test07
1.0-SNAPSHOT
17
17
UTF-8
org.springframework.boot
spring-boot-starter-web
2.6.13
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2022.0.0.0-RC2
org.springframework.boot
spring-boot-starter
2.6.13
com.google.sitebricks
slf4j
0.8.3
com.alibaba.nacos
nacos-spring-context
1.1.1
MyApplication类
package org.cyl;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.cyl.service.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
@NacosPropertySource(dataId = "test",autoRefreshed = true)
public class MyApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(MyApplication.class);
MyService myService = annotationConfigApplicationContext.getBean(MyService.class);
myService.test();
}
}
MyService类
package org.cyl.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyService {
@Value("${password}")
public String username;
public void test(){
System.out.println(username);
}
}
pom文件
4.0.0
org.cyl
test07
1.0-SNAPSHOT
17
17
UTF-8
org.springframework.boot
spring-boot-starter-web
2.6.13
org.springframework.boot
spring-boot-starter
2.6.13
com.google.sitebricks
slf4j
0.8.3
com.alibaba.boot
nacos-config-spring-boot-starter
0.2.12
com.fasterxml.jackson.core
jackson-databind
2.15.3
MyApplication类
package org.cyl;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@NacosPropertySource(dataId = "test",autoRefreshed = true)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
application.properties
nacos.config.server-addr=127.0.0.1:8848
注意:如果想要全部写在properties里面应该这样写:
nacos.config.server-addr=127.0.0.1:8848
nacos.config.data-id=test
nacos.config.auto-refresh=true
nacos.config.bootstrap.enable=true
Controller
package org.cyl.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@NacosValue(value = "${password}",autoRefreshed = true)
public String password;
@GetMapping("/")
public String test(){
return password;
}
}
注意:有的版本是会直接读取bootstrap.properties里面
算了,我还是直接写一个微服务系列吧