2023年8月17日,本人之前使用过nacos+dubbo+springboot、eureka+feign等环境。最近学习到了istio服务网格集成到k8s也可以实现分布式微服务。
1. 环境
- Kubernetes集群
- istio集成到k8s
- jdk17 (8也ok)
- gPRC服务间通信
2. 微服务
- cloud-config服务是spring-cloud-config-server配置中心,用于加载远程配置文件,该服务端口号8081
- boot-rpc-service服务是生产者微服务,开放的gRPC端口18082
- boot-grpc-client服务是消费者微服务,开放的http端口是8083,开放接口
/test1?name=hello
实现了grpc调用生产者微服务 - 公共服务boot-grpc-api是生成的grpc proto信息
3. 部署到k8s
本地测试成功后,部署k8s
-
生成各个服务jar包,上传服务器
- cloud-config.jar
- boot-rpc-service.jar
- boot-grpc-client.jar
-
构建各个服务docker镜像
- istio-demo-spring-configserver:v3
- istio-demo-spring-gprc-server:v3
- istio-demo-spring-gprc-client:v3
-
创建各个微服务deployment资源
- image: 'istio-demo-spring-configserver:v3'ports:- containerPort: 8081
- image: 'istio-demo-spring-gprc-server:v3'ports:- containerPort: 18082
- image: 'istio-demo-spring-gprc-client:v3'ports:- containerPort: 8083
-
创建service资源
kind: Service metadata:name: web-base spec:ports:- name: config8081port: 8081targetPort: 8081- name: gserver18082port: 18082targetPort: 18082- name: gclient8083port: 8083targetPort: 8083
-
到此还没使用istio的功能,先验证一下服务是否正常启动
curl -I "http://web-base:8083/test1?name=hello"
结果;
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 14
date: Thu, 17 Aug 2023 02:40:23 GMT
x-envoy-upstream-service-time: 32
server: envoy -
创建istio虚拟服务用于内部grpc通信
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: web-base-internal-vs spec:hosts:- web-basehttp:route:- destination:host: web-basesubset: dd11headers:response:set:ssxppp: ffffffyyyyyyy-internall --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata:name: web-base-dr spec:host: web-basesubsets:- name: dd11labels:version: vv11
-
使用istio的功能,先验证一下内部服务是否添加了响应header信息
curl -I "http://web-base:8083/test1?name=hello"
结果;
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 14
date: Thu, 17 Aug 2023 02:40:23 GMT
x-envoy-upstream-service-time: 32
server: envoy
ssxppp: ffffffyyyyyyy-internall -
创建istio网关和虚拟服务用于外部交互
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: web-base-external-vs spec:hosts:- grpcdemo.xxx.cngateways:- grpcdemo-gatewayhttp:route:- destination:host: web-basesubset: dd11port:number: 8083headers:response:set:ssxppp: ffffffyyyyyyy-externall --- apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata:name: grpcdemo-gateway spec:selector:istio: ingressgateway # use Istio default gateway implementationservers:- port:number: 80name: httpprotocol: HTTPhosts:- "grpcdemo.xxx.cn"
-
验证外部访问是否成功
curl -I -HHost:grpcdemo.xxx.cn "http://192.168.0.101:32318/test1?name=hello"
(端口号是ingressgateway服务的nodeport)
结果;
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 14
date: Thu, 17 Aug 2023 02:40:23 GMT
x-envoy-upstream-service-time: 32
server: envoy
ssxppp: ffffffyyyyyyy-extenall
源码
https://github.com/shenshuxin01/grpc-springboot/tree/dev-istio