@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>
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐