1、异构语言Node.js,扩展js功能,js是脚本语言,弱语言,不允许访问本地资源(io文件)。 Node.js突破了原JS问题束缚,让它能操作部分资源。 JS语法没有Java全。
2、Demo
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 === '/index'){ 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'); });3、运行&查看 在node-service.js文件所在的目录打开dos运行界面,或在dos界面打开node-service.js文件所在路径,输入命令
node 文件名显示如下内容,可访问8060端口号。
第二部分设定了访问规则,若为 /index ==》欢迎 /health.json ==>status:UP 其他 404
通过Sidecar来调用node.js服务。注意:yaml文件中不能有中文。
1、新建Maven项目 sidecar,配置pom文件
<!-- 父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> </parent> <!-- 解决乱码问题,设定jdk版本 --> <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> <!-- Eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- sidecar --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</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>2、yaml文件配置
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.json3、入口文件配置
@SpringBootApplication @EnableSidecar public class RunApplicationSidecar { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(RunApplicationSidecar.class, args); } }4、修改Zuul网关的yaml文件
server: port: 8050 spring: application: name: gateway-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启动Eureka、Sidecar、Zuul