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()
# 注冊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的注冊中心注冊服務(wù)名為python-tornado的服務(wù),端口為3333,提供兩個請求方式為GET和POST,接口路徑為/test和/main的外部調(diào)用接口
啟動python服務(wù)(在此之前要創(chuàng)建一個Eureka服務(wù)注冊中心)
python manage.py runserver
運(yùn)行結(jié)果
服務(wù)調(diào)用(Feign) - Feign用于便捷調(diào)用HTTP API
congfig類中需添加注解@EnableFeignClients,具體使用請百度
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é)果
在這里,我們用請求工具Postman來測試一下,可以看出,由TestController調(diào)用TestAPIClient再調(diào)用Python服務(wù),至此,已完成微服務(wù)調(diào)用Python Web服務(wù)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Idea創(chuàng)建多模塊maven聚合項(xiàng)目的實(shí)現(xiàn)
欄 目:Java
下一篇:Java內(nèi)存模型可見性問題相關(guān)解析
本文標(biāo)題:SpringCloud融入Python的實(shí)現(xiàn)
本文地址:http://www.jygsgssxh.com/a1/Java/8870.html
您可能感興趣的文章


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery


