使用Eureka将Node.js引入Spring Cloud

来源:互联网 发布:矩阵乘法优化 编辑:程序博客网 时间:2024/05/23 18:40
  • 简介

    • Spring Cloud是目前非常流行的微服务化解决方案,它将Spring Boot的便捷开发和Netflix OSS的丰富解决方案结合起来。如我们所知,Spring Cloud不同于Dubbo,使用的是基于HTTP(s)的Rest服务来构建整个服务体系。
    • Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能。
  • 服务端配置(Java)

    • application.properties

      server.port=1111eureka.instance.hostname=localhostspring.application.name=localhosteureka.client.register-with-eureka=trueeureka.client.fetch-registry=trueeureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    • pom.xml

      <?xml version="1.0" encoding="UTF-8"?><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>com.didispace</groupId>    <artifactId>eureka-server</artifactId>    <version>1.0.0</version>    <packaging>jar</packaging>    <name>eureka-server</name>    <description>Spring Cloud project</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.5.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka-server</artifactId>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Brixton.RELEASE</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
    • Application.java
      package com.didispace;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}
  • Node.js配置

    • 安装eureka-client

       npm install eureka-client --save
    • 安装express

      npm install express --save
    • 注册服务

      var Eureka = require('eureka-client').Eureka;const client = new Eureka({    instance: {        app: 'PhoneQ',        hostName: 'localhost',        ipAddr: '127.0.0.1',        statusPageUrl: 'http://localhost:3333',        port: {            '$': 3333,            '@enabled': 'true',        },        vipAddress: 'test.something.com',        dataCenterInfo: {            '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',            name: 'MyOwn',        },    },    eureka: {        serviceUrl: ['http://localhost:1111/eureka/apps/'],    },});client.start(function(error) {    console.log(error || 'Node server register completed');});
    • 使用express挂起服务

      const express = require('express');const app = express();app.get('/health', (req, res) => {    res.json({        status: 'UP'    });});app.get('/', (req, res, next) => {    res.json({status: true,message:"It's works!"});});app.listen(3333);
    • 测试Eureka服务
      启动Eureka服务,在浏览器输入http://localhost:1111/,看到类似下面的图片即说明服务成功开启:
      这里写图片描述
    • 使用Node.js注册
      将上面Node.js注册服务部分代码保存到index.js,然后开启终端输入:node index.js,看到类似下面的结果说明服务成功注册:
      这里写图片描述
    • 查看注册服务
      浏览器输入http://localhost:1111/,可以看到服务已经成功注册了:
      这里写图片描述
    • 测试express服务
      浏览器输入http://localhost:3333,可以看到服务已经挂起来了:
      这里写图片描述

    至此,Node.js成功引入Spring Cloud,看到这里的你也赶快动手试试吧,祝你成功!

0 0
原创粉丝点击