SpringBoot實戰電商項目mall(20k+star)地址: https://github.com/macrozheng/mall
Spring Cloud Eureka是Spring Cloud Netflix 子項目的核心組件之一,主要用於微服務架構中的服務治理。
本文將對搭建Eureka註冊中心,搭建Eureka客戶端,搭建Eureka集羣及給Eureka註冊中心添加登陸認證進行介紹。java
在微服務架構中每每會有一個註冊中心,每一個微服務都會向註冊中心去註冊本身的地址及端口信息,註冊中心維護着服務名稱與服務實例的對應關係。每一個微服務都會定時從註冊中心獲取服務列表,同時彙報本身的運行狀況,這樣當有的服務須要調用其餘服務時,就能夠從本身獲取到的服務列表中獲取實例地址進行調用,Eureka實現了這套服務註冊與發現機制。git
這裏咱們以建立並運行Eureka註冊中心來看看在IDEA中建立並運行SpringCloud應用的正確姿式。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
server: port: 8001 #指定運行端口 spring: application: name: eureka-server #指定服務名稱 eureka: instance: hostname: localhost #指定主機地址 client: fetch-registry: false #指定是否要從註冊中心獲取服務(註冊中心不須要開啓) register-with-eureka: false #指定是否要註冊到註冊中心(註冊中心不須要開啓) server: enable-self-preservation: false #關閉保護模式
此時服務已經建立完成,點擊啓動類的main方法就能夠運行了。可是在微服務項目中咱們會啓動不少服務,爲了便於管理,咱們使用IDEA的Run Dashboard來啓動。
<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-web</artifactId> </dependency>
@EnableDiscoveryClient @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
server: port: 8101 #運行端口號 spring: application: name: eureka-client #服務名稱 eureka: client: register-with-eureka: true #註冊到Eureka的註冊中心 fetch-registry: true #獲取註冊實例列表 service-url: defaultZone: http://localhost:8001/eureka/ #配置註冊中心地址
因爲全部服務都會註冊到註冊中心去,服務之間的調用都是經過從註冊中心獲取的服務列表來調用,註冊中心一旦宕機,全部服務調用都會出現問題。因此咱們須要多個註冊中心組成集羣來提供服務,下面將搭建一個雙節點的註冊中心集羣。
server: port: 8002 spring: application: name: eureka-server eureka: instance: hostname: replica1 client: serviceUrl: defaultZone: http://replica2:8003/eureka/ #註冊到另外一個Eureka註冊中心 fetch-registry: true register-with-eureka: true
server: port: 8003 spring: application: name: eureka-server eureka: instance: hostname: replica2 client: serviceUrl: defaultZone: http://replica1:8002/eureka/ #註冊到另外一個Eureka註冊中心 fetch-registry: true register-with-eureka: true
這裏咱們經過兩個註冊中心互相註冊,搭建了註冊中心的雙節點集羣,因爲defaultZone使用了域名,因此還需在本機的host文件中配置一下。github
127.0.0.1 replica1 127.0.0.1 replica2
在IDEA中咱們能夠經過使用不一樣的配置文件來啓動同一個SpringBoot應用。
從原啓動配置中複製一個出來
配置啓動的配置文件
添加eureka-client的配置文件application-replica.yml,讓其同時註冊到兩個註冊中心。
server: port: 8102 spring: application: name: eureka-client eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/ #同時註冊到兩個註冊中心
以該配置文件啓動後訪問任意一個註冊中心節點均可以看到eureka-client
須要添加SpringSecurity模塊。
<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>
主要是配置了登陸註冊中心的用戶名和密碼。
server: port: 8004 spring: application: name: eureka-security-server security: #配置SpringSecurity登陸用戶名和密碼 user: name: macro password: 123456 eureka: instance: hostname: localhost client: fetch-registry: false register-with-eureka: false
默認狀況下添加SpringSecurity依賴的應用每一個請求都須要添加CSRF token才能訪問,Eureka客戶端註冊時並不會添加,因此須要配置/eureka/**路徑不須要CSRF token。
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } }
http://${username}:${password}@${hostname}:${port}/eureka/
server: port: 8103 spring: application: name: eureka-client eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://macro:123456@localhost:8004/eureka/
eureka: client: #eureka客戶端配置 register-with-eureka: true #是否將本身註冊到eureka服務端上去 fetch-registry: true #是否獲取eureka服務端上註冊的服務列表 service-url: defaultZone: http://localhost:8001/eureka/ # 指定註冊中心地址 enabled: true # 啓用eureka客戶端 registry-fetch-interval-seconds: 30 #定義去eureka服務端獲取服務列表的時間間隔 instance: #eureka客戶端實例配置 lease-renewal-interval-in-seconds: 30 #定義服務多久去註冊中心續約 lease-expiration-duration-in-seconds: 90 #定義服務多久不去續約認爲服務失效 metadata-map: zone: jiangsu #所在區域 hostname: localhost #服務主機名稱 prefer-ip-address: false #是否優先使用ip來做爲主機名 server: #eureka服務端配置 enable-self-preservation: false #關閉eureka服務端的保護機制
springcloud-learning ├── eureka-server -- eureka註冊中心 ├── eureka-security-server -- 帶登陸認證的eureka註冊中心 └── eureka-client -- eureka客戶端
https://github.com/macrozheng/springcloud-learningweb
mall項目全套學習教程連載中,關注公衆號第一時間獲取。spring