当前位置:网站首页>Fastapi application joins Nacos

Fastapi application joins Nacos

2022-07-23 14:53:00 Hengyunke

Recently, there is a project that needs to work with sap Communications   Adopted web service,

To facilitate communication   To give up java Request , Direct use Fastapi  Realization

import nacos
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from suds.client import Client
from suds.transport.https import HttpAuthenticated

client=nacos.NacosClient('172.16.244.200:8848',namespace='8b6f08b7-b09c-4181-bcac-1505fba5e1aa')

async def beat():
    client.add_naming_instance('fastapi-service','172.16.244.10',8000,group_name='dev')
    
 
#  Microservice registration nacos
def register_nacos():
    client.add_naming_instance('fastapi-service','172.16.244.10',8000,group_name='dev')

 
app=FastAPI()
 
#  Microservice registration 
register_nacos()

 
@app.on_event('startup')
def init_scheduler():
 
    scheduler = AsyncIOScheduler(timezone="Asia/Shanghai")
    scheduler.add_job(beat, 'interval', seconds=5)
    scheduler.start()

@app.get('/sap/materials')
async def sap_materials():
   '''  Function realization '''
  return JSONResponse({'code':1000,'msg':'succ','data':data},status_code=200)

Some points to pay attention to

group_name To work with others java The micro service configuration should be consistent   Incorrect writing spring cloud gateway  Tips 404 error

timezone='Asia/Shangai'  The time zone will be prompted when starting without utc

  The results

  In terms of efficiency fastapi From development to deployment, it took 1 Days to solve the problem

原网站

版权声明
本文为[Hengyunke]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230925037391.html