在了解了 Euerka 的 REST API
瓤逼、核心類
、核心操作
库物、參數(shù)調(diào)優(yōu)
等概念之后霸旗,在實(shí)際的項(xiàng)目中來驗(yàn)證這些概念。
Eureka Server 集群擴(kuò)展
在之前的例子中戚揭,和 SpringCloud 中文社區(qū)的公益 Eureka Server 都是單節(jié)點(diǎn)的诱告,如果 Server 掛掉了,那么整個微服務(wù)的注冊將不在可用民晒。在這時精居,就需要搭建 Eureka Server 高可用集群,保證整個微服務(wù)不會因?yàn)橐粋€ Server 掛掉而導(dǎo)致整個微服務(wù)不可用潜必。
使用 profile靴姿,搭建高可用集群
有兩種常用的方式啟動多個 eureka server
在一個配置文件中,指定多個配置
可以使用如下配置刮便,在一個 application.yml 文件中空猜,配置多個 Eureka Server,相互注冊指定 defaultZone恨旱,并使用 profile 區(qū)別每個 Server 的配置辈毯。
spring:
application:
name: spring-cloud-eureka-server-ha
---
server:
port: 8761
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:8762/eureka,http://localhost:8763/eureka
spring:
profiles: peer1
---
spring:
profiles: peer2
server:
port: 8762
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8763/eureka
---
spring:
profiles: peer3
server:
port: 8763
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
驗(yàn)證多 Server 啟動
使用 mvn spring-boot:run -Dspring.profiles.active=peer1
peer1 可以換為 peer2、peer3搜贤,啟動其他的 profile
在瀏覽器中輸入: http://localhost:8761谆沃、http://localhost:8762、http://localhost:8763仪芒,都可以訪問到 Eureka Server
使用這種方式啟動存在的問題:
在一個配置文件中存在多個 Server 的配置唁影,太過雜亂無章,不好管理
如果 Server 在不同的機(jī)器上掂名,由于 ip 地址不同据沈,在第一個 Server 啟動時由于找不到注冊中心,必報錯饺蔑,當(dāng)?shù)诙€ Server 啟動后正常
使用多配置文件
將 application.yml 復(fù)制多份锌介,改名為 application-peer1.yml
、application-peer2.yml
、application-peer3.yml
孔祸,然后使用 mvn spring-boot:run -Dspring.profiles.active=peer1 啟動項(xiàng)目隆敢。
為驗(yàn)證此種配置方式可用,將 peer3 的端口該為 8764崔慧,啟動測試
使用這種方式的問題:
可能存在同樣的配置在多個配置文件都存在拂蝎,需要修改時需要修改每個文件,太過冗余
Server 在不同機(jī)器上的時候惶室,出現(xiàn)的問題和 在一個配置文件中温自,指定多個配置 的問題一致
接口驗(yàn)證
@SpringBootApplication
@EnableEurekaServer
@RestController
public class SpringCloudEurekaServerHaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaServerHaApplication.class, args);
}
@Autowired
private EurekaClientConfigBean eurekaClientConfigBean;
@GetMapping(value = "eureka-service-url")
public Object getEurekaServerUrl() {
return eurekaClientConfigBean.getServiceUrl();
}
}
重啟 3 個Server,瀏覽器訪問 http://localhost:8764/eureka-service-url
可以看到拇涤,peer3 中注冊了兩個 eureka server捣作。
Eureka Client 注冊到多 Server
Client 注冊到多 Server誉结,只需要在配置文件中指定對應(yīng) Server 的 defauleZone 即可鹅士。
spring:
application:
name: eureka-client
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8764/eureka/,
server:
enable-self-preservation: false
使用 Region、Zone 搭建高可用集群
配置文件 application-zone1a.yml:
spring:
application:
name: spring-cloud-eureka-server-region-zone
server:
port: 8761
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
region: region-east # 設(shè)置 region
availability-zones:
region-east: zone1,zone2 # 設(shè)置可用 region-zone
instance:
hostname: localhost
prefer-ip-address: true
metadata-map:
zone: zone1 # 設(shè)置 zone
application-zone1b.yml: 將 server.port 修改為 8762
application-zone2a.yml: 將 server.port 修改為 8763惩坑,eureka.instance.metadata-map.zone 修改為 zone2
application-zone2b.yml: 將 server.port 修改為 8764掉盅,eureka.instance.metadata-map.zone 修改為 zone2
驗(yàn)證 Eureka Server
在瀏覽器訪問 http://localhost:8761
創(chuàng)建 Eureka Client
創(chuàng)建兩個 Eureka Client,分別對應(yīng)兩個 Zone
application-zone1.yml
server:
port: 8081
spring:
application:
name: spring-cloud-eureka-client-region-zone
eureka:
instance:
metadata-map:
zone: zone1
client:
region: region-east
availability-zones:
region-east: zone1,zone2
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
application-zone2.yml:修改 server.port=8082以舒,eureka.instance.metadata-map.zone=zone2
暴露服務(wù)端點(diǎn)
application.yml:
management:
endpoints:
web:
exposure:
include: '*'
引入 pom 依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 暴露端點(diǎn) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
驗(yàn)證 region趾痘、zone
訪問 client 暴露的環(huán)境端點(diǎn),驗(yàn)證 region蔓钟、zone
http://localhost:8081/actuator/env永票、http://localhost:8082/actuator/env
由此,可以驗(yàn)證 client1滥沫、client2 的 zone 是指定的 zone侣集。
開啟 Http Basic
現(xiàn)在的實(shí)例中,訪問 Eureka Server 是不需要用戶名兰绣、密碼的履肃,不需要安全驗(yàn)證藏鹊。為了防止微服務(wù)暴露,可以開啟 Http Basic 安全教研。
Eureka Server 開啟 Http Basic
引入 pom 依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
創(chuàng)建配置文件
server:
port: 8761
spring:
security:
user:
name: laiyy # 訪問 Eureka Server 的用戶名
password: 123456 # 訪問 Eureka Server 的密碼
eureka:
client:
service-url:
defaultZone: http://localhost:${server.port:8761}/eureka/
register-with-eureka: false
fetch-registry: false
訪問 http://localhost:8761
Eureka Client 開啟 Http Basic
引入 pom 依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置文件
spring:
application:
name: spring-cloud-eureka-client-http-basic
eureka:
client:
security:
basic:
user: laiyy
password: 123456
service-url:
defaultZone: http://${eureka.client.security.basic.user}:${eureka.client.security.basic.password}@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
server:
port: 8081
需要注意奴艾,defaultZone 需要設(shè)置為: http://user:password@ip:port/eureka/
啟動 Eureka Client,驗(yàn)證 Http Basic
在啟動 Client 后笛洛,觀察日志况木,可以看到出現(xiàn)了 403 錯誤:
[圖片上傳失敗...(image-6ca959-1551662462133)]
明明已經(jīng)指定了 Eureka Server 的用戶名、密碼健无、ip荣恐、端口,為什么還是注冊失敳墙А募胃?
是因?yàn)?Http Basic 默認(rèn)是同源的旗唁,而 client、server 的 ip痹束、端口不一致检疫,會出現(xiàn)跨域訪問請求,導(dǎo)致 403.
解決辦法:在 Eureka Server 端關(guān)閉 csrf 訪問祷嘶。
@EnableWebSecurity
public class HttpBasicConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
}
}
重新啟動 Server屎媳、Client,訪問 Server论巍,可以看到 Client 注冊成功