??wei_shuo的個人主頁
??wei_shuo的學(xué)習(xí)社區(qū)
??Hello World !
Spring Cloud Eureka:服務(wù)注冊與發(fā)現(xiàn)
Spring Cloud Eureka是Spring Cloud生態(tài)系統(tǒng)中的一個組件,它是用于實現(xiàn)服務(wù)注冊與發(fā)現(xiàn)的服務(wù)治理組件。在微服務(wù)架構(gòu)中,服務(wù)之間存在復(fù)雜的依賴關(guān)系,而Spring Cloud Eureka可以幫助解決服務(wù)之間相互查找和通信的問題
Eureka簡介
Eureka是Netflix開源的服務(wù)發(fā)現(xiàn)組件,用于在分布式系統(tǒng)中實現(xiàn)服務(wù)注冊與發(fā)現(xiàn)。它是Netflix公司在構(gòu)建微服務(wù)架構(gòu)時開發(fā)的核心組件之一,后來成為了Spring Cloud生態(tài)系統(tǒng)中的一部分
Eureka注冊中心搭建
Eureka服務(wù)端搭建
- eureka-server依賴導(dǎo)入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
- 啟動類添加注解
@EnableEurekaServer
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
- 配置文件application.yml配置
server: port: 8001 #指定運行端口 spring: application: name: eureka-server #指定服務(wù)名稱 eureka: instance: hostname: localhost #指定主機地址 client: fetch-registry: false #指定是否要從注冊中心獲取服務(wù)(注冊中心不需要開啟) register-with-eureka: false #指定是否要注冊到注冊中心(注冊中心不需要開啟) server: enable-self-preservation: false #關(guān)閉保護模式
Eureka客戶端搭建
- eureka-client依賴導(dǎo)入
<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
@EnableDiscoveryClient @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
- 配置文件application.yml配置
server: port: 8101 #運行端口號 spring: application: name: eureka-client #服務(wù)名稱 eureka: client: register-with-eureka: true #注冊到Eureka的注冊中心 fetch-registry: true #獲取注冊實例列表 service-url: defaultZone: http://localhost:8001/eureka/ #配置注冊中心地址
Eureka集群搭建
- eureka-sever添加配置文件application-replica1.yml配置第一個注冊中心
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
- 給eureka-sever添加配置文件application-replica2.yml配置第二個注冊中心
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
- 修改Eureka-client,連接到集群
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添加認(rèn)證
- 添加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>
- 添加application.yml配置文件(配置用戶名、密碼)
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
添加 Java 配置WebSecurityConfig
默認(rèn)情況下,Spring Security會開啟CSRF(Cross-Site Request Forgery)保護,這是一種用于防止跨站點請求偽造攻擊的安全機制。當(dāng)你添加了Spring Security依賴到應(yīng)用程序中時,每個POST、PUT、DELETE等修改類請求都需要在請求頭中包含CSRF token才能被服務(wù)器接受
默認(rèn)情況下添加SpringSecurity依賴的應(yīng)用每個請求都需要添加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); } }
?? 結(jié)語:創(chuàng)作不易,如果覺得博主的文章賞心悅目,還請——
點贊
??收藏
??評論
??文章來源:http://www.zghlxwxcb.cn/news/detail-708049.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-708049.html
到了這里,關(guān)于Spring Cloud Eureka:服務(wù)注冊與發(fā)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!