該篇文章分享如何將Python Web
服務融入到Spring Cloud微服務體系中,並調用其服務,Python Web框架用的是Flaskjava
Sidecar
+ Flask
,在這裏,咱們會使用Sidecar
將Python
接口註冊到SpringCloud
中,將Python
接口看成Java
接口進行調用(經過SpringCloud
去調用Sidecar
,而後經過Sidecar
去轉發咱們的程序請求)python
Sidecar
是SpringCloud
提供的一個可將第三方的rest
接口集成到SpringCloud
中的工具manage.py
import json from flask import Flask, Response, request, make_response, jsonify app = Flask(__name__) @app.route("/health") def health(): result = {'status': 'UP'} return Response(json.dumps(result), mimetype='application/json') @app.route("/getUser") def getUser(): result = {'username': 'python', 'password': 'python'} return Response(json.dumps(result), mimetype='application/json') @app.errorhandler(404) def not_found(error): return make_response(jsonify({'error': 'Not found'}), 404) if __name__ == "__main__": app.run(host="0.0.0.0", port=3000)
大體說下上述代碼,Python
服務監聽3000
端口,health
方法用於給Sidecar
提供健康接口,用於實時向Sidecar
提供本身的健康狀態,getUser
是Python
向外界提供的服務web
python manage.py runserver
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
SidecarApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.sidecar.EnableSidecar; @EnableSidecar @SpringBootApplication public class SidecarApplication { public static void main(String[] args) { SpringApplication.run(SidecarApplication.class, args); } }
application.yml
spring: profiles: active: "dev" application: name: demo-sidecar sidecar: port: 3000 health-uri: http://localhost:${sidecar.port}/health ribbon: ConnectTimeout: 50000 ReadTimeout: 50000 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 10000 server: port: 8326 eureka: client: healthcheck: enabled: true service-url: defaultZone: http://${registry.host:localhost}:${registry.port:8761}/eureka/ registry: host: localhost port: 31091
大體說下上述代碼,main
方法要使用@EnableSidecar
註解,sidecar port
表明監聽Python
運行的端口,server port
表明Sidecar
運行的端口,spring application name
表明Sidecar
的服務名,sidecar health-uri
是Python
健康接口,指向python的健康服務
spring
@SpringBootApplication public class DemoServerApplication { public static void main(String[] args) { SpringApplication.run(DemoServerApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
RestTemplateController.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class RestTemplateController { @Autowired private RestTemplate restTemplate; @RequestMapping("/java-user") public String JavaUser() { return "{'username': 'java', 'password': 'java'}" ; } @RequestMapping("/python-user") public String PythonUser() { return restTemplate.getForEntity("http://demo-sidecar/getUser", String.class).getBody(); // return restTemplate.getForEntity("http://localhost:3000/getUser", String.class).getBody(); } }
@LoadBalanced
用於開啓負載均衡,在這裏有兩種調用方式,使用和不使用@LoadBalanced
@LoadBalanced
註解後,RestTemplate
能夠直接調用服務名@Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } ++++++++++++++++++++++++++++++ return restTemplate.getForEntity("http://demo-sidecar/getUser", String.class).getBody();
@LoadBalanced
註解,RestTemplate
調用的就是固定的IP+PORT@Bean // @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } ++++++++++++++++++++++++++++++ return restTemplate.getForEntity("http://localhost:3000/getUser", String.class).getBody();
congfig
類中需添加註解@EnableFeignClients
,具體使用請百度SidecarController.java
@RestController public class SidecarController { private SidecarAPIClient sidecarAPIClient; @Autowired public SidecarController(SidecarAPIClient sidecarAPIClient) { this.sidecarAPIClient = sidecarAPIClient; } @GetMapping("/getUser") public Object getUser() { return this.sidecarAPIClient.getUser(); } }
SidecarAPIClient.java
@FeignClient(name="demo-sidecar", configuration = FeignConfigure.class) public interface SidecarAPIClient { @GetMapping("/getUser") Object getUser(); }
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)); } }
Sidecar
是一個用於監聽非JVM應用程序(能夠是Python
或者Node
或者Php
等等)的一個工具,經過Sidecar
能夠實現Java
和第三方應用程序的雙向交互Sidecar
報告本身的狀態,告訴Sidecar
本身還在運行着。Sidecar
應用程序必須和第三方應用程序運行在同一臺電腦上,也就是說他們之間是localhost
,不能是IP
訪問SpringCloud 整合Python 感謝大佬json
endflask