spring cloud 2.x版本 Eureka Server服務註冊中心教程

本文采用Spring cloud本文爲2.1.8RELEASE,version=Greenwich.SR3 java

1.建立服務註冊中心

1.1 新建Spring boot工程:eureka-server

1.2 pom.xml所需依賴jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
複製代碼

1.3 EurekaServerApplication添加註解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
複製代碼

@EnableEurekaServer:啓用eureka server相關配置git

1.4 添加配置文件內容:application.yml

spring:
 application:
 name: eureka-server

server:
 port: 8701
#無參數啓動
eureka:
 instance:
 hostname: localhost
 prefer-ip-address: true
 client:
 service-url:
 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
 register-with-eureka: false #默認爲true,設置爲false,僅做爲服務中心,不做爲服務客戶端
 fetch-registry: false #默認爲true,設置爲false, 不從服務中心檢索註冊的服務
 server:
 eviction-interval-timer-in-ms: 5000 #清理間隔(單位毫秒, 默認是60*1000)
 enable-self-preservation: true #默認爲true,設置爲false,關閉自我保護
    #eureka server: 在運行期間會去統計心跳失敗比例在15分鐘以內是否低於85%
 renewal-percent-threshold: 0.49 #默認0.85
複製代碼

單機模式下: register-with-eureka和fetch-registry應爲false,不然啓動會報錯:Cannot execute request on any known server。緣由,在默認設置下,eureka服務註冊中心會將本身做爲客戶端來嘗試註冊本身。github

1.5 啓動服務

打開瀏覽器輸入:http://localhost:8701, 顯示以下:web

紅框內容表明尚未實例註冊spring

結語

至此,一個簡單的單機服務註冊中心就搭建完成了。瀏覽器

搭建服務註冊中心集羣

爲了保證服務的高可用,咱們須要把單機應用改爲集羣應用,接下來,咱們建立一個簡單的eureka server集羣.app

1.1 修改本地host

  • 127.0.0.1 eureka1.server.com
  • 127.0.0.1 eureka2.server.com
  • 127.0.0.1 eureka3.server.com

1.2 增長application.yml配置文件

增長application-server1.yml和application-server2.yml文件,同時修改原來的application.yml文件,文件內容以下:分佈式

  • application.yml
spring:
 application:
 name: eureka-server

server:
 port: 8701
#無參數啓動
eureka:
 instance:
 hostname: eureka1.server.com
 prefer-ip-address: true
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
 register-with-eureka: false
 fetch-registry: true
 server:
 eviction-interval-timer-in-ms: 5000
 enable-self-preservation: true
 renewal-percent-threshold: 0.49 
複製代碼
  • application-server1.yml
spring:
 application:
 name: eureka-server

server:
 port: 8702
#無參數啓動
eureka:
 instance:
 hostname: eureka2.server.com
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
 register-with-eureka: false 
 fetch-registry: true 
 server:
 eviction-interval-timer-in-ms: 5000 
 enable-self-preservation: true 
 renewal-percent-threshold: 0.49
複製代碼
  • application-server2.yml
spring:
 application:
 name: eureka-server

server:
 port: 8703
#無參數啓動
eureka:
 instance:
 hostname: eureka3.server.com
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
 register-with-eureka: false 
 fetch-registry: true
 server:
 eviction-interval-timer-in-ms: 5000
 enable-self-preservation: true
 renewal-percent-threshold: 0.49 #默認0.85
複製代碼

1.3 在idea中添加啓動服務

設置啓動服務,按照截圖中1234順序依次添加ide

分別建立eureka-server2和eureka-server3.(注:eureka-server1用原來的啓動就能夠)

1.4 按照順序分別啓動3個eureka server服務

啓動服務後分別訪問 eureka1.server.com:8701eureka1.server.com:8702eureka1.server.com:8703 如圖顯示:spring-boot

三個頁面如上圖顯示就表明服務所有啓動成功。至此,一個簡單的服務中心集羣就搭建完成。

總結

能夠將application-server1.yml和application-server2.yml的配置信息都放到原application.yml配置中,經過‘---’ 三橫槓模加spring.profiles模式來啓動,同時增長啓動參數: --spring.profiles.active=config-server1。本文采用過個application.yml的方式,方便之後的維護。

本文只是簡單的搭建了服務註冊中心的單機和集羣應用,對eureka作爲服務註冊中心有一個簡單對認識,後續會更新eureka的其餘內容。

代碼倉庫

gitHub地址


《Srping Cloud 2.X小白教程》目錄

轉載請註明出處,

  • 聯繫方式:4272231@163.com
相關文章
相關標籤/搜索