赞
踩
@TOC
现在微服务开发模式应用的越来越广泛,注册中心Eureka
也逐渐被其它注册中心产品替代,比如阿里出品的Nacos
。随着云原生相关技术的普及,k8s
迅猛发展,我们把K8s
中的Pod
暴露给外部访问,通过少了Service
,这也是今天的主角。
有没有发现,其实Service
已经解决了Pod
的注册与发现的问题,并且也实现了负载,我们在基于云原生开发微服务的时候,可以利用Service
的能力,获取后面的Pod
列表,通过Ribbon
等客户端负载对Pod
发起调用,也可以直接利用Service
的负载能力进行调用。k8s
内部会使用ETCD
服务维护这些信息的变化。Spring
官网也为k8s
提供了一套原生的支持子项目,那就是Spring Cloud Kubernetes
。
开发依赖 | 版本 |
---|---|
Spring Boot | 3.1.0 |
Spring Cloud | 2022.0.3 |
JDK | 20 |
在传统的微服务开发中,会借助Nacos注册中心,现在没有Nacos了,本地通过Fabric8,底层与k8s的API Server进行交互,获取集群内的资源信息
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-fabric8-all</artifactId>
</dependency>
</dependencies>
@EnableDiscoveryClient
开启服务发现注册功能@EnableFeignClients
启用@FeignClient
功能package com.wen3.springcloudk8s.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemoSpringCloudKubernetesApplication {
public static void main(String[] args) {
SpringApplication.run(DemoSpringCloudKubernetesApplication.class, args);
}
}
service
相互调用url
参数,优级级比name
要高,url
可以指定为集群内service
暴露的外部端点package com.wen3.springcloudk8s.demo.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@FeignClient(name = "springboot-min", url = "${springboot-min.url:}")
public interface SpringMinFeignClient {
@RequestMapping(path = "/hello")
String hello(@RequestBody(required = false) Map<String,Object> bodyMap);
}
package com.wen3.springcloudk8s.demo.controller;
import com.wen3.springcloudk8s.demo.feign.SpringMinFeignClient;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.ApiextensionsAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
@RequestMapping(path = "/k8s")
@RestController
public class K8sController {
@Resource
private DiscoveryClient discoveryClient;
@Resource
private SpringMinFeignClient springMinFeignClient;
@Resource
private KubernetesClientProperties kubernetesClientProperties;
@GetMapping("/services")
public List<String> getServices() {
return discoveryClient.getServices();
}
@RequestMapping(path = "/hello")
public String hello(@RequestBody(required = false) Map<String,Object> bodyMap) {
return springMinFeignClient.hello(bodyMap);
}
}
debug: true
logging:
level:
root: debug
spring:
application:
name: spring-cloud-k8s-demo
cloud:
kubernetes:
client:
namespace: demo
master-url: https://k8s-cluster-ip:6443
trust-certs: true
oauth-token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
discovery:
enabled: true
# all-namespaces为true,代表在整个集群范围查找资源,可能没有权限
all-namespaces: false
namespaces:
- cop
reload:
enabled: true
mode: event
# mode: polling
# period: 5000
loadbalancer:
enabled: true
mode: service
clusterDomain: cluster.local
portName: rest
springboot-min:
url: http://10.79.193.64:8880
写一个最简单的微服务,提供一个/hello接口,部署到k8s,这一步直接省略。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。