python服务或者springCloud使用注册中心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创建的微服务),application.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创建的微服务),application.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基于springBoot2.0.2
4、py-eureka-client学习笔记
5、python-eureka-client

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值