本文主要是介绍SpringCloud-使用Sidecar支持异构平台的微服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
概述
Spring Cloud是目前非常流行的微服务化解决方案,它将Spring Boot的便捷开发和Netflix OSS的丰富解决方案结合起来。如我们所知,Spring Cloud不同于Dubbo,使用的是基于HTTP(s)的Rest服务来构建整个服务体系。
那么有没有可能使用一些非JVM语言,例如我们所熟悉的Node.js来开发一些Rest服务呢?当然是可以的。但是如果只有Rest服务,还不能接入Spring Cloud系统。我们还想使用起Spring Cloud提供的Eureka进行服务发现,使用Config Server做配置管理,使用Ribbon做客户端负载均衡。这个时候Spring sidecar就可以大显身手了。
Sidecar起源于Netflix Prana。他提供一个可以获取既定服务所有实例的信息(例如host,端口等)的http api。你也可以通过一个嵌入的Zuul,代理服务到从Eureka获取的相关路由节点。Spring Cloud Config Server可以直接通过主机查找或通过代理Zuul进行访问。
需要注意的是你所开发的Node.js应用,必须去实现一个健康检查接口,来让Sidecar可以把这个服务实例的健康状况报告给Eureka。
创建工程microservice-sidecar
pom.xml
microservice-sidecar/pom.xml
<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.itmuch.cloud</groupId><artifactId>microservice-spring-cloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>microservice-sidecar</artifactId><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-netflix-sidecar</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies></project>
说明:
1)添加依赖spring-cloud-netflix-sidecar
application.yml
spring:application:name: microservice-sidecar
server:port: 8070
eureka:client:service-url:defaultZone: http://user:password123@localhost:8761/eurekainstance:prefer-ip-address: true
sidecar:port: 8060health-uri: http://localhost:8060/health.json
说明:
1)在application.yml里加入sidecar.port和sidecar.health-uri的配置。
2)其中sidecar.port属性代表这个Node.js应用监听的端口,指定非java应用的健康监听端口。
3)这是为了让sidecar可以正常的注册到Eureka服务中。
4)sidecar.health-uri是一个用来模拟Spring Boot应用健康指标的接口的uri。
5)它必须返回如下形式的json文档: health-uri-document
SidecarApplication
com.itmuch.cloud.SidecarApplication
package com.itmuch.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;@SpringBootApplication
@EnableSidecar
public class SidecarApplication {public static void main(String[] args) {SpringApplication.run(SidecarApplication.class, args);}
}
说明:
1)编写sidecar微服务;
2)启动类上加上@EnableSidecar注解。
3)这是一个组合注解,它整合了三个注解,分别是@EnableCircuiBreaker,@EnableDiscoveryClient和@EnableZuulProxy;
创建nodejs工程-node-service
node-service/node-service.js
var http = require('http');
var url = require("url");
var path = require('path');// 创建server
var server = http.createServer(function(req, res) {// 获得请求的路径var pathname = url.parse(req.url).pathname; res.writeHead(200, { 'Content-Type' : 'application/json; charset=utf-8' });// 访问http://localhost:8060/,将会返回{"index":"欢迎来到首页"}if (pathname === '/') {res.end(JSON.stringify({ "index" : "欢迎来到首页" }));}// 访问http://localhost:8060/health,将会返回{"status":"UP"}else if (pathname === '/health.json') {res.end(JSON.stringify({ "status" : "UP" }));}// 其他情况返回404else {res.end("404");}
});
// 创建监听,并打印日志
server.listen(8060, function() {console.log('listening on localhost:8060');
});
启动
node node-service.js
测试
查看注册的服务
http://localhost:8761/
启动nodejs工程
http://localhost:8060/
sidecar检查异构语言的健康地址
1)http://localhost:8060/health.json
说明:
1)非java应用返回健康状态;
2)sidecar.health-uri是一个用来模拟Spring Boot应用健康指标的接口的uri;
3)提供http接口,返回json:{"status" : "up"},status用于描述微服务的状态,
4)常见的取值有UP,DOWN,OUT_OF_SERVICE,UNKNOWN等
2)访问microservice-sidecar服务
http://localhost:8070/
http://localhost:8070/health.json
3)通过zuul访问microservice-sidecar服务
查看zuul路由映射
http://localhost:8040/routes
zuul访问microservice-sidecar服务
http://localhost:8040/microservice-sidecar/health.json
http://localhost:8040/microservice-sidecar/
说明:
1)zuul-》microservice-sidecar-》nodes服务;
---------------------
原文:https://blog.csdn.net/shenzhen_zsw/article/details/81009238
这篇关于SpringCloud-使用Sidecar支持异构平台的微服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!