请教一下,spring boot下,如何获取加网关代理的nacos配置?-灵析社区

ciiiiinema

基本环境: 1. 根据手册,本地搭建一个nacos server, 版本 2.2.3, standalone 2. 利用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 目前问题是: 1. 用spring boot读取不到配置 spring boot里的配置就非常简单, nacos.config.server-addr=http://localhost:9080 spring boot代码: // 版本 0.2.12 @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也不行 com.alibaba.nacos nacos-client 2.3.0 // 2.2.3, 2.2.4都试过 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, }

阅读量:26

点赞量:0

问AI
解决问题了: 1. nacos client 2.x版本问题,最直接的方式,把nacos client版本换成:1.x,如:1.4.6 2. 增加代理的端口监听,9848 基本原因是: 9848 1000 客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求 参考:"https://nacos.io/zh-cn/docs/next/v2/upgrading/2.0.0-compatibi..." (https://link.segmentfault.com/?enc=MDMhs4NLfY0ZcWNE1Y2vBg%3D%3D.9jd%2BhdcACy1q65Db7SjysI0nI63PzJTJvmLAoF1ubSyJIgSuESFECFCHLjtUCkzG9%2BLqFRzVMepaXoEanPy5gFKXx1GZTQtNe411G4llIps%3D)