2023年8月17日,本人之前使用過nacos+dubbo+springboot、eureka+feign等環(huán)境。最近學(xué)習(xí)到了istio服務(wù)網(wǎng)格集成到k8s也可以實(shí)現(xiàn)分布式微服務(wù)。
1. 環(huán)境
- Kubernetes集群
- istio集成到k8s
- jdk17 (8也ok)
- gPRC服務(wù)間通信
2. 微服務(wù)
- cloud-config服務(wù)是spring-cloud-config-server配置中心,用于加載遠(yuǎn)程配置文件,該服務(wù)端口號(hào)8081
- boot-rpc-service服務(wù)是生產(chǎn)者微服務(wù),開放的gRPC端口18082
- boot-grpc-client服務(wù)是消費(fèi)者微服務(wù),開放的http端口是8083,開放接口
/test1?name=hello
實(shí)現(xiàn)了grpc調(diào)用生產(chǎn)者微服務(wù) - 公共服務(wù)boot-grpc-api是生成的grpc proto信息
3. 部署到k8s
本地測試成功后,部署k8s
-
生成各個(gè)服務(wù)jar包,上傳服務(wù)器
- cloud-config.jar
- boot-rpc-service.jar
- boot-grpc-client.jar
-
構(gòu)建各個(gè)服務(wù)docker鏡像
- istio-demo-spring-configserver:v3
- istio-demo-spring-gprc-server:v3
- istio-demo-spring-gprc-client:v3
-
創(chuàng)建各個(gè)微服務(wù)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
-
創(chuàng)建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的功能,先驗(yàn)證一下服務(wù)是否正常啟動(dòng)
curl -I "http://web-base:8083/test1?name=hello"
結(jié)果;
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 -
創(chuàng)建istio虛擬服務(wù)用于內(nèi)部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的功能,先驗(yàn)證一下內(nèi)部服務(wù)是否添加了響應(yīng)header信息
curl -I "http://web-base:8083/test1?name=hello"
結(jié)果;
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 -
創(chuàng)建istio網(wǎng)關(guān)和虛擬服務(wù)用于外部交互
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"
-
驗(yàn)證外部訪問是否成功
curl -I -HHost:grpcdemo.xxx.cn "http://192.168.0.101:32318/test1?name=hello"
(端口號(hào)是ingressgateway服務(wù)的nodeport)
結(jié)果;
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文章來源:http://www.zghlxwxcb.cn/news/detail-662639.html
源碼
https://github.com/shenshuxin01/grpc-springboot/tree/dev-istio文章來源地址http://www.zghlxwxcb.cn/news/detail-662639.html
到了這里,關(guān)于springboot+grpc+k8s+istio環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!