前言
該篇文章分享如何將Python Web服務融入到Spring Cloud微服務體系中,並調用其服務,Python Web框架用的是Tornado,Tornado是使用Python編寫的Web服務器兼Web應用框架,與其他主流的Web服務器框架(主要是Python框架)不同是采用epoll非阻塞IO,響應快速,可處理數千並發連接,特別適用用於實時的Web服務。
構建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