Eureka高可用集群配置
當注冊中心扛不住高並發的時候,這時候 要用集群來扛;
我們再新建兩個module microservice-eureka-server-2002 microservice-eureka-server-2003
第一步:
pom.xml 把依賴加下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-eureka-server</
artifactId
>
</
dependency
>
<!-- 修改后立即生效,熱部署 -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>springloaded</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-devtools</
artifactId
>
</
dependency
>
</
dependencies
>
|
第二步:
2002 2003的主啟動類EurekaServerApplication_2002,EurekaServerApplication_2003復制修改下;
第三步:
前面單機的時候 eureka注冊中心實例名稱 是localhost,現在是集群,不能三個實例都是localhost,這里復雜的辦法是搞三個虛擬機,麻煩,這里有簡單辦法,直接配置本機hosts,來實現本機域名映射;
找到 C:\Windows\System32\drivers\etc 打開hosts,加配置
127.0.0.1 eureka2001.java1234.com
127.0.0.1 eureka2002.java1234.com
127.0.0.1 eureka2003.java1234.com
第四步:
修改三個項目的application.yml文件,主要是修改 hostname和defaultZone,
2001修改:
server:
port: 2001
context-path: /
eureka:
instance:
# 單機 hostname: localhost #eureka注冊中心實例名稱
hostname: eureka2001.java1234.com # 集群
client:
register-with-eureka: false #false 由於該應用為注冊中心,所以設置為false,代表不向注冊中心注冊自己。
fetch-registry: false #false 由於注冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置為false
service-url:
defaultZone: http://eureka2002.java1234.com:2002/eureka/,http://eureka2003.java1234.com:2003/eureka/ # 集群
#單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設置與Eureka注冊中心交互的地址,查詢服務和注冊服務用到
2002修改:
server:
port: 2002
context-path: /
eureka:
instance:
# 單機 hostname: localhost #eureka注冊中心實例名稱
hostname: eureka2002.java1234.com # 集群
client:
register-with-eureka: false #false 由於該應用為注冊中心,所以設置為false,代表不向注冊中心注冊自己。
fetch-registry: false #false 由於注冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置為false
service-url:
defaultZone: http://eureka2001.java1234.com:2001/eureka/,http://eureka2003.java1234.com:2003/eureka/ # 集群
#單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設置與Eureka注冊中心交互的地址,查詢服務和注冊服務用到
2003修改:
server:
port: 2003
context-path: /
eureka:
instance:
# 單機 hostname: localhost #eureka注冊中心實例名稱
hostname: eureka2003.java1234.com # 集群
client:
register-with-eureka: false #false 由於該應用為注冊中心,所以設置為false,代表不向注冊中心注冊自己。
fetch-registry: false #false 由於注冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置為false
service-url:
defaultZone: http://eureka2001.java1234.com:2001/eureka/,http://eureka2002.java1234.com:2002/eureka/ # 集群
#單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設置與Eureka注冊中心交互的地址,查詢服務和注冊服務用到
第五步:修改服務提供者項目的application.yml,主要修改eureka.client.service-url.defaultZone
server:
port: 1001
context-path: /
# 數據源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_springcloud2
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost #eureka客戶端主機實例名稱
appname: microservice-student #客戶端服務名
instance-id: microservice-student:1001 #客戶端實例名稱
prefer-ip-address: true #顯示IP
client:
service-url:
# 單機 defaultZone: http://localhost:2001/eureka #把服務注冊到eureka注冊中心
defaultZone: http://eureka2001.java1234.com:2001/eureka/,http://eureka2002.java1234.com:2002/eureka/,http://eureka2003.java1234.com:2003/eureka/ # 集群
info:
groupId: $project.groupId$
artifactId: $project.artifactId$
version: $project.version$
負責人: 張三
聯系電話: 110
最后我們測試下:
啟動三個注冊中心,以及服務提供者項目;
然后瀏覽器地址欄輸入:http://eureka2001.java1234.com:2001/
這里說明,集群創建OK; 紅色的那塊 ,后面會講到,是eureka的自我保護。
假如訪問 http://eureka2002.java1234.com:2002/ http://eureka2003.java1234.com:2003/
都是一個效果;
這里本質是三個服務注冊中心都有我們服務提供者的信息,等后面講到服務發現和服務調用,我們通過一些策略(默認輪詢),會去找對應的服務注冊中心;通過集群,能減輕每個服務注冊中心的壓力;