這篇文章將為大家詳細(xì)講解有關(guān)如何進(jìn)行SpringCloud融入Python的實(shí)現(xiàn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)建站成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:028-86922220前言
該篇文章分享如何將Python Web
服務(wù)融入到Spring Cloud
微服務(wù)體系中,并調(diào)用其服務(wù),Python Web
框架用的是Tornado
構(gòu)建Python web服務(wù)
引入py-eureka-client客戶端
pip install py_eureka_client
manage.py
#!/usr/bin/env pythonimport tornado.httpserverimport tornado.ioloopimport tornado.optionsimport tornado.webimport py_eureka_client.eureka_client as eureka_clientfrom tornado.options import define, optionsfrom time import sleepdefine("port", default=3333, help="run on the given port", type=int)class IndexHandler(tornado.web.RequestHandler): def get(self): username = self.get_argument('username', 'Hello') self.write(username + ', Administrator User!') def post(self): username = self.get_argument('username', 'Hello') self.write(username + ', Administrator User!')class MainHandler(tornado.web.RequestHandler): def get(self): username = self.get_argument('username', 'Hello') self.write(username + ', Coisini User!') def post(self): username = self.get_argument('username', 'Hello') self.write(username + ', Coisini User!')def main(): tornado.options.parse_command_line() # 注冊(cè)eureka服務(wù) 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
的注冊(cè)中心注冊(cè)服務(wù)名為python-tornado
的服務(wù),端口為3333
,提供兩個(gè)請(qǐng)求方式為GET
和POST
,接口路徑為/test
和/main
的外部調(diào)用接口
啟動(dòng)python服務(wù)(在此之前要?jiǎng)?chuàng)建一個(gè)Eureka服務(wù)注冊(cè)中心)
python manage.py runserver
運(yùn)行結(jié)果
服務(wù)調(diào)用(Feign) - Feign用于便捷調(diào)用HTTP API
congfig
類中需添加注解@EnableFeignClients
,具體使用請(qǐng)百度
TestController.java
@RestControllerpublic class TestController { private TestAPIClient testAPIClient; @Autowired public TestController(TestAPIClient testAPIClient) { this.testAPIClient = testAPIClient; } @PostMapping("/test") public String test(@RequestParam String username) throws Exception { return this.testAPIClient.test(username); } @GetMapping("/test") public String test1() throws Exception { return this.testAPIClient.test1(); }}
TestAPIClient.java
@FeignClient(name="python-tornado", configuration = FeignConfigure.class)public interface TestAPIClient { @PostMapping("/test") String test(@RequestParam("username") String username); @GetMapping("/test") String test1();}
FeignConfigure.java
import feign.Logger;import feign.codec.Encoder;import feign.form.spring.SpringFormEncoder;import org.springframework.beans.factory.ObjectFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.web.HttpMessageConverters;import org.springframework.cloud.netflix.feign.support.SpringEncoder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class FeignConfigure { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } @Autowired private ObjectFactory
Feign依賴
運(yùn)行結(jié)果
在這里,我們用請(qǐng)求工具Postman
來測(cè)試一下,可以看出,由TestController
調(diào)用TestAPIClient
再調(diào)用Python服務(wù),至此,已完成微服務(wù)調(diào)用Python Web服務(wù)
關(guān)于如何進(jìn)行SpringCloud融入Python的實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。