使用Eureka将Node.js引入Spring Cloud

  • 简介

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

    • application.properties

      server.port=1111
      eureka.instance.hostname=localhost
      spring.application.name=localhost
      eureka.client.register-with-eureka=true
      eureka.client.fetch-registry=true
      eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    • 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.0modelVersion>
          <groupId>com.didispacegroupId>
          <artifactId>eureka-serverartifactId>
          <version>1.0.0version>
          <packaging>jarpackaging>
          <name>eureka-servername>
          <description>Spring Cloud projectdescription>
          <parent>
              <groupId>org.springframework.bootgroupId>
              <artifactId>spring-boot-starter-parentartifactId>
              <version>1.3.5.RELEASEversion>
              <relativePath/> 
          parent>
          <properties>
              <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
              <java.version>1.8java.version>
          properties>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.bootgroupId>
                  <artifactId>spring-boot-starter-testartifactId>
                  <scope>testscope>
              dependency>
              <dependency>
                  <groupId>org.springframework.cloudgroupId>
                  <artifactId>spring-cloud-starter-eureka-serverartifactId>
              dependency>
          dependencies>
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloudgroupId>
                      <artifactId>spring-cloud-dependenciesartifactId>
                      <version>Brixton.RELEASEversion>
                      <type>pomtype>
                      <scope>importscope>
                  dependency>
              dependencies>
          dependencyManagement>
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.bootgroupId>
                      <artifactId>spring-boot-maven-pluginartifactId>
                  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
      @SpringBootApplication
      public 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/,看到类似下面的图片即说明服务成功开启:
      使用Eureka将Node.js引入Spring Cloud_第1张图片
    • 使用Node.js注册
      将上面Node.js注册服务部分代码保存到index.js,然后开启终端输入:node index.js,看到类似下面的结果说明服务成功注册:
      使用Eureka将Node.js引入Spring Cloud_第2张图片
    • 查看注册服务
      浏览器输入http://localhost:1111/,可以看到服务已经成功注册了:
      使用Eureka将Node.js引入Spring Cloud_第3张图片
    • 测试express服务
      浏览器输入http://localhost:3333,可以看到服务已经挂起来了:
      使用Eureka将Node.js引入Spring Cloud_第4张图片

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

你可能感兴趣的:(Spring)