这是一个服务发现组件,整合了Spring Cloud
,通过注解org.springframework.cloud.client.discovery.EnableDiscoveryClient
开启功能后,可以让你的应用快速获取Kubernetes
上部署的服务的实例信息。
<dependency>
<groupId>io.github.dbses</groupId>
<artifactId>cloud-discovery-starter</artifactId>
</dependency>
启动类添加注解:org.springframework.cloud.client.discovery.EnableDiscoveryClient
。
package com.github.open.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
cloud:
kubernetes:
apiServerUrl: https://127.0.0.0:5443
caCertFileUrl: ${user.dir}/cloud-discovery-example/secret/caCertFile.crt
oauth2Token: eyJhbXXX
includeNamespaces: infra, vip
package com.github.open.example.service;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ExampleService implements InitializingBean {
@Autowired
private DiscoveryClient discoveryClient;
@Override
public void afterPropertiesSet() {
System.out.println("===== Cloud Discovery Example =====");
List<ServiceInstance> instance = discoveryClient.getInstances("courier-producer");
System.out.println("instance: " + instance);
System.out.println("ip: " + instance.stream().map(ServiceInstance::getHost).collect(Collectors.toList()));
}
}