Eureka整合sidecar异构调用
本次使用nodejs脚本生成的异构程序测试:
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'});
if(pathname === '/'){
res.end(JSON.stringify({ "index":"欢迎" }));
}else if(pathname === '/health.json'){
res.end(JSON.stringify({ "status":"UP" }));
}else{
res.end("404");
}
});
//创建监听,并打印日志
server.listen(8060, function(){
console.log('listening on localhost:8060');
});
生成js文件保存执行cmd node+路径
测试 : localhost:8060 是否正常访问

1.首先修改Zuul网关的配置:
server:
port: 8050
spring:
application:
name: client-Zuul
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
zuul:
ignoredServices: '*'
routes:
app-provider-user: #名称随意,唯一就好
path: /user/**
serviceId: provider-user
app-sidecar: #通过sidecar调用nodejs的服务
path: /sidecar/**
serviceId: sidecar
2.SideCarRunApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;
@SpringBootApplication
@EnableSidecar
public class SideCarRunApp {
public static void main(String[] args) {
SpringApplication.run(SideCarRunApp.class, args);
}
}
application.yml
server:
port: 8070
spring:
application:
name: sidecar
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
sidecar:
port: 8060
health-uri: http://localhost:8060/health.json
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> <groupId>eureka.sidecar</groupId> <artifactId>eureka-sidecar</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </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> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>

浙公网安备 33010602011771号