這篇文章將為大家詳細講解有關(guān)SpringCloud融入Python的實現(xiàn)方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司10多年成都定制網(wǎng)站服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)站及推廣,對水電改造等多個行業(yè)擁有多年的網(wǎng)站設(shè)計經(jīng)驗的網(wǎng)站建設(shè)公司。
構(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
運行結(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 ObjectFactorymessageConverters; @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } }
Feign依賴
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
運行結(jié)果
在這里,我們用請求工具Postman
來測試一下,可以看出,由TestController
調(diào)用TestAPIClient
再調(diào)用Python服務(wù),至此,已完成微服務(wù)調(diào)用Python Web服務(wù)
關(guān)于“SpringCloud融入Python的實現(xiàn)方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。