Spring Cloud 學(xué)習(xí)(4) --- Eureka(三) 集群、Region Zone湾揽、Http Basic

在了解了 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

在一個配置文件中,指定多個配置

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-server-ha

可以使用如下配置刮便,在一個 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

Eureka Server Peer1

Eureka Server Peer2

Eureka Server Peer3

在瀏覽器中輸入: http://localhost:8761谆沃、http://localhost:8762http://localhost:8763仪芒,都可以訪問到 Eureka Server

使用這種方式啟動存在的問題:

在一個配置文件中存在多個 Server 的配置唁影,太過雜亂無章,不好管理
如果 Server 在不同的機(jī)器上掂名,由于 ip 地址不同据沈,在第一個 Server 啟動時由于找不到注冊中心,必報錯饺蔑,當(dāng)?shù)诙€ Server 啟動后正常

使用多配置文件

將 application.yml 復(fù)制多份锌介,改名為 application-peer1.ymlapplication-peer2.ymlapplication-peer3.yml孔祸,然后使用 mvn spring-boot:run -Dspring.profiles.active=peer1 啟動項(xiàng)目隆敢。

為驗(yàn)證此種配置方式可用,將 peer3 的端口該為 8764崔慧,啟動測試

Eureka Server Peer4

使用這種方式的問題:

可能存在同樣的配置在多個配置文件都存在拂蝎,需要修改時需要修改每個文件,太過冗余
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

Service Urls

可以看到拇涤,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 搭建高可用集群

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-client-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

Region Zone Server

創(chuàng)建 Eureka Client

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-client-region-zone

創(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

Client Zone1

Client Zone2

由此,可以驗(yàn)證 client1滥沫、client2 的 zone 是指定的 zone侣集。


開啟 Http Basic

現(xiàn)在的實(shí)例中,訪問 Eureka Server 是不需要用戶名兰绣、密碼的履肃,不需要安全驗(yàn)證藏鹊。為了防止微服務(wù)暴露,可以開啟 Http Basic 安全教研。

Eureka Server 開啟 Http Basic

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-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

HTTP Basic

Eureka Client 開啟 Http Basic

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-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 注冊成功

Eureka Client Http Basic
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末烛谊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子嘉汰,更是在濱河造成了極大的恐慌丹禀,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鞋怀,死亡現(xiàn)場離奇詭異双泪,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)密似,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門焙矛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人残腌,你說我怎么就攤上這事村斟。” “怎么了抛猫?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵蟆盹,是天一觀的道長。 經(jīng)常有香客問我邑滨,道長日缨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任掖看,我火速辦了婚禮匣距,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘哎壳。我一直安慰自己毅待,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布归榕。 她就那樣靜靜地躺著尸红,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上外里,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天怎爵,我揣著相機(jī)與錄音,去河邊找鬼盅蝗。 笑死鳖链,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的墩莫。 我是一名探鬼主播芙委,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼狂秦!你這毒婦竟也來了灌侣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤裂问,失蹤者是張志新(化名)和其女友劉穎侧啼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體愕秫,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡慨菱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年焰络,在試婚紗的時候發(fā)現(xiàn)自己被綠了戴甩。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡闪彼,死狀恐怖甜孤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情畏腕,我是刑警寧澤缴川,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站描馅,受9級特大地震影響把夸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜铭污,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一恋日、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嘹狞,春花似錦岂膳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春簸喂,著一層夾襖步出監(jiān)牢的瞬間毙死,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工喻鳄, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留规哲,地道東北人。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓诽表,卻偏偏與公主長得像唉锌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子竿奏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354