SpringCloud 融入 Python - Tornado
前言
该篇文章分享如何将Python Web服务融入到Spring Cloud微服务体系中,并调用其服务,Python Web框架用的是Tornado
构建Python web服务
- 引入
py-eureka-client
客户端
pip install py_eureka_client
manage.py
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=3000, 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="tornado-server",
instance_port=3000)
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
的注册中心注册服务名为tornado-server
的服务,端口为3000
,提供两个请求方式为GET
和POST
,接口路径为/test
和/main
的外部调用接口
- 启动python服务(在此之前要创建一个Eureka服务注册中心)
python manage.py runserver
- 运行结果
服务调用 - consumer-server工程
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- 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>
</dependencies>
ConsumerApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringCloudApplication
public class ConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
application.yml
spring:
profiles:
active: "dev"
application:
name: consumer-server
server:
port: 8325
eureka:
client:
healthcheck:
enabled: true
service-url:
defaultZone: http://${registry.host:localhost}:${registry.port:8761}/eureka/
---
spring:
profiles: dev
registry:
host: localhost
port: 31091
TestController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.coisini.consumer.client.TestAPIClient;
@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
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.coisini.consumer.config.FeignConfigure;
@FeignClient(name="tornado-server", 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.EnableFeignClients;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableFeignClients(basePackages = "com.coisini")
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));
}
}
运行结果
在这里,我们用请求工具Postman
来测试一下,可以看出,由TestController
调用TestAPIClient
再调用Python服务成功,至此,已完成微服务调用Python Web
服务
Demo下载
GitHub:SpringCloud 整合 Python - Tornado
Gitee:SpringCloud 整合 Python - Tornado
end
SpringCloud 融入 Python - Tornado的更多相关文章
- python tornado websocket 多聊天室(返回消息给部分连接者)
python tornado 构建多个聊天室, 多个聊天室之间相互独立, 实现服务器端将消息返回给相应的部分客户端! chatHome.py // 服务器端, 渲染主页 --> 聊天室建立web ...
- Python.tornado.0
Tornado https://github.com/facebook/tornado http://www.tornadoweb.org/en/stable/guide/intro.html (A ...
- python tornado 入门
#!/usr/bin/env python # coding:utf-8 import textwrap import tornado.httpserver import tornado.ioloop ...
- Python Tornado
按照http://www.tornadoweb.cn/所提供的方法下载安装后编写如下程序: import tornado.ioloop import tornado.web class MainHan ...
- 使用python + tornado 做项目demo演示模板
很简单,可是却也折腾了不是时间,走了不少弯路.在此备注记录一下,以供后需. # web_server.py #!/usr/bin/env python # coding=utf-8 import os ...
- python tornado+mongodb的使用
tornado tar xvzf tornado-1.2.1.tar.gz cd tornado-1.2.1 python setup.py build sudo python setup.py in ...
- Windows在配置Python+tornado
1,安装Python 2.7.x版本号 地址:https://www.python.org/downloads/release/python-278/ 2,安装python setuptools工具 ...
- python tornado 实现类禅道系统
最近楼主加班 喽, 好久没有更新我的博客了,哎,一言难尽,废话我就不说了,来开始上精华. 背景:目前市面上有很多bug管理工具,但是各有各的特点,最著名,最流行的就是禅道,一个偶然的机会接触到了pyt ...
- Python Tornado篇
Tornado既是一个web server,也是web framework.而它作为web server 采用的是asynchronous IO的网络模型,这是一种很高效的模型. Tornado 和现 ...
随机推荐
- enctype="multipart/form-data"的form传参
1.jsp <li class="btns"><input id="btnImport" class="btn btn-primar ...
- elasticsearch query 和 filter 的区别
Query查询器 与 Filter 过滤器 尽管我们之前已经涉及了查询DSL,然而实际上存在两种DSL:查询DSL(query DSL)和过滤DSL(filter DSL).过滤器(filter)通常 ...
- [转]WPF自定义控件之带倒计时的按钮--Button
1.说明 之前做过一个小项目,点击按钮,按钮进入倒计时无效状态,计时完成后,恢复原样,现在就实现该效果---带倒计时的按钮 2.效果 1)正常状态 2)MouseOver( ...
- [code] if (x<0)x=0;else if (x>255)x=255;
//颜色范围0-255: // 1.原始: )tem_b=;)tem_b=; )tem_g=;)tem_g=; )tem_r=;)tem_r=; //2.使用条件状态值生成掩码来移除条件分支 tem_ ...
- jmeter设置代理
JMeter设置Http代理对web或者app进行录制 一.录制web 1.首先保证JMeter的安装环境都正确.启动JMeter:在安装路径的bin目录下双击jmeter.bat (例如:D:\ap ...
- Docker(四)安装Redis
1.安装redis https://blog.csdn.net/qq_29917523/article/details/78019511 2.测试连接
- TZOJ 5986 玄武密码(AC自动机)
描述 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此. 很多年后,人们终于在 ...
- switch...case...之替换方案一
很多时候,当switch中有N个分支,且分支数已达10+,每个分支都是一个不小的方法体,那我们是不是应该考虑换一种方式来实现这个分支. 而我目前所能想到的是会用到如下几种方法. 1.Action 2. ...
- Ubuntu 18.04 美化
Ubuntu 18.04 美化 sudo apt install gnome-tweak-tool sudo apt install gnome-shell-extensions sudo apt i ...
- C#解析字符串公式
/// <summary> /// 中缀表达式到逆波兰表达式的转换及求值 /// </summary> public class RpnExpression { #region ...