k8s集群搭建(七)-------- 微服务间的调用

目标

本文我们将实现两个微服务之间的调用。在 k8s 集群中,服务之间可以通过 服务名+端口号 的方式互相访问,例如 http://cloud-demo-service-a/service-a/hello。

构建服务B

以之前的文章 《k8s集群搭建(五)-------- 创建一个微服务》中的 cloud-demo-service-a 为基础,快速复制一个 cloud-demo-service-b 出来,并且将 application.properties 中的 spring.application.name 值修改为 cloud-demo-service-b,这样我们调用 /hello 接口就能拿到如下输出:

hello, this is 【cloud-demo-service-b】, custom property is 【default-value】

并且参照那篇文章,部署 cloud-demo-service-b 到 k8s 中,需要注意的,不执行映射路由操作,我们保持 cloud-demo-service-b 为一个内部应用,无法被外部访问,并且微服务之间的调用使用 http,所以我们需要把用于部署的 yml 文件中的对外暴露端口修改为 80

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloud-demo-service-b
  namespace: cloud-services
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cloud-demo-service-b
  template:
    metadata:
      labels:
        app: cloud-demo-service-b
    spec:
      containers:
      - name: cloud-demo-service-b
        image: walli.nexus.repo/cloud-demo-service-b:0.0.1
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: cloud-demo-service-b
  namespace: cloud-services
spec:
  selector:
    app: cloud-demo-service-b
  ports:
    - protocol: TCP
      port: 80 # =====> 此处修改为 80 端口
      targetPort: 8080
  type: ClusterIP

修改服务A

我们在 cloud-demo-service-a 中新增一个接口,/hello-b,这个接口将调用 cloud-demo-service-b 的 /hello 获取返回值并且输出。微服务之间的调用,我们使用 webflux 来完成。

  1. 引入 webflux 依赖

    org.springframework.boot spring-boot-starter-webflux
  2. 编写 ServiceB 的调用类

    package com.walli.cloud_demo_service_a;

    import java.time.Duration;

    import org.springframework.stereotype.Service;
    import org.springframework.web.reactive.function.client.WebClient;

    import reactor.core.publisher.Mono;

    @Service
    public class ServiceBClient {

    复制代码
     private final WebClient webClient;
    
     public ServiceBClient(WebClient.Builder webClientBuilder) {
         this.webClient = webClientBuilder.baseUrl("http://cloud-demo-service-b").build();
     }
    
     public Mono<String> getHelloB() {
         return webClient.get()
                 .uri("/service-b/hello")
                 .retrieve()
                 .bodyToMono(String.class)
                 .timeout(Duration.ofSeconds(5)) // 超时配置
                 .onErrorResume(throwable -> {
                     // 错误处理
                     return Mono.just("error get hello b");
                 });
     }

    }

  3. 新增接口 /hello-b

    package com.walli.cloud_demo_service_a;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RequestMapping("/service-a")
    public class HelloController {

    复制代码
     @Value("${spring.application.name}")
     private String serviceName;
    
     @Value("${custom.property}")
     private String customProperty;
    
     @Autowired
     private ServiceBClient serviceBClient;
    
     @GetMapping("/hello")
     public String hello() {
         return "hello, this is 【" + serviceName + "】, custom property is 【" + customProperty + "】";
     }
    
     @GetMapping("/hello-b")
     public String helloB() {
         return serviceBClient.getHelloB().block();
     }

    }

  4. 重新构建 cloud-demo-service-a 的镜像,并推送私有仓库。

  5. 更新 k8s 中的 cloud-demo-service-a 应用,注意,如果你修改了 cloud-demo-service-a 的版本号,那么请将对应 yml 部署文件中的镜像版本号更新一致;如果你没有更新版本号,为了防止部署时使用了本地缓存的旧镜像, 可以在 yml 中增加强制拉取镜像。

    。。。
    containers:
    - name: cloud-demo-service-a
    image: wx.ankoninc.com.cn/cloud-demo-service-a:0.0.1 # 镜像地址
    imagePullPolicy: Always # ====> 强制拉取镜像
    。。。

先使用指令 kubectl delete -f deply-demo-service-a.yaml删除 ServiceA

再使用指令 kubectl apply -f deply-demo-service-a.yaml重新创建它

验证

所有微服务部署完成之后,我们访问 https://k8s.test.com/cloud-demo-service-a/service-a/hello-b ,可以看到接口有如下返回,证明了 cloud-demo-service-a 成功调用了 cloud-demo-service-b 的接口

相关推荐
开发者联盟league1 小时前
使用k8s安装Sonarqube
云原生·容器·kubernetes
阿狸猿3 小时前
论微服务架构及其应用
java·微服务·架构
m0_738120724 小时前
渗透测试基础——基于Docker的Rsync服务靶场搭建与原理讲解
运维·服务器·网络·安全·web安全·docker·容器
兮山与4 小时前
SpringCloud1.0
微服务
松岩5 小时前
网络问题导致 Pod Pending
kubernetes·aiops
极客先躯9 小时前
高级java每日一道面试题-2026年02月09日-实战篇[Docker]-Docker 容器有哪些安全风险?如何缓解?
java·运维·网络·安全·docker·容器
yuanzhengme9 小时前
Ollama【部署 07】搭建本地智能体的简单说明(局域网离线部署Ollama+模型迁移+Docker部署AnythingLLM)
运维·docker·容器·大模型·ollama·本地智能体
运维老郭9 小时前
Kubernetes 二进制部署完全指南:从零搭建生产级HA集群
运维·云原生·kubernetes
成为你的宁宁10 小时前
【K8S黑盒监控实践:Probe配置、Prometheus验证与Grafana可视化】
kubernetes·grafana·prometheus
成为你的宁宁10 小时前
【Prometheus Operator监控K8S Nginx】
nginx·kubernetes·prometheus