参考文章:
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工程
Sidecar简介
- 在springcloud的项目中,我们需要允许使用不同语言去实现微服务,但是非java语言是无法接入eureka,hystrix,fegin,ribbon等相关组件。有一种思路就是启动一个代理的微服务,由该代理微服务同非jvm服务交流,并接入eureka等相关组件。Sidecar就是该思路的实现,总结的说就是作为一个代理的服务来间接性的让其他语言可以使用Eureka等相关组件。
- 目前sidecar这个服务必须和非jvm语言的服务在同一台主机上面,也就是说他们之间是localhost访问的,不能是ip访问等等
- 目前觉得sidecar还不是非常适用,如每一个异构的微服务都要对应的建一个sidecar的微服务,十分不合理。
- 通过sidecar微服务异构其它类型服务后,该微服务异构的服务, 只能通过其它微服务调用该微服务才能访问,且调用方式只能使用ribbon调用, feign和zuul调用均失败
前提: 这里假设eureka注册中心已搭建完成
一.搭建python服务
1.新建py_test.py
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')
二.搭建spring-sidecar-python-server工程
该工程负责调用步骤一中的python工程
1.pom.xml文件
<?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>
2.application.yml文件配置
#用于开发环境
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}
3.springboot main入口文件
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-caller工程
该工程负责调用步骤二中的spring-sidecar-python-server工程
1.pom.xml文件
<?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>
2.application.yml文件配置
spring:
application:
name: spring-sidecar-python-caller #ServiceId, 配置服务命名,不区分大小写,在注册中心管理界面默认大写显示
server:
port: 10232
eureka.client.service-url.defaultZone: http://10.101.15.159:18000/eureka/
3.springboot main入口文件
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();
}
}
4.controller文件
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();
}
}
全部启动完成后,