使用SpringCloud Kubernetes组件获取Service

介绍

SpringCloud Kubernetes,提供了使用 Kubernetes 本地服务的 Spring Cloud 通用接口实现。目标是促进 Spring Cloud 和运行在 Kubernetes 中的 Spring Boot 应用程序的集成。

SpringCloud Kubernetes 能在 Kubernetes 中完成服务发现和配置监听功能主要依赖 Fabric8 提供的Kubernetes Client 组件,该组件是 Kubernetes Java API 的第三方客户端,它的主要功能是远程操作 Kubernetes API 完成在 Kubernetes 环境下的一系列操作。

功能:

  • 在 Kubernetes 中实现服务发现、服务名称解析功能。
  • 在 Kubernetes 中读取 ConfigMapsSecrets 的配置,当 ConfigMap 或 Secret 更改时重新加载应用程序属性。
  • 在 Kubernetes 可去掉 Kubernetes 自带的服务负载均衡,实现与 Ribbon 结合,通过 Ribbon 完成负载均衡。

权限配置

在有k8s集群的机器上把root/.kube/目录下的config文件复制一份放到windows用户下的.kube文件夹中

Maven 配置

spring boot项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<!--SpringBoot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot Actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--SpringCloud Kubernetes Discovery-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
</dependencies>

简单的Controller类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
public class ServiceController {

@Autowired
private DiscoveryClient discoveryClient;

@GetMapping("/service")
public List<String> getServiceList(){
return discoveryClient.getServices();
}

@GetMapping("/instance")
public Object getInstance(@RequestParam("name") String name){
return discoveryClient.getInstances(name);
}

}

项目参数配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
spring:
application:
name: k8sdemo
cloud:
kubernetes:
client:
namespace: default # 如果getInstance无法获取到数据 请设置此参数
discovery:
enabled: true
all-namespaces: true


server:
port: 8080

management:
server:
port: 8081
endpoints:
web:
exposure:
include: "*"


创建启动类并启用服务发现注解

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableDiscoveryClient
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

测试接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[
"cep-log-app-svc",
"cep-log-es-client-svc",
"bacth-es-app-svc",
"etcd-global",
"etcd-global-client",
"etcd-zone1",
"etcd-zone1-client",
"kubernetes",
"mytomcat-nsvc",
"myweb-svc",
"myweb-svc-endpoint",
"pg-prometheus",
"pg-prometheus-adapter",
"vtctld",
"vtgate-zone1",
"vttablet",
"kube-dns",
"monit-prometheus-operator-kubelet",
"prome-operator-prometheus-kubelet",
"test-monitor-prometheus-op-kubelet",
"tiller-deploy"
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[
{
"instanceId": "ae423b58-b1a6-11e9-88b6-fa163e532b86",
"serviceId": "pg-prometheus",
"secure": false,
"metadata": {
"app": "pg-prometheus",
"port.postgresql": "5432"
},
"host": "10.244.0.147",
"scheme": "http://",
"port": 5432,
"uri": "http://10.244.0.147:5432"
}
]

参考链接

Kubernetes 开发 SpringCloud (一)、使用SpringCloud Kubernetes组件进行服务发现(含有配置详解和源码解释)