1、創建服務注冊中心
創建一個普通的Spring Boot工程
首先我們需要創建一個普通的Spring Boot工程,命名為eureka-server,普通到什么程度呢?就是一個starter都不需要添加,創建成功之后就只引用了一個父starter。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> </parent>
添加Eureka依賴
工程創建成功之后,向pom.xml文件中添加eureka-server的依賴,目前eureka的穩定版本是Dalston.SR3,添加完依賴之后,pom.xml文件如下所示:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version><!-- eureka版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
配置服務注冊中心
最后我們再做一點簡單的配置就可以了,配置就寫在Spring Boot的配置文件application.properties中,寫法如下:
server.port=1111 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
或者application.yml
server: port: 1111 #服務端口 eureka: instance: hostname: localhost client: register-with-eureka: false #是否將eureka自身作為應用注冊到eureka注冊中心 fetch-registry: false #為true時,可以啟動,但報異常:Cannot execute request on any known server serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
OK,那么關於這幾行注釋,我說如下幾點:
1.server.port=1111表示設置該服務注冊中心的端口號
2.eureka.instance.hostname=localhost表示設置該服務注冊中心的hostname
3.eureka.client.register-with-eureka=false,由於我們目前創建的應用是一個服務注冊中心,而不是普通的應用。默認情況下,這個應用會向注冊中心(也是它自己)注冊它自己,設置為false表示禁止這種默認行為
4.eureka.client.fetch-registry=false,表示不去檢索其他的服務,因為服務注冊中心本身的職責就是維護服務實例,它也不需要去檢索其他服務
服務注冊中心啟動類
啟動一個服務注冊中心的方式很簡單,就是在Spring Boot的入口類上添加一個@EnableEurekaServer注解,不加這個注解訪問會報404,如下:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
啟動測試
OK,做完這一切之后,我們就可以啟動這一個Spring Boot 服務,服務啟動成功之后,在瀏覽器中輸入:http://localhost:1111
DS Replicas
Instances currently registered with Eureka
Application | AMIs | Availability Zones | Status |
---|---|---|---|
No instances available |
2、注冊服務提供者
OK,那么現在服務注冊中心有了之后,我們可以考慮向這個服務注冊中心注冊一個服務提供者了。
創建一個新的Spring Boot工程
還是創建一個Spring Boot工程,這次創建比之前創建多一個步驟,在創建的時候選中web的starter,我們來創建一個web工程。(本案例在blogdo基礎上帶動)
添加Eureka依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置服務名稱和注冊中心地址
最后的最后,我們在application-dev.yml文件中配置一下服務名和注冊中心地址即可,如下:
eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka spring: application: name: blogdo
創建應用的入口,添加注解@EnableEurekaClient
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class BlogdoApplication { public static void main(String[] args) { SpringApplication.run(BlogdoApplication.class, args); System.out.println("ヾ(◍°∇°◍)ノ゙ blogdo啟動成功 ヾ(◍°∇°◍)ノ゙\n"); } }
測試
做完這一切之后,我們就可以來測試了,直接運行這個Spring Boot工程,運行成功之后,我們刷新剛才的http://localhost:1111,就可以看到有一個服務已經注冊成功了。
DS Replicas
Instances currently registered with Eureka
Application | AMIs | Availability Zones | Status |
---|---|---|---|
BLOGDO | n/a (1) | (1) | UP (1) - SZVY2AWX5511361.china.huawei.com:blogdo:8083 |