@TOC
background
Now the microservice development model is more and more widely used, and the registration center Eureka
is gradually replaced by other registration center products, such as those produced by Ali Nacos
. With the popularization and rapid development of cloud-native related technologies k8s
, we have exposed K8s
the internals Pod
to external access, and there are fewer passes Service
, which is also the protagonist today.
Did you find that the problem of registration and discovery Service
has been solved Pod
, and the load has also been realized. When we develop microservices based on cloud native, we can use the ability to Service
obtain the following Pod
list, and initiate a call by Ribbon
waiting for the client load to Pod
, and can also Service
be called directly using the load capacity. k8s
Changes to these information are maintained internally using ETCD
services. Spring
The official website also k8s
provides a set of native support sub-projects, that is Spring Cloud Kubernetes
.
Description of local development environment
development dependencies | Version |
---|---|
Spring Boot | 3.1.0 |
Spring Cloud | 2022.0.3 |
JDK | 20 |
How to develop and debug in a local non-K8s environment
In traditional microservice development, the Nacos registration center will be used. Now there is no Nacos, and the underlying layer interacts with the k8s API Server through Fabric8 locally to obtain resource information in the cluster.
main pom.xml dependency
<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>
startup class
- Use
@EnableDiscoveryClient
the service discovery registration function to enable - Use
@EnableFeignClients
enable@FeignClient
function
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);
}
}
Write a call to Feign
- If multiple microservices are deployed in the cluster, they can be called by
service
calling each other - If it is a microservice in the local tuning cluster, you can specify
url
parameters, the priority level rationame
is higher, and you can specify it as an external endpoint exposedurl
in the clusterservice
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);
}
Write a Controller
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);
}
}
bootstrap.yaml
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
k8s deployment springboot-min
Write the simplest microservice, provide a /hello interface, and deploy it to k8s. This step is directly omitted.