赞
踩
2023年8月17日,本人之前使用过nacos+dubbo+springboot、eureka+feign等环境。最近学习到了istio服务网格集成到k8s也可以实现分布式微服务。
/test1?name=hello
实现了grpc调用生产者微服务本地测试成功后,部署k8s
生成各个服务jar包,上传服务器
构建各个服务docker镜像
创建各个微服务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: config8081
port: 8081
targetPort: 8081
- name: gserver18082
port: 18082
targetPort: 18082
- name: gclient8083
port: 8083
targetPort: 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-base http: route: - destination: host: web-base subset: dd11 headers: response: set: ssxppp: ffffffyyyyyyy-internall --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: web-base-dr spec: host: web-base subsets: - name: dd11 labels: 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.cn gateways: - grpcdemo-gateway http: route: - destination: host: web-base subset: dd11 port: number: 8083 headers: response: set: ssxppp: ffffffyyyyyyy-externall --- apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: grpcdemo-gateway spec: selector: istio: ingressgateway # use Istio default gateway implementation servers: - port: number: 80 name: http protocol: HTTP hosts: - "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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。