Spring Clould Sidecar整合异构服务

前一阵子听 轮回说 他们公司用 Sidecar 整合异构php服务,我一听很好奇所以就研究了下 写了个 demo 。

本节我们主要讨论一下异构平台(比如,nodejs、python、php等提供的Rest接口服务)的服务,怎么通过spring cloud组件对这些服务注册到eureka中心以及与在微服务中怎么和异构平台的服务进行通信。这里主要是通过spring cloud的sidecar来构建异构平台的服务注册与通信。

sidecar灵感来自Netflix Prana。它可以获取注册中心的所有微服务实例的信息(例如host,端口等)的http api。也可以通过嵌入的Zuul代理来代理服务调用,该代理从Eureka获取其路由条目。 Spring Cloud配置服务器可以通过主机查找或通过嵌入的Zuul直接访问。

说简单点就是 ,我现在在 java写的微服务中如何才能调用其他语言写的对外的服务接口呢 这时候可以考虑一下 Sidecar 。

服务名 端口 用途
eureka-server 8765 服务注册与发现
microservice-sidecar 8070 异构服务对接服务
node 8060 nodejs服务

首先搭建 eureka-server

配置文件如下:

spring.application.name=eureka
server.port=8765
spring.cloud.client.ip-address=127.0.0.1
#表示是否注册Eureka服务器,因为自身作为服务注册中心,所以为false
eureka.client.registerWithEureka=false
eureka.server.enable-self-preservation=false

eureka.client.fetch-registry=false

#是否从eureka上获取注册信息,同上
eureka.client.fetchRegistry=false
spring.security.user.name=admin
spring.security.user.password=admin
spring.cloud.inetutils.timeout-seconds=10
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true
#eureka注册中心服务器地址
eureka.client.serviceUrl.defaultZone=http://admin:admin@localhost:8765/eureka/

spring.cloud.inetutils.ignored-interfaces[0]=em1:1
spring.cloud.inetutils.ignored-interfaces[1]=lo
spring.cloud.inetutils.ignored-interfaces[2]=tun0

启动类: EurekaApplication

/**
 * @author zhangyinghao
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

安全配置类:WebSecurityConfig


/**
 * @author zhangyinghao
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

启动后可以看见如下所示界面: 如下可以看到注册上去的服务 。


屏幕快照 2019-06-26 上午11.42.44.png

microservice-sidecar 异构其他语言服务模块

引入依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-sidecar</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zuul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
    </dependencies>

配置文件

server.port=8070
eureka.client.serviceUrl.defaultZone=http://admin:admin@localhost:8765/eureka/

eureka.instance.prefer-ip-address=true

eureka.client.register-with-eureka=true
eureka.instance.hostname=localhost
sidecar.port=8060
sidecar.health-uri=http://localhost:8060/health
spring.application.name=microservice-sidecar
#server.servlet.context-path=/microservice-sidecar

ribbon.connect-timeout=30000
ribbon.ReadTimeout=9000

解释下 :
sidecar.health-uri 对应 (第三方的服务地址) node 服务的健康检查接口
sidecar.port=8060 对应 (第三方的服务地址) node 服务启动端口

启动类 SidecarApplication

@EnableEurekaClient
@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
@EnableSidecar
@EnableFeignClients
public class SidecarApplication {
    public static void main(String[] args) {
        SpringApplication.run(SidecarApplication.class, args);
    }
}

java 对应 相关第三方服务的对应接口

@FeignClient("microservice-sidecar")
public interface NodeServiceClient {
    @GetMapping("/xixi")
    String getXixi();
    @GetMapping("/health")
    String getHealth();
    @GetMapping("/")
    String getIndex();
}

调用 第三方服务的 Controller

@RestController
public class NodeController {
    @Autowired
    private NodeServiceClient nodeServiceClient;
    @RequestMapping("/node/xixi")
    public String nodeXixi() {
        return nodeServiceClient.getXixi();
    }

    @RequestMapping("/node/health")
    public String nodeHealth() {
        return nodeServiceClient.getHealth();
    }

    @RequestMapping("/node")
    public String nodeIndex() {
        return nodeServiceClient.getIndex();
    }
}

第三方 服务 node 与上方的服务方法对应 (请求方式 参数 返回 )

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') {
        res.end(JSON.stringify({ "status" : "UP" }));
    }
    else if (pathname === '/xixi') {
        res.end(JSON.stringify({ "xixi" : "hhhh哈哈哈哈哈哈" }));
    }
    // 其他情况返回404
    else {
        res.end("404");
    }
});
// 创建监听,并打印日志
server.listen(8060, function() {
    console.log('listening on localhost:8060');
});

启动 node

屏幕快照 2019-06-26 上午11.57.23.png

启动顺序:
1.启动 node
2.启动eruka server
3.启动 microservice-sidecar

http://localhost:8070

屏幕快照 2019-06-26 上午11.59.36.png

http://localhost:8060/xixi
本地node 服务:

屏幕快照 2019-06-26 下午12.00.33.png

http://localhost:8070/node/xixi
sidecar 服务 :

屏幕快照 2019-06-26 下午1.13.01.png

如果大家需要完整代码请留言。
参考大神的sidecar 地址:https://www.jianshu.com/p/2788b7220407

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容