1. 程式人生 > >springcloud(二):spring cloud eureka 註冊中心server 啟動

springcloud(二):spring cloud eureka 註冊中心server 啟動

尤里卡伺服器

spring cloud已經幫我實現了服務註冊中心,我們只需要很簡單的幾個步驟就可以完成。

1,POM中新增依賴

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

2,新增啟動程式碼中新增@EnableEurekaServer註解

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudEurekaApplication.class, args);
	}
}

3,配置檔案

在預設設定下,該服務註冊中心也會將自己作為客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行為,在application.properties

新增以下配置:

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
  • eureka.client.register-with-eureka :表示是否將自己註冊到尤里卡伺服器,預設為真。
  • eureka.client.fetch-registry
     :表示是否從Eureka Server獲取註冊資訊,預設為true。
  • eureka.client.serviceUrl.defaultZone :設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這些地址。預設是http:// localhost:8761 / eureka;多個地址可使用,分隔。

或者用下面這種配置

伺服器:
   埠: 8761
management:
  security:
    enabled: false
因為eureka註冊中心的重要性,必須要使用高可靠,叢集

叢集

註冊中心這麼關鍵的服務,如果是單點話,遇到故障就是毀滅性的。在一個分散式系統中,服務註冊中心是最重要的基礎部分,理應隨時處於可以提供服務的狀態。為了維持其可用性,使用叢集是很好的解決方案。Eureka通過互相註冊的方式來實現高可用的部署,所以我們只需要將Eureke Server配置其他可用的serviceUrl就能實現高可用部署。

雙節點註冊中心

首次我們嘗試一下雙節點的註冊中心的搭建。

1、建立application-peer1.properties,作為peer1服務中心的配置,並將serviceUrl指向peer2

spring.application.name=spring-cloud-eureka
server.port=8000
eureka.instance.hostname=peer1

eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

2、建立application-peer2.properties,作為peer2服務中心的配置,並將serviceUrl指向peer1

spring.application.name=spring-cloud-eureka
server.port=8001
eureka.instance.hostname=peer2

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

3、host轉換

在hosts檔案中加入如下配置

127.0.0.1 peer1  
127.0.0.1 peer2  

4、打包啟動

依次執行下面命令

#打包
mvn clean package
# 分別以peer1和peeer2 配置資訊啟動eureka
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

依次啟動完成後,瀏覽器輸入:http://localhost:8000/ 效果圖如下:

根據圖可以看出peer1的註冊中心DS Replicas已經有了peer2的相關配置資訊,並且出現在available-replicas中。我們手動停止peer2來觀察,發現peer2就會移動到unavailable-replicas一欄中,表示peer2不可用。

到此雙節點的配置已經完成。

eureka叢集使用

在生產中我們可能需要三臺或者大於三臺的註冊中心來保證服務的穩定性,配置的原理其實都一樣,將註冊中心分別指向其它的註冊中心。這裡只介紹三臺叢集的配置情況,其實和雙節點的註冊中心類似,每臺註冊中心分別又指向其它兩個節點即可,使用application.yml來配置。

application.yml配置詳情如下:

---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer1
server:
  port: 8000
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer2
server:
  port: 8001
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer3
server:
  port: 8002
eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/

分別以方1,對等方2,對等體3408的配置引數啟動尤里卡註冊中心。

java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

相關推薦

springcloud():spring cloud eureka 註冊中心server 啟動

尤里卡伺服器spring cloud已經幫我實現了服務註冊中心,我們只需要很簡單的幾個步驟就可以完成。1,POM中新增依賴<dependencies> <dependency> <groupId>org.springframework

Spring Cloud eureka註冊中心例項不可用,ribbon配置請求重試

 今天 在開發的時候,開發環境都 註冊到服務中心,但是有些開發同事服務是不可用的,但是又設定了服務保護機制,例項仍然是up 的,在本地進行開發的時候,可以使用eureka.server.enable-self-preservation=false引數來關閉保護機制,以確保註冊

Spring Cloud Eureka——註冊中心叢集

前面我們介紹了Spring Cloud Eureka的簡單使用。 但前面的例子中,這個服務註冊中心是單點的,顯然這並不適合應用於線上生產環境。服務註冊中心是服務的中心位置,如果服務註冊中心崩了,顯然是災難性的,不符合微服務架構的高可用性特點。 那麼如何搭建高可用的服務註冊中

Spring Cloud Eureka 註冊中心叢集搭建,Greenwich 最新版!

開發十年,就只剩下這套架構體系了! >>>   

2.spring cloud服務註冊中心eureka server---新增Hystrix Dashboard(第四章)

Hystrix Dashboard 我們在熔斷示例專案spring-cloud-consumer-hystrix的基礎上更改,重新命名為:spring-cloud-consumer-hystrix-dashboard。 1、新增依賴 org.springframework.boot 版本 :

1.spring cloud服務註冊中心eureka server---新增Security使用者認證(第四章)

為服務註冊中心eureka server—新增Security使用者認證 在spring-cloud-eureka服務註冊中心專案的基礎上增加使用者認證。 1、新增依賴 <dependency> <groupId>org.springframew

Spring Cloud Eureka):Eureka 註冊中心體驗

1、Eureka 簡述 本文主要從應用角度體驗一下注冊中心的搭建和使用,後文會由淺入深學習Spring Cloud Eureka 的各種原理和機制。 Spring Cloud Eureka 是 Spring Cloud Netflix 元件的一部分,而 Spri

Spring Cloud註冊中心 Eureka

前言 算是正式開始學習 spring cloud 的專案知識了,大概的知道Springcloud 是由眾多的微服務組成的,所以我們現在一個一個的來學習吧。 註冊中心,在微服務中算是核心了。所有的服務都會註冊到註冊中心,請求服務的時候,並不會直接去請求服務地址,而是先通過註冊中心再轉到目的地址。雖然Eureka

Spring Cloud註冊中心Eureka

大家如果覺得我寫的好,可以關注這個專案,之後我的學習過程中都會把學習筆記放入這個專案中。 https://github.com/IndustriousSnail/learning-notes Spring Cloud的註冊中心Eureka 目錄 一、Eureka基本概念

第二章、spring cloud服務註冊中心eureka---服務提供與呼叫

服務提供與呼叫 案例中有三個角色:服務註冊中心、服務提供者、服務消費者,其中服務註冊中心就是我們上一篇的eureka單機版啟動既可,流程是首先啟動註冊中心,服務提供者生產服務並註冊到服務中心中,消費者從服務中心中獲取服務並執行。 02eureka-pro

第一章、spring cloud服務註冊中心eureka---概念

註冊中心Eureka 背景介紹 服務中心 服務中心又稱註冊中心,管理各種服務功能包括服務的註冊、發現、熔斷、負載、降級等,比如dubbo admin後臺的各種功能。 有了服務中心呼叫關係會有什麼變化,畫幾個簡圖來幫忙理解 專案A呼叫專案B 正常呼叫專案A請求專案B

3、spring cloud服務註冊中心eureka---基於feign的負載均衡(第二章)

基於feign的負載均衡 spring-cloud-producer-one修改,將其中的controller改動如下: @RestController public class HelloController { @RequestMapping("/hello")

2、spring cloud服務註冊中心eureka---服務消費者(第二章)

服務呼叫 1、pom包配置 和服務提供者一致 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww

1、spring cloud服務註冊中心eureka---服務提供者(第二章)

服務提供 我們假設服務提供者有一個hello方法,可以根據傳入的引數,提供輸出“hello xxx,this is first messge”的服務 1、pom包配置 建立一個springboot專案,pom.xml中新增如下配置: <?xml version="1.0"

3、spring cloud服務註冊中心eureka---叢集配置(第一章)

eureka叢集配置 在生產中我們可能需要三臺或者大於三臺的註冊中心來保證服務的穩定性,配置的原理其實都一樣,將註冊中心分別指向其它的註冊中心。這裡只介紹三臺叢集的配置情況,其實和雙節點的註冊中心類似,每臺註冊中心分別又指向其它兩個節點即可,使用application.yml來配置。

2、spring cloud服務註冊中心eureka—雙節點配置(第一章)

叢集 註冊中心這麼關鍵的服務,如果是單點話,遇到故障就是毀滅性的。在一個分散式系統中,服務註冊中心是最重要的基礎部分,理應隨時處於可以提供服務的狀態。為了維持其可用性,使用叢集是很好的解決方案。Eureka通過互相註冊的方式來實現高可用的部署,所以我們只需要將Eureke Server配

1、spring cloud服務註冊中心eureka---單節點配置(第一章)

Eureka Server—單節點配置 spring cloud已經幫我實現了服務註冊中心,我們只需要很簡單的幾個步驟就可以完成。 1、pom中新增依賴 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=

基於spring-cloud-eureka 服務中心註冊

本文基於Finchley.SR2以上,Edgware.SR5的配置部分不適用 spring-boot版本為2.0.2RELEASE, 用部分設定1.5已不適用 伺服器端設定 伺服器pom檔案設定 加入依賴 spring-cloud-starter-netflix-eureka

SpringCloudSpring Cloud Bus配置中心

一、簡介 ConfigServer使用了Spring Cloud Bus之後(引入Spring Cloud Bus用來操作訊息佇列),會對外提供一個介面,叫做bus-refresh,遠端git訪問這個介面ConfigServer就會把配置更新的資訊傳送到訊息佇列(RabbitMQ)裡面,Co

Spring Cloud註冊中心Eureka(02)

Eureka是Netflix開源的一款提供服務註冊和發現的產品,它提供了完整的Service Registry和Service Discovery實現。也是springcloud體系中最重要最核心的元件之一。   背景介紹 服務中心 服務中心又稱註冊中心,管理各種服務功能包