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 依赖

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
  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 的接口

相关推荐
蝎子莱莱爱打怪13 小时前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
stark张宇19 小时前
微服务架构必备:Gin + gRPC + Consul + Nacos + GORM 打造用户服务
微服务·gin·grpc
蝎子莱莱爱打怪4 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀4 天前
Docker部署Nacos
docker·容器
缓解AI焦虑4 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
阿里云云原生4 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生4 天前
阿里云微服务引擎 MSE 及 API 网关 2026 年 1 月产品动态
微服务
1candobetter5 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
阿里云云原生5 天前
Kubernetes 官方再出公告,强调立即迁移 Ingress NGINX
kubernetes
至此流年莫相忘5 天前
Kubernetes实战篇之配置与存储
云原生·容器·kubernetes