1. 程式人生 > >如何創建“服務註冊中心”

如何創建“服務註冊中心”

例子 框架 pom AC pro 開啟 spring tid 註解

創建一個基礎的Spring Boot工程,命名為eureka-server,並在pom.xml中引入需要的依賴內容:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

通過@EnableEurekaServer註解啟動一個服務註冊中心提供給其他應用進行對話。這一步非常的簡單,只需要在一個普通的Spring Boot應用中添加這個註解就能開啟此功能,比如下面的例子:

@EnableEurekaServer  
@SpringBootApplication  
public class Application {  
public static void main(String[] args) {  
new SpringApplicationBuilder(Application.class)  
.web(true).run(args);  
}  
}  

在默認設置下,該服務註冊中心也會將自己作為客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行為,只需要在application.properties配置文件中增加如下信息:

spring.application.name=eureka-server  
server.port=1001  
eureka.instance.hostname=localhost  
eureka.client.register-with-eureka=false  
eureka.client.fetch-registry=false  

為了與後續要進行註冊的服務區分,這裏將服務註冊中心的端口通過server.port屬性設置為1001。

從現在開始,我這邊會將近期研發的springcloud微服務雲架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud框架的朋友,希望可以幫助更多的好學者。大家來一起探討spring cloud架構的搭建過程及如何運用於企業項目。

如何創建“服務註冊中心”