基本环境:
- 根据手册,本地搭建一个nacos server, 版本 2.2.3, standalone
利用apisix,给nacos server加一层网关代理
{ "uri": "/nacos/*", "name": "local.8848", "upstream_id": "xx", "enable_websocket": true, "status": 1 } { "nodes": [ { "host": "host.docker.internal", "port": 8848, "weight": 100 } ], "type": "roundrobin", "pass_host": "pass", "name": "local.nacos" }
访问:
http://localhost:9080/nacos/index.html
都很正常的,也可以用curl -X GET "http://localhost:9080/nacos/v1/cs/configs?dataId=nacos.cfg.dataId"
配置了值:
namespace=public
dataId=nacos.cfg.dataId
group=DEFAULT_GROUP
value: // properties
time.pattern=yyyy-mm-dd
目前问题是:
- 用spring boot读取不到配置
spring boot里的配置就非常简单,
nacos.config.server-addr=http://localhost:9080
spring boot代码:
// 版本
<nacos-config-spring-boot.version>0.2.12</nacos-config-spring-boot.version>
@SpringBootApplication
@NacosPropertySource(dataId = "nacos.cfg.dataId", type = ConfigType.PROPERTIES, autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
@Controller
public class ConfigController {
@NacosValue(value = "${time.pattern}", autoRefreshed = true)
private String value;
@RequestMapping(value = "/config/get", method = GET)
@ResponseBody
public String get() {
return value;
}
}
直接用sdk也不行
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.3.0</version> // 2.2.3, 2.2.4都试过
</dependency>
public static void main(String[] args) throws Exception {
String serverAddr = "http://localhost:9080"; 就可以了
String dataId = "nacos.cfg.dataId";
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
// 换成 serverAddr=http://localhost:8848, content就是配置的值了
System.out.println(content); // content=null,
}
解决问题了:
基本原因是:
9848 1000 客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求
参考:https://nacos.io/zh-cn/docs/next/v2/upgrading/2.0.0-compatibi...