SpringCloud---(9)Sidecar(支持异构微服务)

集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)

关于Sidecar不会讲很多,只要讲一下在SpringCloud中的应用


1、在一些稍微复杂点系统中,往往都不是单一代码写的服务,而恰恰相反集成了各种语言写的系统,并且我们还要很好的解耦合集成到自己的系统中;
2、出于上述现状,SpringCloud 生态圈中给我们提供了很好的插件式服务,利用 sidecar 我们也可以轻松方便的集成异构系统到我们自己的系统来;
3、该项目就是为了解决轻松简便的集成异构系统到自己的微服务系统中来的;

注意:Sidecar并不是SpringCloud生态的组件,而是SpringCloud支持Sidecar

 

该项目使用node.js语言作为测试

 

9.1 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>

    <artifactId>microservice-sidecar</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>microservice-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 异构系统模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-sidecar</artifactId>
            <version>1.2.1.RELEASE</version>
        </dependency>


        <!-- 客户端发现模块,由于文档说 Zuul 的依赖里面不包括 eureka 客户端发现模块,所以这里还得单独添加进来 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

</project>

 

9.2 application.yml

spring:
  application:
    name: microservice-sidecar
server:
  port: 8070  #指定服务端口
eureka:
  datacenter: SpringCloud   # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Data center 显示信息
  environment: Test         # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Environment 显示信息
  client:
    service-url:
      defaultZone: http://kevin:123456@localhost:8761/eureka
  #注册IP
  instance:
    prefer-ip-address: true
#打印日志
logging:
  level:
    com.netflix: debug
#异构微服务的配置, port 代表异构微服务的端口;health-uri 代表异构微服务的操作链接地址
sidecar:
  port: 8060
  health-uri: http://localhost:8060/health.json

9.3 SidecarApplication.java

package com.kevin.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;

/**
 *
 * @title   集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)。
 * 注意 EnableSidecar 注解能注册到 eureka 服务上,是因为该注解包含了 eureka 客户端的注解,该 EnableZuulProxy 是一个复合注解。
 * @EnableSidecar --> { @EnableCircuitBreaker、@EnableDiscoveryClient、@EnableZuulProxy } 包含了 eureka 客户端注解,同时也包含了 Hystrix 断路器模块注解,还包含了 zuul API网关模块。
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication
@EnableSidecar
public class SidecarApplication {

    public static void main(String[] args) {
        SpringApplication.run(SidecarApplication.class, args);
        System.out.println("SideCar启动...");
    }

}

 

9.4 node-server.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":"欢迎来到首页,该程序由Node.js提供"}
   if(pathname === '/'){
      res.end(JSON.stringify({"index" : "欢迎来到首页,该程序由Node.js提供"}));
   }
   //访问http://localhost:8060/health,将会返回{"status" : "UP"}
   else if (pathname === '/health.json'){
      res.end(JSON.stringify({"status" : "UP"}));
   }
   //其他情况返回404
   else{
      res.end("404")
   }
});
//创建监听,并打印日志
server.listen(8060,function(){
   console.log('listening on localhost:8060')
});

 

9.5 演示

 

注:如果电脑中没有安装node.js语言,可以先安装node.js语言

1.启动zureka
2.
启动异构微服务
node node-service.js
3.
启动zuul
访问异构微服务本身
http://localhost:8060/
通过zuul网关访问异构微服务
http://localhost:8040/microservice-sidecar

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值