python spring cloud_SpringCloud 整合 Python - Tornado

前言

该篇文章分享如何将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

org.springframework.cloud

spring-cloud-starter-eureka-server

org.springframework.cloud

spring-cloud-starter-hystrix

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

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

@Bean

public Encoder feignFormEncoder() {

return new SpringFormEncoder(new SpringEncoder(messageConverters));

}

}

运行结果

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

Demo下载

end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值