目标
本文我们将实现两个微服务之间的调用。在 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 的接口
![]()
1036

被折叠的 条评论
为什么被折叠?



