1. 程式人生 > >Spring Cloud構建微服務架構—創建“服務註冊中心”

Spring Cloud構建微服務架構—創建“服務註冊中心”

springboot springcloud mybatis eureka config

創建一個基礎的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。啟動工程後,可以看到下面的頁面,其中還沒有發現任何服務。完整項目的源碼來源 技術支持求求1791743380

Spring Cloud構建微服務架構—創建“服務註冊中心”