目錄
1 概述
2 Eureka Server
2.1 建構Server項目
2.2 添加Spring Security認證
3 Eureka Client
3.1 建構Client項目
4 測試
5 Eureka Server 叢集
1 概述
Eureka是一個服務注冊中心,分為Eureka Server和Eureka Client,Server和Client均是SpringBoot應用程式,其中Client即為各個微服務,其需要向Server進行注冊,并發送心跳來維護活躍,下面開始建構一個簡單的Eureka服務注冊環境。
2 Eureka Server
2.1 建構Server項目
通過IDEA的Spring Initializr建立一個包含Eureka Server元件依賴的SpringBoot項目,如下圖所示勾選Eureka Server依賴,填寫項目的相關資訊一路next到finish。
本例建立了一個名為eureka-server的SpringBoot項目,建立成功後,IDEA會自動為我們添加Eureka Server依賴,如下:
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
下面,對項目進行一些配置,啟用Eureka Server的功能。
首先,編寫 application.yml 配置檔案,内容如下:
server:
port: 8000
eureka:
instance:
hostname: 127.0.0.1 # 服務注冊中心IP位址
client:
registerWithEureka: false # 向服務注冊中心注冊自己
fetchRegistry: false # 檢索服務
service-url: # 指定服務注冊中心的位置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
由于配置的是server,不需要将server本身注冊到服務中心,是以需要關閉一些預設為true的配置,最終的eureka注冊位址為:http://127.0.0.1:8000/eureka,這也是client進行服務注冊的請求位址。
然後,修改啟動類,添加 @EnableEurekaServer 注解,如下:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
至此,一個單點的Eureka Server就搭建完成了,在浏覽器輸入localhost:8000,可以看到如下界面:
但是我們會發現,背景界面不需要任何認證即可通路,接下來,我們來為Server添加Spring Security認證。
2.2 添加Spring Security認證
在pom.xml添加如下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
修改 application.yml 配置檔案,啟用Spring Security,如下:
server:
port: 8000
spring:
security:
user:
name: admin
password: admin123
eureka:
instance:
hostname: 127.0.0.1 # 服務注冊中心IP位址
client:
registerWithEureka: false # 是否向服務注冊中心注冊自己
fetchRegistry: false # 是否檢索服務
service-url: # 服務注冊中心的配置内容,指定服務注冊中心的位置
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
需要注意的是,添加Security認證後,注冊位置變成 http://admin:[email protected]:8000/eureka
修改啟動類,添加@EnableWebSecurity注解,如下:
@SpringBootApplication
@EnableEurekaServer
@EnableWebSecurity
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
網上很多的教程均是通過此種方式進行配置的,但這種方式存在一個問題:Client無法向Server注冊服務,錯誤資訊為 Cannot execute request on any known server,具體可以參考SpringCloud下的ISSUE spring boot 2.0,eureka registration failed with spring security
原因為:SpringBoot 2.0版本起,Security中預設啟用了CSRF保護,需要關閉CSRF保護。
建立Security配置類,如下:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.httpBasic();
}
}
這裡需要注意的是,如果直接通過 http.csrf().disable(); 關閉SCRF保護(網上很多方案均是如此),那麼也将關閉其他請求的認證,是以需要為其他的請求配置需要認證。
OK,到這裡,Server的配置就完成了,再次通路背景界面時,浏覽器就會彈出登入認證了。
3 Eureka Client
3.1 建構Client項目
通過IDEA的Spring Initializr建立一個包含Eureka Discovery元件依賴的SpringBoot項目,勾選如下圖所示的Eureka Discovery依賴。Eureka Client即為各個微服務,下面建立一個示例服務eureka-client。
建立成功後,pom.xml依賴如下:
<dependencies>
<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-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
編寫 application.yml 配置檔案如下:
server:
port: 8801
spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://admin:[email protected]:8000/eureka/
修改啟動類,添加@EnableEurekaClient注解,表示這是一個Eureka Client應用,如下所示:
@SpringBootApplication
@EnableEurekaClient
public class UserMsApplication {
public static void main(String[] args) {
SpringApplication.run(UserMsApplication.class, args);
}
}
4 測試
1. 啟動 eureka-server 項目
2. 啟動 eureka-client 項目
3. 通路 eureka-server,localhost:8800,輸入我們配置的使用者名和密碼,就可以看到 eureka-client 已經注冊到server中了
5 Eureka Server 叢集
上面建構的 Eureka Server 是一個單點部署的服務注冊中心,這種方式在服務注冊中心崩潰時會導緻整個系統的癱瘓,通常hi使用叢集化部署的方式來避免這個問題。
Eureka Server 叢集的配置與單點配置大緻相同,唯一的不同之處在于需要在每個 Server 的配置中指定叢集中其他 Server 節點的位址,如下所示:
Server1配置:
server:
port: 8000
eureka:
instance:
hostname: server1
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://server2:8001/eureka/,http://server3:8002/eureka/
Server2配置:
server:
port: 8001
eureka:
instance:
hostname: server2
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://server1:8000/eureka/,http://server3:8002/eureka/
Server3配置:
server:
port: 8002
eureka:
instance:
hostname: server2
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://server1:8000/eureka/,http://server2:8001/eureka/
按照如上配置,分别啟動多個eureka-server執行個體即可啟動叢集化部署。
Client 在指定 Server 注冊位址時,可以指定一個或多個位址,多個位址間用","分隔。
源碼位址:https://github.com/GreedyStar/spring-cloud-demo