Spring Boot 教程:Eureka 服务器

【注】本文译自: https://www.tutorialspoint.com/spring_boot/spring_boot_eureka_server.htm

  Eureka 服务器是一个应用,它包含所有客户端服务应用的信息。每个微服务都会注册到 Eureka 服务器并且 Eureka 服务器知道所有客户端应用的端口和 IP 地址。Eureka 服务器也被称为发现服务器。
  本文将带你学习如何搭建 Eureka 服务器。搭建 Eureka 服务器
  Eureka 服务器与 Spring Cloud 打包发布。基于此,我们需要开发 Eureka 服务器并将它运行于缺省的 8761 端口上。
  访问 Spring 初始化器主页 https://start.spring.io/ 并下载 Spring Boot 工程的 Eureka 服务器依赖。如下图所示:

  工程下载之后,在主 Spring Boot 应用类文件中,我们要加上 @EnableEurekaServer 注解。@EnableEurekaServer 注解可使你的 Spring Boot 应用用作 Eureka 服务器。
  主 Spring Boot 应用类文件如下所示:

package com.tutorialspoint.eurekaserver;

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

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

  确保你的构建配置文件中已经加入了 Spring cloud Eureka 服务器依赖。
  Maven 用户的依赖代码如下:

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

  Gradle 用户的依赖代码如下:

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

  完整的构建配置代码文件如下所示:
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.tutorialspoint</groupId>
   <artifactId>eurekaserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>eurekaserver</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-server</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-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

  缺省情况下,Eureka 服务器会将自身注册到发现中。你应当加入以下配置到 application.properties 或 application.yml 文件中:
application.properties 文件如下所示:

eureka.client.registerWithEureka = false
eureka.client.fetchRegistry = false
server.port = 8761

application.yml 文件如下所示:

eureka:
  client:
     registerWithEureka: false
     fetchRegistry: false
server:
  port: 8761

  现在可以使用 Maven 或 Gradle 命令创建可执行 executable JAR 文件并运行 Spring Boot 应用了:
  Maven 命令如下:

mvn clean install

  在 “BUILD SUCCESS” 之后,你可以在 target 目录下找到 JAR 文件。
  Gradle 可以使用以下命令:

gradle clean build

  在 “BUILD SUCCESSFUL” 之后,你可以在 build/libs 目录下找到 JAR 文件。
  使用以下命令运行 JAR 文件:

java –jar <JARFILE>

  应用已在 Tomcat 8761 端口启动,如下图所示:

  接下来,在 web 浏览器中单击 URL http://localhost:8761/,可以发现 Eureka 服务器已经运行在 8761 端口,如下所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值