springcloud nacos 根据服务名获取服务的ip端口
@Autowiredprivate NacosServiceManager nacosServiceManager;@Autowiredprivate NacosDiscoveryProperties nacosDiscoveryProperties;public String getGatewayAddress() {String res = null;try {NamingService na
·
@Autowired
private NacosServiceManager nacosServiceManager;
@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties;
public String getGatewayAddress() {
String res = null;
try {
NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
Instance instance = namingService.selectOneHealthyInstance("DBApi-cluster-gateway");
res = instance.getIp() + ":" + instance.getPort();
return res;
} catch (NacosException e) {
e.printStackTrace();
return null;
}
}
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
更多推荐
已为社区贡献1条内容
所有评论(0)