SpringCloud融入Python的实现方法-创新互联

这篇文章将为大家详细讲解有关SpringCloud融入Python的实现方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

专业领域包括成都做网站、成都网站建设、成都商城网站开发、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,创新互联公司的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。

构建Python web服务

引入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服务
  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

运行结果

SpringCloud融入Python的实现方法

SpringCloud融入Python的实现方法

服务调用(Feign) - Feign用于便捷调用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 messageConverters;
  
  @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

运行结果

SpringCloud融入Python的实现方法

SpringCloud融入Python的实现方法

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

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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站题目:SpringCloud融入Python的实现方法-创新互联
网页路径:http://myzitong.com/article/gseji.html