Spring Cloud Eureka
目錄:
Eureka介紹
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.
Eureka雲端服務發現,是一個基於 REST 的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。
簡單說,eureka是一個服務註冊中心,具有服務註冊和發現功能,在此基礎上可以更好的管理服務,
並且配合SpringCloud其他組件,如Zuul、Hystrix、Bus、Feign等,來更加高效、便利地治理微服務環境
單節點註冊中心
引入maven依賴:
<!--父依賴-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/>
</parent>
<!--父依賴-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<!--springcloud依賴組件-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--springboot 打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件 application.yml (yaml形式,或者application.properties,參考springboot配置說明)
server:
port: 9999
eureka:
instance:
hostname: localhost
client:
#由於該應用爲註冊中心,所以設置爲false,代表不向註冊中心註冊自己
registerWithEureka: false
#由於註冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置爲false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #註冊中心地址 注意:這裏需要加http:// 開頭,不然頁面會報錯
添加springboot啓動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer //eureka支持
@SpringBootApplication //springboot支持
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
啓動後,訪問 http://localhost:9999/,效果:
Eureka集羣
如果Eureka服務掛了,那麼其他註冊到上面的服務也將不可用。
Eureka可以多節點配置,也就是說每個Eureka可以作爲單獨的節點,並且這些節點互相註冊互相作爲註冊方和消費方。
這樣其中一個幾點掛了,其他節點照樣能提供服務。
項目演示:
在application.yml的基礎上,新建配置文件:application-pee1.yml application-pee2.yml application-pee3.yml
application.yml:
eureka:
instance:
hostname: localhost
client:
#默認爲ture,代表向註冊中心註冊自己
#registerWithEureka: false
#默認爲ture,從註冊中心獲取服務信息
#fetchRegistry: false
spring:
profiles:
active: pee1 #默認啓動pee1環境的配置
application-pee1.yml配置
server:
port: 9901
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9902/eureka/,http://localhost:9903/eureka/ #註冊中心地址爲其他2個節點地址
spring:
application:
name: eureka-server1 #服務名稱
pee1和pee2配置
pee2同pee1,將端口改爲9902,註冊中心地址改爲 http://9901,http://9903
pee3同pee1,將端口改爲9903,註冊中心地址改爲 http://9901,http://9902
分別啓動pee1 pee2
啓動startApp後,將application.yml中的active分別改爲pee2,pee3,分別啓動
然後分別訪問http://localhost:9901 http://localhost:9902 http://localhost:9903
效果如下:
源碼地址
https://github.com/yangzhenlong/springCloudDemo/springCloudEureka