当前位置: 首页 > 编程笔记 >

SpringCloud融入Python的实现

白星渊
2023-03-14
本文向大家介绍SpringCloud融入Python的实现,包括了SpringCloud融入Python的实现的使用技巧和注意事项,需要的朋友参考一下

前言

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

构建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,提供两个请求方式为GET和POST,接口路径为/test和/main的外部调用接口

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

python manage.py runserver

运行结果


服务调用(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<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服务

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 简介 Spring cloud config 分为两部门 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用server端暴露接口获取配置信息 config-server 创建config-server 首先创建config-server工程. 文件结构: ├── config-server.iml ├── po

  • 本文向大家介绍SpringCloud Eureka Provider及Consumer的实现,包括了SpringCloud Eureka Provider及Consumer的实现的使用技巧和注意事项,需要的朋友参考一下 Eureka-Provider 服务的提供者 新建一个服务提供者项目 1、导入pom文件 2、在启动类上加注解 上边那个@EnableDiscoverClient 注解加不加都行的

  • 本文向大家介绍python实现两张图片的像素融合,包括了python实现两张图片的像素融合的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python实现两张图片像素融合的具体代码,供大家参考,具体内容如下 通过计算两张图片的颜色直方图特征,利用直方图对图片的颜色进行融合。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • eureka Standalone Mode Peer Awareness Standalone Mode java -jar springcloud-eureka-0.0.1.jar Cluster Mode java -Deureka.instance.hostname=eureka01 -Deureka.client.serviceUrl.defaultZone=http://eureka0

  • 本文向大家介绍springcloud实现注册中心Eureka,包括了springcloud实现注册中心Eureka的使用技巧和注意事项,需要的朋友参考一下 Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。 背景介绍 服务中心 服务中心又称注

  • 自我介绍 谈大学经历,你最有成就感的事情 问项目 python的数据类型有哪些 list有哪些方法 dict有哪些应用 这俩项目里感觉最困难的是什么 django运作的一整套流程 说一下你最熟悉的协议(tcp、http、get、post) 说一下docker 了解k8s吗 说一下redis吧 说一下mongodb吧 你还有什么要问我的吗#实习,投递多份简历没人回复怎么办##pyhton##开发##