類似於DUBBO 的zookeeper, SpringCloud本身提供一套服務注冊中心--eureka
與zookeeper的區別在於
1:zookeeper本身就是一個應用,安裝即可用;eureka其實是一個jar,需要新建一個maven項目,以及手動配置端口和pom文件。發布后即可使用
2:zookeeper本身不提供web端展示,需要重新安裝配置dubbo客戶端或者dubbokeeper實時監控服務;eureka發布成功后,即可有對應的spring 服務監控頁面。
搭建SpringCloud的服務注冊中心-eureka
1.新建maven項目eureka-server,eureka-server是作為一個子項目,目錄結構如下
eureka-server還是比較簡單的,主要需要修改這三個文件:EurekaServerApplication,application.yml,pom.xml
1.pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
spring-cloud-starter-eureka-server:erueka注冊中心核心依賴包
當然,作為一個springboot項目,需要依賴於:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
所有的子項目都是springcoot項目,所以該依賴放在了父項目的pom中
2.application.yml
springboot啟動時,會自動掃描該配置文件,當然,application.properties也是可以的
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
其中:server.port:定義端口號
eureka.instance.hostname:定義eureka為本地ip,發布測試或者線上環境視情況配置
eureka.client.registerWithEureka:是否注冊本身,這里不需要。所以為false
eureka.client.fetchRegister:是否從服務器獲取注冊信息,這里也不需要
eureka.client.serviceUrl.defaultZone:定義服務注冊地址,后續的提供或者消費都需要通過該地址進行注冊
3.EurekaServerApplication.java
springboot啟動文件
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
@EnableEurekaServer:表明他是服務注冊中心
@SpringBootApplication:表明他是springboot項目
最后:啟動該項目,就可以了
瀏覽器打開:http://localhost:8761/
從中可以看出,eureka服務注冊中心已經啟動成功。只是目前的application中沒有任何內容,當然,我們只是搞定了這個注冊中心,還沒有任何的服務呢
下面,我們來嘗試寫一個服務,同時注冊到該注冊中心
注冊服務eureka-client
首先,還是來看下這個項目eureka-client的結構
還是修改這三個文件:EurekaClientApplication,application.yml,pom.xml(所有的springboot項目,這三個文件也是最基本的,缺一不可)
1.pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8762 spring: application: name: eureka-client
eureka.client.serviceUrl.defaultZone:與eureka-server中的配置文件配置一樣就行了
server.port:定義端口號
spring.application.name:定義項目名,方便在注冊中心查看
3.EurekaClientApplication.java
@SpringBootApplication @EnableEurekaClient @RestController public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/") public String home() { return "hello world from port " + port; } }
@EnableEurekaClient:同意注冊到注冊中心
再啟動eureka-client 后,會發現如圖:
eureka-client已經注冊成功
直接輸入:http://localhost:8762/
至此,基礎的服務注冊中心搭建成功,有問題希望大家多提意見,謝謝!