SpringCloud融入Python的實(shí)現(xiàn)
前言
該篇文章分享如何將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 python import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import py_eureka_client.eureka_client as eureka_client from tornado.options import define, options from time import sleep define("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
@RestController public 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; @Configuration public class FeignConfigure { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } }
Feign依賴
<!-- Feign Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.4.1</version> </dependency>
運(yùn)行結(jié)果
在這里,我們用請(qǐng)求工具Postman
來測(cè)試一下,可以看出,由TestController
調(diào)用TestAPIClient
再調(diào)用Python服務(wù),至此,已完成微服務(wù)調(diào)用Python Web服務(wù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java異常中toString()和getMessage()區(qū)別
在java異常體系中,要打印異常信息,可以通過:e.getMessage() 、 e.toString() e.printStackTrace() 等方法打印,本文主要介紹了Java異常中toString()和getMessage()區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(1)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07js中去除字符串中所有的html標(biāo)簽代碼實(shí)例
這篇文章主要介紹了js中去除字符串中所有的html標(biāo)簽代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Mybatis之foreach標(biāo)簽內(nèi)傳入list為空的問題
這篇文章主要介紹了Mybatis之foreach標(biāo)簽內(nèi)傳入list為空的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Java中ArrayList和LinkedList的遍歷與性能分析
這篇文章主要給大家介紹了ArrayList和LinkedList這兩種list的五種循環(huán)遍歷方式,各種方式的性能測(cè)試對(duì)比,根據(jù)ArrayList和LinkedList的源碼實(shí)現(xiàn)分析性能結(jié)果,總結(jié)結(jié)論。相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考價(jià)值,有需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-12-12Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(10)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07