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

  1. 生成各个服务jar包,上传服务器

    • cloud-config.jar
    • boot-rpc-service.jar
    • boot-grpc-client.jar
  2. 构建各个服务docker镜像

    • istio-demo-spring-configserver:v3
    • istio-demo-spring-gprc-server:v3
    • istio-demo-spring-gprc-client:v3
  3. 创建各个微服务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
    
  4. 创建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
    
  5. 到此还没使用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

  6. 创建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
    
  7. 使用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

  8. 创建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"
    
  9. 验证外部访问是否成功
    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

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐