一 建立一个Spring Boot工程,命名为eureka-server,并在pom.xml中引入必要的依赖,代码以下。愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三java
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.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-actuator</artifactId>--> <!--</dependency>--> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
二 经过@EnableEurekaServer注解启动一个服务注册中心提供给其余应用程序进行对话,只须要在Spring Boot应用中添加下面这个注解就能开启此功能。web
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
三 在默认状况下,服务注册中也会将本身做为客户端来尝试注册它本身,因此须要禁用它的客户端行为。spring
application.properties中增长以下配置。app
spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=localhost # 关闭保护机制 #eureka.server.enable-self-preservation=false eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ logging.file=${spring.application.name}.log
说明:
eureka.client.register-with-eureka:因为该应用为注册中心,因此设置为false,表明不向注册中心注册本身。
eureka.client.fetch-registry:因为注册中心的职责就是维护服务实例,它并不须要去检索服务,因此也设置为false。
总体代码结构以下:spring-boot