温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行SpringCloud融入Python的实现

发布时间:2021-10-13 09:43:32 来源:亿速云 阅读:271 作者:柒染 栏目:编程语言

这篇文章将为大家详细讲解有关如何进行SpringCloud融入Python的实现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

前言

该篇文章分享如何将Python Web服务融入到Spring Cloud微服务体系中,并调用其服务,Python Web框架用的是Tornado

构建Python web服务

引入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()  # 注册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,提供两个请求方式为GETPOST,接口路径为/test/main的外部调用接口

启动python服务(在此之前要创建一个Eureka服务注册中心)

python manage.py runserver

运行结果

服务调用(Feign) - Feign用于便捷调用HTTP API

congfig类中需添加注解@EnableFeignClients,具体使用请百度

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<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>

运行结果

在这里,我们用请求工具Postman来测试一下,可以看出,由TestController调用TestAPIClient再调用Python服务,至此,已完成微服务调用Python Web服务

关于如何进行SpringCloud融入Python的实现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI