SpringCloud融入Python的实现
前言
该篇文章分享如何将PythonWeb服务融入到SpringCloud微服务体系中,并调用其服务,PythonWeb框架用的是Tornado
构建Pythonweb服务
引入py-eureka-client客户端
pipinstallpy_eureka_client
manage.py
#!/usr/bin/envpython
importtornado.httpserver
importtornado.ioloop
importtornado.options
importtornado.web
importpy_eureka_client.eureka_clientaseureka_client
fromtornado.optionsimportdefine,options
fromtimeimportsleep
define("port",default=3333,help="runonthegivenport",type=int)
classIndexHandler(tornado.web.RequestHandler):
defget(self):
username=self.get_argument('username','Hello')
self.write(username+',AdministratorUser!')
defpost(self):
username=self.get_argument('username','Hello')
self.write(username+',AdministratorUser!')
classMainHandler(tornado.web.RequestHandler):
defget(self):
username=self.get_argument('username','Hello')
self.write(username+',CoisiniUser!')
defpost(self):
username=self.get_argument('username','Hello')
self.write(username+',CoisiniUser!')
defmain():
tornado.options.parse_command_line()
#注册eureka服务
eureka_client.init_registry_client(eureka_server="http://localhost:31091/eureka/,http://localhost:8761/eureka/",
app_name="python-tornado",
instance_port=3333)
app=tornado.web.Application(handlers=[(r"/test",IndexHandler),(r"/main",MainHandler)])
http_server=tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if__name__=='__main__':
main()
大致说下上述代码,向端口为31091的注册中心注册服务名为python-tornado的服务,端口为3333,提供两个请求方式为GET和POST,接口路径为/test和/main的外部调用接口
启动python服务(在此之前要创建一个Eureka服务注册中心)
pythonmanage.pyrunserver
运行结果
@RestController
publicclassTestController{
privateTestAPIClienttestAPIClient;
@Autowired
publicTestController(TestAPIClienttestAPIClient){
this.testAPIClient=testAPIClient;
}
@PostMapping("/test")
publicStringtest(@RequestParamStringusername)throwsException{
returnthis.testAPIClient.test(username);
}
@GetMapping("/test")
publicStringtest1()throwsException{
returnthis.testAPIClient.test1();
}
}
@FeignClient(name="python-tornado",configuration=FeignConfigure.class)
publicinterfaceTestAPIClient{
@PostMapping("/test")
Stringtest(@RequestParam("username")Stringusername);
@GetMapping("/test")
Stringtest1();
}
importfeign.Logger;
importfeign.codec.Encoder;
importfeign.form.spring.SpringFormEncoder;
importorg.springframework.beans.factory.ObjectFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.autoconfigure.web.HttpMessageConverters;
importorg.springframework.cloud.netflix.feign.support.SpringEncoder;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassFeignConfigure{
@Bean
Logger.LevelfeignLoggerLevel(){
returnLogger.Level.FULL;
}
@Autowired
privateObjectFactorymessageConverters;
@Bean
publicEncoderfeignFormEncoder(){
returnnewSpringFormEncoder(newSpringEncoder(messageConverters));
}
}
org.springframework.cloud spring-cloud-starter-feign io.github.openfeign.form feign-form 3.4.1 io.github.openfeign.form feign-form-spring 3.4.1
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。