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 的接口

相关推荐
赵文宇(温玉)2 小时前
不翻墙,基于Rancher极速启动Kubernetes,配置SSO登录,在线环境开放学习体验
学习·kubernetes·rancher
炸裂狸花猫2 小时前
开源域名证书工具 - cert-manager
云原生·容器·kubernetes·开源·cert-manager
会飞的小蛮猪3 小时前
Ubuntu24.04基于Docker部署K8s(使用私服部署)
经验分享·docker·云原生·容器·kubernetes
阿拉斯攀登4 小时前
Spring Cloud Gateway 的内置路由过滤器使用
spring cloud·微服务·gateway
无心水4 小时前
【分布式利器:限流】3、微服务分布式限流:Sentinel集群限流+Resilience4j使用教程
分布式·微服务·架构·sentinel·分布式限流·resilience4j·分布式利器
一起学开源5 小时前
分布式基石:CAP定理与ACID的取舍艺术
分布式·微服务·架构·流程图·软件工程
h***01548 小时前
Docker启动安装nacos(详情讲解,全网最细)
运维·docker·容器
TracyCoder1239 小时前
微服务概念理解学习笔记
学习·微服务·架构
人工智能训练9 小时前
windows系统中的docker,xinference直接运行在容器目录和持载在宿主机目录中的区别
linux·服务器·人工智能·windows·ubuntu·docker·容器
谷隐凡二9 小时前
docker的简单介绍
docker·容器·eureka