一 建立一個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