Nacos
nacos本质上也是一个springboot 项目
aliyunECS部署单机nacos
# java环境依赖
yum install -y java-1.8.0-openjdk-devel.x86_64
# 解压
tar -zxvf nacos-server-2.3.0.tar.gz
# 单机模式启动
./startup.sh -m standalone
aliyun安全组
8848端口:web访问
9848端口:gRPC协议client访问
9849端口:服务端gRPC请求服务端端口,用于服务间同步等
nacos2.x需要多暴露两个端口
SDKCaller测试类
package com.test.nacos.SDK;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.time.LocalDateTime;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* @projectName: nacos
* @package: com.SDKCaller.nacos.SDK
* @className: SDKCaller
* @author: ludi
* @description: TODO
* @date: 2024/2/5 15:23
* @version: 1.0
*/
public class SDKCaller {
public static void main(String[] args) {
try {
// String serverAddr = "172.23.74.116:8848";
String serverAddr = "114.55.131.223:8848";
String dataId = "druidDemoDev";
// String dataId = "test1";
String group = "DEFAULT_GROUP";
String content = "testContent";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
// properties.put(PropertyKeyConst.NAMESPACE, "public");
properties.put(PropertyKeyConst.NAMESPACE, "2ce82298-746d-4566-aef5-88508bb20294");
properties.put(PropertyKeyConst.USERNAME, "nacos");
properties.put(PropertyKeyConst.PASSWORD, "");
ConfigService configService = NacosFactory.createConfigService(properties);
// create(configService, "createTesting", group, "createTesting");
String newContent = "testUpdateTesting" + LocalDateTime.now();
// update(configService, dataId, group, newContent);
pull(configService, dataId, group);
// listener(configService, dataId, group);
// ServiceInit.serviceCall();
// 阻塞主线程,保持监听状态
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (NacosException e) {
e.printStackTrace();
}
}
/**
* 创建配置
*
* @param configService
* @param dataId
* @param group
* @param content
* @throws NacosException
*/
private static void create(ConfigService configService, String dataId, String group, String content) throws NacosException {
// 发布配置
boolean publishSuccess = configService.publishConfig(dataId, group, content);
if (publishSuccess) {
System.out.println("Config published successfully.");
} else {
System.out.println("Failed to publish config.");
}
}
/**
* 更新配置
*
* @param configService
* @param dataId
* @param group
* @param newContent
* @throws NacosException
*/
private static void update(ConfigService configService, String dataId, String group, String newContent) throws NacosException {
boolean updateSuccess = configService.publishConfig(dataId, group, newContent);
if (updateSuccess) {
System.out.println("Config updated successfully.");
} else {
System.out.println("Failed to update config.");
}
}
/**
* 拉取配置
*
* @param configService
* @param dataId
* @param group
* @return
* @throws NacosException
*/
private static String pull(ConfigService configService, String dataId, String group) throws NacosException {
String content = configService.getConfig(dataId, group, 5000);
System.out.println("pull:" + content);
return content;
}
/**
* 监听配置
*
* @param configService
* @param dataId
* @param group
* @throws NacosException
*/
private static void listener(ConfigService configService, String dataId, String group) throws NacosException {
String result;
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("Config changed: " + configInfo);
}
@Override
public Executor getExecutor() {
// 如果有需要,可以返回一个自定义的Executor
return null;
}
});
}
}
nacos鉴权开启
cd /opt/nacos/conf
vi application.properties
> nacos.core.auth.enabled=true
nacos.core.auth.server.identity.key=***
nacos.core.auth.server.identity.value=***
nacos.core.auth.plugin.nacos.token.secret.key=***
Springboot整合nacos
pom.xml依赖增加,注意springboot、springcloud和nacos三者之间的版本兼容问题,如果springboot和springcloud不兼容会出现bean创建失败无法启动
spring-cloud我这边使用的springboot版本为2.7.2对应cloud为2021.0.x,在2020.0.x版本开始默认不开启bootstrap会导致bootstrap.yml加载不进来,所以这边又在pom中增加了spring-cloud-starter-bootstrap的依赖
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.0.5.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
增加注解
@SpringBootApplication
@EnableDiscoveryClient
public class DruidDemoApplication {
public static void main(String[] args) {
SpringApplication.run(DruidDemoApplication.class, args);
}
}
bootstrap.yml
spring:
application:
name: druidDemoDev #为服务名
cloud:
nacos:
username: nacos
password:
discovery:
# namespace 只识别命名空间id
namespace: 2ce82298-746d-4566-aef5-88508bb20294
group: DEFAULT_GROUP
server-addr: 114.55.131.223:8848
config:
file-extension: yaml #后缀名,只支持 properties 和 yaml 类型
prefix: druidDemoDev #文件名,如果没有配置则默认为 ${spring.appliction.name}
namespace: 2ce82298-746d-4566-aef5-88508bb20294
auto-refresh: true
enable-remote-sync-config: true #启用远程同步配置,
timeout: 3000
group: DEFAULT_GROUP #配置组
refresh-enabled: true
server-addr: 114.55.131.223:8848
server:
port: 8090