pythonサービスはconsul(eureka)を使用してサービス登録と発見を行う


Springcloud+springcloud gateway+consul(eureka)マイクロサービスプラットフォームの構築
1、springcloud+spring Gateway(ゲートウェイ)+consul(登録センター)マイクロサービスプラットフォームを構築する
pythonサービスはconsul登録センターに登録されています
#pip install python-consul
import consul
from config.readConfig import ReadConfig


class ConsulClient(object):
    '''
        python         
    '''

    def __init__(self):
        '''   ,  consul   '''

        # consul     port ip
        self.consul_host = ReadConfig().get('consul', 'consul_host')
        self.consul_port = ReadConfig().get('consul', 'consul_port')

        #   voiceDataAnalysis   ip port
        self.voiceData_host = ReadConfig().get('voiceDataAnalysis', 'server_host')
        self.voiceData_port = ReadConfig().get('voiceDataAnalysis', 'server_port')

        self._consul = consul.Consul(self.consul_host, self.consul_port)


    def RegisterService(self, service_name, service_id, host, port, tags=None):
        '''
                
        :param service_name:
        :param service_id:
        :param host:
        :param port:
        :param tags:
        :return:
        '''

        tags = tags or []
        #     
        self._consul.agent.service.register(
            service_name,
            service_id,
            host,
            port,
            tags,
            #       :    ip/  ,    :5s,    :30s,    :30s
            check=consul.Check.tcp(host, port, "5s", "15s", "30s"))
        print(f"=============    {service_name}  ================")


    def GetService(self, service_id):
        '''
                id         
        :param service_id:
        :return:
        '''

        services = self._consul.agent.services()
        service = services.get(service_id)
        if not service:
            return None, None
        # addr = "{0}:{1}".format(service['Address'], service['Port'])
        return service


    def Unregister(self, service_id):
        '''
                
        :param service_id:
        :return:
        '''

        self._consul.agent.service.deregister(service_id)
        self._consul.agent.check.deregister(service_id)
        print(f"===============      {service_id}====================")


    def serve(self):
        try:
            consul_client = ConsulClient()
            consul_client.RegisterService("python-service", "python-service-"+self.voiceData_port,
                                          self.voiceData_host,int(self.voiceData_port), ['secure=false'])
            # res = consul_client.GetService("python-service")
            # raise Exception("===============  python-service  ===============")
        except Exception as  e:
            consul_client.Unregister("python-service-"+self.voiceData_port)
            

Springcloud consul-serviceサービスの作成
Springcloudはconsul-serviceサービス(pythonで作成したマイクロサービスを使用してサードパーティ言語を監視するためのサービス)を作成し、アプリケーション.ymlでconsulサービスを構成します.
spring:
  application:
    name: consul-service #   
    
    #     consul  
    consul:
      #       consul
      host: 127.0.0.1 #consul    ip
      port: 8500 #consul    port
      discovery:
        service-name: ${spring.application.name} #      
        hostname: 127.0.0.1 #        
        #            ,    
        health-check-critical-timeout: 30s

2、springcloud+spring Gateway(ゲートウェイ)+eureka(登録センター)マイクロサービスプラットフォームを構築する
pythonサービスeureka登録センターへの登録

from django.http import JsonResponse
from django.views.decorators.http import require_GET


class PySidecarView:
    '''
          python         ,
           springcloud-Sidecar       ,      down ,
        Sidecar                       
    '''

    @require_GET
    def health(request):
        '''
            health     Sidecar      ,   Sidecar         
        :return:
        '''
        result = {'status': 'UP'}
        return JsonResponse(result, safe=False)

Springcloud sidecar-serviceサービスの作成
Springcloudはpython-sidecar-serviceサービス(サードパーティ言語を監視するためのサービス、ここではpythonで作成したマイクロサービス)を作成し、アプリケーション.ymlでsidecarサービスを構成します.
#sidecar  
server:
  port: 8001 #sidecar    


spring:
  application:
    name: python-sidecar-service #sidecar     

# sidecar  
sidecar:
  health-uri: http://${sidecar.hostname}:${sidecar.port}/health  #          ,            
  hostname: 127.0.0.1 #       ip,     localhost 127.0.0.1
  port: 8000 #         

#    
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000
ribbon:
  ConnectTimeout: 5000
  ReadTimeout: 5000

#        
eureka:
  instance:
    ip-address: 127.0.0.1 #        ip  
    prefer-ip-address: true  #      ip  ,   false
    instance-id: ${spring.cloud.client.ip-address}:${server.port}  #        ip+  
  client:
    service-url:
      defaultZone: http://eureka  :eureka  @127.0.0.1:8889/eureka/ #Eureka     

参考ブログ:
1、Spring Cloud Consul 2、python consulによるサービス登録と発見3、springCloud統合python springBoot 2.0.2 4、py-eureka-client学習ノート5、python-eureka-client