欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud融入Python的實(shí)現(xiàn)

 更新時(shí)間:2019年12月26日 09:19:27   作者:95.8℃  
這篇文章主要介紹了SpringCloud融入Python的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

該篇文章分享如何將Python Web服務(wù)融入到Spring Cloud微服務(wù)體系中,并調(diào)用其服務(wù),Python Web框架用的是Tornado

構(gòu)建Python web服務(wù)

引入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()
  # 注冊(cè)eureka服務(wù)
  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的注冊(cè)中心注冊(cè)服務(wù)名為python-tornado的服務(wù),端口為3333,提供兩個(gè)請(qǐng)求方式為GETPOST,接口路徑為/test/main的外部調(diào)用接口

啟動(dòng)python服務(wù)(在此之前要?jiǎng)?chuàng)建一個(gè)Eureka服務(wù)注冊(cè)中心)

python manage.py runserver

運(yùn)行結(jié)果


服務(wù)調(diào)用(Feign) - Feign用于便捷調(diào)用HTTP API

congfig類中需添加注解@EnableFeignClients,具體使用請(qǐng)百度

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>

運(yùn)行結(jié)果



在這里,我們用請(qǐng)求工具Postman來測(cè)試一下,可以看出,由TestController調(diào)用TestAPIClient再調(diào)用Python服務(wù),至此,已完成微服務(wù)調(diào)用Python Web服務(wù)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java異常中toString()和getMessage()區(qū)別

    Java異常中toString()和getMessage()區(qū)別

    在java異常體系中,要打印異常信息,可以通過:e.getMessage() 、 e.toString() e.printStackTrace() 等方法打印,本文主要介紹了Java異常中toString()和getMessage()區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(1)

    java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(1)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • Java雙冒號(hào)(::)運(yùn)算符使用詳解

    Java雙冒號(hào)(::)運(yùn)算符使用詳解

    之前沒用過::這個(gè)東西,今天看flink的時(shí)候發(fā)現(xiàn)官網(wǎng)有個(gè)例子用到了這個(gè)符號(hào),本文就詳細(xì)的來介紹一下Java雙冒號(hào)(::)運(yùn)算符使用詳解,感興趣的可以了解一下
    2021-09-09
  • Springboot集成mybatis與jsp過程詳解

    Springboot集成mybatis與jsp過程詳解

    這篇文章主要介紹了Springboot集成mybatis與jsp過程,Spring Boot是一種全新的框架(相對(duì)而言),是用來簡(jiǎn)化Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進(jìn)行配置
    2021-09-09
  • 教你如何區(qū)分Spring與Structs2中間件

    教你如何區(qū)分Spring與Structs2中間件

    這篇文章主要介紹了教你如何區(qū)分Spring與Structs2中間件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • js中去除字符串中所有的html標(biāo)簽代碼實(shí)例

    js中去除字符串中所有的html標(biāo)簽代碼實(shí)例

    這篇文章主要介紹了js中去除字符串中所有的html標(biāo)簽代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Mybatis之foreach標(biāo)簽內(nèi)傳入list為空的問題

    Mybatis之foreach標(biāo)簽內(nèi)傳入list為空的問題

    這篇文章主要介紹了Mybatis之foreach標(biāo)簽內(nèi)傳入list為空的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java中ArrayList和LinkedList的遍歷與性能分析

    Java中ArrayList和LinkedList的遍歷與性能分析

    這篇文章主要給大家介紹了ArrayList和LinkedList這兩種list的五種循環(huán)遍歷方式,各種方式的性能測(cè)試對(duì)比,根據(jù)ArrayList和LinkedList的源碼實(shí)現(xiàn)分析性能結(jié)果,總結(jié)結(jié)論。相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考價(jià)值,有需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12
  • 判斷二叉樹是否為完全二叉樹的實(shí)例

    判斷二叉樹是否為完全二叉樹的實(shí)例

    這篇文章主要介紹了判斷二叉樹是否為完全二叉樹的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(10)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(10)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07

最新評(píng)論