Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Integration of SpringCloud into Python

Shulou Source: shulou.com Published: 2022-06-03 06:21:21 10月03日 Update

This article will explain in detail how to integrate SpringCloud into Python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Preface

This article shares how to integrate Python Web services into the Spring Cloud microservice architecture and invoke its services. The Python Web framework uses Tornado.

Build a Python web service

Introduction of py-eureka-client client

Pip install py_eureka_client

Manage.py

#! / usr/bin/env pythonimport tornado.httpserverimport tornado.ioloopimport tornado.optionsimport tornado.webimport py_eureka_client.eureka_client as eureka_clientfrom tornado.optionsimport define, optionsfrom time import sleepdefine ("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 username') Def post (self): username = self.get_argument ('username',' Hello') self.write (username +', Administrator username') class MainHandler (tornado.web.RequestHandler): def get (self): username = self.get_argument ('username',' Hello') self.write (username +', Coisini username') Def post (self): username = self.get_argument ('username',' Hello') self.write (username +', Coisini username') def main (): tornado.options.parse_command_line () # register eureka service 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 ()

Roughly speaking, the above code registers a service named python-tornado with the registry service with port 31091, port 3333, provides two request methods: GET and POST, and the interface path is / test and / main.

Start the python service (create an Eureka service registry before that)

Python manage.py runserver

Running result

Service invocation (Feign)-Feign for convenient invocation of HTTP API

You need to add an annotation @ EnableFeignClients to the congfig class. For more information, please use Baidu.

TestController.java

RestControllerpublic 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;@Configurationpublic 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 dependence

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

Running result

Here, we test it with the request tool Postman. We can see that TestController invokes TestAPIClient and then invokes the Python service. At this point, the micro service invokes the Python Web service.

On how to implement SpringCloud into Python to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Service article content interface article more knowledge port result run good convenient two code system preface customer client tool way Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info OPPO Reno Linux Apple