首页 > 其他分享 >Fastapi之微服务Consul应用注册发现

Fastapi之微服务Consul应用注册发现

时间:2023-04-28 14:34:12浏览次数:62  
标签:name service Fastapi Consul 之微 port print server id

import uvicorn
from fastapi import FastAPI


app = FastAPI()


def register(server_name, ip, port):
    c = consul.Consul(host="127.0.0.1", port=8500)  # consul服务器信息
    print(f"开始注册服务{server_name}")
    check = consul.Check.tcp(ip, port, "10s")  # 健康检查的ip,端口,检查间隔时间
    result= c.agent.service.register(
        name=server_name,  # 应用名称 
        service_id="0d4b3555d221441bef23a35f3984532f",  # 应用唯一标识,应用异常注销时需要用到
        address=ip,  # 应用IP              
        port=port,  # 应用地址
        check=check,  # 应用服务信息
        timeout=60  # 注册超时时间
    )
    if result:
        print(f"注册服务{server_name}成功")
    else:
        print(f"注册服务{server_name}失败")


def unregister(service_id):
    c = consul.Consul()
    print(f"开始退出服务{service_id}")
    result = c.agent.service.deregister(service_id=service_id)
    if result:
        print(f"注销服务{service_id}成功")
    else:
        print(f"注销服务{service_id}失败")


@app.on_event("startup")
async def startup_event():
    register("user-service", "127.0.0.1", 8000)


@app.on_event("shutdown")
async def shutdown_event():
    unregister("0d4b3555d221441bef23a35f3984532f")



if __name__ == '__main__':
    uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True,
                debug=True, workers=1)

标签:name,service,Fastapi,Consul,之微,port,print,server,id
From: https://www.cnblogs.com/puffer/p/16428139.html

相关文章

  • Fastapi之OAuth2认证
    1.基于用户名密码认证fromtypingimportOptionalfromfastapiimportAPIRouter,Depends,HTTPExceptionfromfastapi.securityimportOAuth2PasswordBearer,OAuth2PasswordRequestFormfrompydanticimportBaseModelfromstarletteimportstatustokens=APIRout......
  • (完结篇)Python web框架FastAPI——一个比Flask和Tornada更高性能的API 框架
    今日鸡汤借问酒家何处有,牧童遥指杏花村。0前言    前几天给大家分别分享了(入门篇)简析Pythonweb框架FastAPI——一个比Flask和Tornada更高性能的API框架和(进阶篇)Pythonweb框架FastAPI——一个比Flask和Tornada更高性能的API框架。今天欢迎大家来到FastAPI系列分享的完结篇......
  • ocelot 注入consul 无法匹配路由的问题可能性
    ocelot注入consul无法匹配路由的问题可能性最近在搭建微服务,在服务的注册、发现和api的统一管理绕不开 ocelot 和 consul,在没有注入consul时,基础的配置文件能正常转发,但是注入consul后服务没有按照规定的上游路径来访问的情况(但是按照http://ip:port/consulName/api/user......
  • FastAPI.2
    目录FastAPI.2一、简单的编写基于fastapi的接口二、请求路径FastAPI.2一、简单的编写基于fastapi的接口创建main.py文件导入fastapifromfastapiimportFastAPI实例化出FastAPI的对象app=FastAPI()通过装饰器添加路径,@app.get("/")'''@app.get("/")的作......
  • Kylin系统微服务consul安装
    Kylin系统微服务consul安装  什么是Consul?Consul是一个用golang开发的分布式高可用服务治理与与服务配置的工具,它有以下功能:服务发现:Consul客户端可以提供服务,其他客户端可以使用服务名称通过DNS以及http等协议来发现服务,有助于提高服务的可扩展性。健康检查:Consul的客......
  • FastAPI.1
    FastAPI.1一、介绍主要特点快速高效编码更少bug智能:编辑器的支持,自动补全功能强大,减少调试时间。简单:易于学习和使用剪短:代码重复最小化,通过不同参数声明实现丰富的功能。简装:生产可用级别的代码,还有自动生成的交互式文档。标准化:基于(并完全兼容)API的相关开放标准:Open......
  • Ubuntu部署FastApi项目
    环境介绍系统:Ubuntu22.04Pyhton版本:3.8.10Fastapi版本:0.95.0Gunicorn版本:20.1.0准备工作1.ssh连接工具(本例使用基于Windows的Linux子系统中的ssh工具)2.配置nginx代理服务器3.配置GunicornWSGIHTTP服务器一、SSH连接Ubuntu服务器sshusername@hostusername......
  • 注册中心对比: zookeeper&&etcd&&consul
    1.注册中心概念注册中心主要有三种角色:服务提供者(RPCServer):在启动时,向Registry注册自身服务,并向Registry定期发送心跳汇报存活状态。服务消费者(RPCClient):在启动时,向Registry订阅服务,把Registry返回的服务节点列表缓存在本地内存中,并与RPCSever建立连接。服务注册......
  • consul 常用命令
    使用帮助Usage:consul[--version][--help]<command>[<args>]Availablecommandsare:aclInteractwithConsul'sACLsagentRunsaConsulagentcatalogInteractwiththecatalogconfig......
  • Ocelot 结成 Consul 做配置以及服务发现出现的一些小问题
    首先创建web项目dotnetnewweb-nApiGateway然后安装对应的nuget包dotnetaddpackageOcelotdotnetaddpackageOcelot.Provider.ConsulProgram配置usingOcelot.Middleware;usingOcelot.DependencyInjection;usingOcelot.Provider.Consul;varbuilder=W......