SpringCloud中的Eureka集群配置方法
更新時間:2023年09月14日 09:44:57 作者:楓葉梨花
這篇文章主要介紹了SpringCloud中的Eureka集群配置,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
微服務框架中最為重要的就是注冊中心,如果只是單注冊中心,一旦出現問題,容易導致整個微服務環(huán)境不可用,所以建議注冊中心集群。
目前SpringCloud框架中使用Eureka作為注冊中心,本文簡單介紹一下Eureka的集群配置,主要的思路就是相互注冊,形成一組相互注冊的注冊中心,達到高可用的效果。
一般來說集群配置至少需要2臺以上的服務器,這里是采用本機測試,道理是一樣的。
集群配置
依賴配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
注冊中心配置
第一個注冊中心配置
# 服務端口號 server: port: 8100 # 服務名稱必須一樣 spring: application: name: app-eureka eureka: instance: # 注冊中心ip地址(本機地址) hostname: 127.0.0.1 client: service-url: # 注冊地址,注冊到9100的注冊中心,如果是三臺以上,一樣可以在這邊添加 # 主要是實現相互注冊 defaultZone: http://127.0.0.1:9100/eureka/ # 將自己注冊給自己的注冊中心 register-with-eureka: true # 檢索自己服務信息 fetch-registry: false
第二個注冊中心配置
# 服務端口號 server: port: 9100 # 服務名稱必須一樣 spring: application: name: app-eureka eureka: instance: # 注冊中心ip地址(本機地址) hostname: 127.0.0.1 client: service-url: # 注冊地址,注冊到8100的注冊中心,如果是三臺以上,一樣可以在這邊添加 # 主要是實現相互注冊 defaultZone: http://127.0.0.1:8100/eureka/ # 將自己注冊給自己的注冊中心 register-with-eureka: true # 檢索自己服務信息 fetch-registry: false
啟動
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //開啟EurekaServer服務 開啟注冊中心 public class AppEureka { public static void main(String[] args) { SpringApplication.run(AppEureka.class, args); } }
客戶端配置
# 訂單服務的端口號 server: port: 8001 # 服務別名 -- 服務注冊到注冊中心名稱 spring: application: name: app-order eureka: client: service-url: # 當前訂單服務注冊到eureka服務地址,兩個注冊中心都注冊 defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka # 需要將服務注冊到eureka register-with-eureka: true # 需要檢索服務 fetch-registry: true
客戶端啟動
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class AppOrder { public static void main(String[] args) { SpringApplication.run(AppOrder.class, args); } }
總結
以上即是Eureka的集群配置,還是比較簡單易懂的。
到此這篇關于SpringCloud中的Eureka的集群配置的文章就介紹到這了,更多相關SpringCloud Eureka集群配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!