(三)SpringBoot+SpringCloud —— 高可用的Eureka註冊中心

1、配置高可用的Eureka註冊中心集羣

以前使用的是獨立模式的註冊中心,爲了防止註冊中心崩潰使整個應用系統癱瘓,現採用集羣的方式部署高可用的註冊中心。java

建立兩個屬性文件,命名爲application-server1.properties和application-server2.properties:linux

#application-server1.properties
spring.application.name=eureka-server-01
server.port=9001
eureka.instance.hostname=server1
eureka.client.serviceUrl.defaultZone=http://server2:9002/eureka/
#application-server2.properties
spring.application.name=eureka-server-02
server.port=9002
eureka.instance.hostname=server2
eureka.client.serviceUrl.defaultZone=http://server1:9001/eureka/

這麼作的目的是要開啓兩個應用,一個端口爲9001,一個爲9002,相互註冊,便是服務端又是客戶端。git

須要注意的是hostname,須要在hostname文件中,加入映射,window系統在C:\Windows\System32\drivers\etc目錄下,linux系統在/etc/hosts:github

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

127.0.0.1 server1
127.0.0.1 server2

經過maven install打包jar包,而後打開兩個cmd,經過命令行執行:web

  • java -jar tbp-eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=server1
  • java -jar tbp-eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=server2

隨後訪問localhost:9001與localhost:9002,能夠看到註冊中心已經互相註冊了:spring

2、將服務註冊到Eureka註冊中心集羣

修改owl-bookstore-service-user工程的application.yml配置文件,將defaultZone設置爲集羣鏈接:bash

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/
server:
  port: 7001
spring:
  application:
    name: SERVICE-USER

從新啓動兩個user服務,打開兩個cmd,輸入:app

  • java -jar owl-bookstore-service-user-1.0-SNAPSHOT.jar --server.port=7001
  • java -jar owl-bookstore-service-user-1.0-SNAPSHOT.jar --server.port=7002

查看註冊中心,確認服務註冊成功:maven

3、將消費端註冊到Eureka註冊中心集羣

同服務的配置文件,這裏也將owl-bookstore-web-console工程的application.yml註冊到集羣:測試

spring:
  application:
    name: owl-bookstore-web-console
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/
server:
  port: 8000

測試如上圖。

到此,Eureka Server的集羣就已經搭建完畢了。

Github:https://github.com/XuePeng87/owl-bookstore

相關文章
相關標籤/搜索