? ? ? Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
? ? ? ?本文章详细介绍如何搭建一个eureka-server工程
步骤一:安装jdk1.8(相信有工作经验的都会,不详细说明了)
步骤二:创建一个maven工程eureka-server,settings使用阿里maven仓库
? ? ? ? settings.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\repository-eureka</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
</profiles>
</settings>
步骤三:修改pom文件,内容如下??
<?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.csii</groupId>
<artifactId>eureka-server</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>eureka-server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 对应springboot1.5.x -->
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<package.output.file.name>eureka-server</package.output.file.name>
<package.output.dir>${project.build.directory}/${package.output.file.name}</package.output.dir>
</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>
<resources>
<resource>
<directory>sh</directory>
<targetPath>${package.output.dir}</targetPath>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<outputDirectory>${package.output.dir}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打指定的包类型 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions> <!--执行器 mvn assembly:assembly -->
<execution>
<id>make-zip</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<finalName>${package.output.file.name}</finalName>
<descriptors> <!--描述文件路径 -->
<descriptor>./assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
步骤四:创建启动类EurekaServerApp.java
package com.csii;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
System.out.println("Application eureka server start success");
}
}
步骤五:配置application.yml
#本地运行默认使用此文件配置
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 30
lease-expiration-duration-in-seconds: 30
server:
enable-self-preservation: false #设为false,关闭自我保护
eviction-interval-timer-in-ms: 60000 #清理间隔(单位毫秒,默认是60*1000)
client:
registerWithEureka: false #是否将自己注册到Eureka Server 默认true
fetchRegistry: false #是否从Eureka Server获取注册的服务信息 fale表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
serviceUrl:
defaultZone: http://localhost:8761/eureka/
步骤六:运行启动类EurekaServerApp.java
出现下图内容说明启动成功
步骤七:访问eureka-server 访问地址:http://localhost:8761
能够正常访问,eureka项目搭建成功。
生产环境往往都是多台集群配置,本人也搭建了一套集群环境,提供了启动脚本和集群部署手册
single:单机版本
peer1:集群节点1
peer2:集群节点2
peer3:集群节点3
需要源码和集群部署手册的可微信联系