本次使用nodejs脚本生成的异构程序测试:
node-server.js
var http = require('http');var url = require('url');var path = require('path');//创建servervar 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: 8050spring: application: name: client-Zuuleureka: client: serviceUrl: defaultZone: http://user:password123@localhost:8761/eurekazuul: 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@EnableSidecarpublic class SideCarRunApp { public static void main(String[] args) { SpringApplication.run(SideCarRunApp.class, args); }}
application.yml
server: port: 8070spring: application: name: sidecareureka: client: serviceUrl: defaultZone: http://user:password123@localhost:8761/eurekasidecar: port: 8060 health-uri: http://localhost:8060/health.json
pom.xml
4.0.0 eureka.sidecar eureka-sidecar 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-netflix-sidecar org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-dependencies Dalston.SR1 pom import
测试访问: