使用 Eureka 註冊服務

【注】本文譯自:https://www.tutorialspoint.co...

  本文將帶你深刻學習如何將 Spring Boot 微服務應用註冊到 Eureka 服務器中。在註冊應用前,請確保 Eureka Server 已經運行在 8761 端口或者先構建 Eureka 服務器並運行起來。有關搭建 Eureka 服務器的信息,能夠參考本系列教程的相關部分。
  首先,你須要在構建配置文件中加入如下依賴,以註冊微服務到 Eureka 服務器。
  Maven 用戶能夠加上下面的依賴到 pom.xml 文件:java

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

  Gradle 用戶能夠加上下面的依賴到 build.gradle 文件:web

compile('org.springframework.cloud:spring-cloud-starter-eureka')

  如今,咱們須要在 Spring Boot 應用類文件中加上 @EnableEurekaClient 註解。@EnableEurekaClient 註解能夠使你的 Spring Boot 應用做爲 Eureka 客戶端。
  主 Spring Boot 就用以下所示:spring

package com.tutorialspoint.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(EurekaclientApplication.class, args);
   }
}

  要註冊 Spring Boot 應用到 Eureka 服務器中,咱們要加上如下配置到 application.properties 或 application.yml 文件,並指定 Eureka 服務器的 URL。
  application.yml 文件的代碼以下:apache

eureka:
  client:
     serviceUrl:
        defaultZone: http://localhost:8761/eureka
     instance:
     preferIpAddress: true
spring:
  application:
     name: eurekaclient

  application.properties 文件的代碼以下:
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka
eureka.client.instance.preferIpAddress = true
spring.application.name = eurekaclient
  如今,在主 Spring Boot 應用中加上 Rest 端點以返回字符串,在構建配置文件中要加上相應的應用描述。示例代碼以下:瀏覽器

package com.tutorialspoint.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(EurekaclientApplication.class, args);
   }
   @RequestMapping(value = "/")
   public String home() {
      return "Eureka Client application";
   }
}

  整個配置文件以下:
  Maven - pom.xmlbash

<?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.tutorialspoint</groupId>
   <artifactId>eurekaclient</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>eurekaclient</name>
   <description>Demo project for Spring Boot</description>

   <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>
      <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</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>

  Gradle - build.gradle服務器

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-starter-eureka')
   testCompile('org.springframework.boot:spring-boot-starter-test')
   compile('org.springframework.boot:spring-boot-starter-web')   
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

  你能夠使用 Maven 或 Gradle 命令建立可執行 executable JAR 文件並運行 Spring Boot 應用:
  Maven 命令以下:app

mvn clean install

  在 「BUILD SUCCESS」 以後,你能夠在 target 目錄下找到 JAR 文件。
  Gradle 能夠使用如下命令:eclipse

gradle clean build

  在 「BUILD SUCCESSFUL」 以後,你能夠在 build/libs 目錄下找到 JAR 文件。
  使用如下命令運行 JAR 文件:maven

java –jar <JARFILE>

  如今,應用已經在 Tomcat 8080 端口啓動,且 Eureka 客戶端應用已經被註冊到 Eureka 服務器,以下所示:

  在 Web 瀏覽器中輸入 URL http://localhost:8761/,能夠看到 Eureka 客戶端應用已經被註冊到 Eureka 服務器。

  在 Web 瀏覽器中輸入 URL http://localhost:8080/,能夠看到 Rest 端點輸出。

相關文章
相關標籤/搜索