赞
踩
参考文章:
spring-cloud-sidecar集成非java服务端(php,node.js,paython)到spring cloud
Spring Cloud微服务解决方案⑨:Sidecar异构
Spring Cloud之Zuul(七):主要实现非jvm语言结合Sidecar实现服务注册
目录
二.搭建spring-sidecar-python-server工程
三.搭建spring-sidecar-python-caller工程
前提: 这里假设eureka注册中心已搭建完成
python可访问服务地址如下::http://localhost:3000/health, http://localhost:3000/getUser
内容如下:
- import json
- from flask import Flask, Response
- app = Flask(__name__)
- @app.route("/health")
- def health():
- result = {'status': 'UP'} # UP要大写
- # response header里面解析的text/html, 应该改成application/json
- 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.run(port=3000, host='0.0.0.0')
该工程负责调用步骤一中的python工程
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <!--<parent>-->
- <!--<groupId>com.cloud</groupId>-->
- <!--<artifactId>cloud-parent</artifactId>-->
- <!--<version>0.0.1-SNAPSHOT</version>-->
- <!--</parent>-->
-
- <!--<properties>-->
- <!--<project-name>spring-sidecar-python-server</project-name>-->
- <!--</properties>-->
-
- <!-- 定义公共资源版本 -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.5.RELEASE</version>
- <relativePath/>
- </parent>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <spring-cloud.version>Finchley.SR2</spring-cloud.version>
- <project-name>spring-sidecar-python-server</project-name>
- </properties>
-
- <artifactId>${project-name}</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>${project-name}</name>
- <description>${project-name}</description>
-
- <dependencies>
- <!-- 包含 mvc,aop 等jar资源 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!--其依赖ribbon, hystrix等-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-netflix-sidecar</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
-
- <!--<dependency>-->
- <!--<groupId>org.springframework.cloud</groupId>-->
- <!--<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
- <!--</dependency>-->
- </dependencies>
-
- <!--依赖管理, 用于管理spring-cloud的依赖, 其中Camden.SR3是版本号-->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <!--远程仓库-->
- <repositories>
- <repository>
- <id>spring-snapshots</id>
- <name>Spring Snapshots</name>
- <url>https://repo.spring.io/snapshot</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
- </project>
- #用于开发环境
- spring:
- application:
- name: spring-sidecar-python-server #ServiceId, 配置服务命名,不区分大小写,在注册中心管理界面默认大写显示
- server:
- port: 10230
-
- eureka.client.service-url.defaultZone: http://10.101.15.59:18000/eureka/
-
- sidecar:
- port: 3000
- health-uri: http://localhost:${sidecar.port}/health # 这里必须是localhost
-
- # ========================= eureka client =========================== #
- eureka:
- client:
- service-url:
- # defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/
- defaultZone: http://10.101.15.59:18000/eureka/
- instance:
- #不使用主机名来定义注册中心的地址, 而使用IP地址的形式,
- #如果设置了 ipAddress 属性, 则使用该属性配置的IP, 否则自动获取除环路IP外的第一个IP地址
- preferIpAddress: true #自动将IP注册到Eureka Server上, 如果不配置就是机器的主机名
- #ipAddress: 192.168.1.128 # IP地址
- #将Instance ID设置成IP:端口的形式
- instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
- package com.cloud;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.sidecar.EnableSidecar;
-
- // @EnableSidecar是一个组合注解, 它整合了三个注解,
- // 分别是:@EnableCircuitBreaker、@EnableDiscoveryClient、@EnableZuulProxy
- @EnableSidecar
- @SpringBootApplication
- public class SidecarPythonServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(SidecarPythonServerApplication.class, args);
- }
- }
该工程负责调用步骤二中的spring-sidecar-python-server工程
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <!--<parent>-->
- <!--<groupId>com.cloud</groupId>-->
- <!--<artifactId>cloud-parent</artifactId>-->
- <!--<version>0.0.1-SNAPSHOT</version>-->
- <!--</parent>-->
-
- <!--<properties>-->
- <!--<project-name>spring-sidecar-python-server</project-name>-->
- <!--</properties>-->
-
- <!-- 定义公共资源版本 -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.5.RELEASE</version>
- <relativePath/>
- </parent>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <spring-cloud.version>Finchley.SR2</spring-cloud.version>
- <project-name>spring-sidecar-python-caller</project-name>
- </properties>
-
- <artifactId>${project-name}</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>${project-name}</name>
- <description>${project-name}</description>
-
- <dependencies>
- <!--Hystrix 依赖 主要是用 @HystrixCommand-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
- </dependency>
- </dependencies>
-
- <!--依赖管理, 用于管理spring-cloud的依赖, 其中Camden.SR3是版本号-->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <!--远程仓库-->
- <repositories>
- <repository>
- <id>spring-snapshots</id>
- <name>Spring Snapshots</name>
- <url>https://repo.spring.io/snapshot</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
- </project>
- spring:
- application:
- name: spring-sidecar-python-caller #ServiceId, 配置服务命名,不区分大小写,在注册中心管理界面默认大写显示
- server:
- port: 10232
-
- eureka.client.service-url.defaultZone: http://10.101.15.159:18000/eureka/
- package com.cloud;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.client.RestTemplate;
-
- @EnableEurekaClient
- @EnableCircuitBreaker
- @SpringBootApplication
- public class SidecarPythonCallerApplication {
- public static void main(String[] args) {
- SpringApplication.run(SidecarPythonCallerApplication.class, args);
- }
-
- /**
- * 让restTemplate具备Ribbon负载均衡的能力。
- * 由于使用feign, 弃用该方式
- */
- @Bean
- @LoadBalanced
- RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
- package com.cloud.controller;
-
- import com.cloud.service.SidecarPythonCallerService;
- 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;
-
- /**
- * @author Administrator 2018/10/29
- */
- @RestController
- @RequestMapping(value = "/")
- public class SidecarPythonCallerController {
- @RequestMapping("/java-user")
- public String JavaUser() {
- return "{'username': 'java', 'password': 'java'}" ;
- }
-
- @RequestMapping("/python-user")
- public String PythonUser() {
- return restTemplate.getForEntity("http://spring-sidecar-python-server/getUser", String.class).getBody();
- }
- }
全部启动完成后,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。