SpringCloud中整合Eureka實現叢集部署服務註冊與服務提供者
場景
SpringCloud分散式微服務專案搭建構造父子模組依賴與實現服務提供者與消費者示例:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124618737
SpringCloud分散式微服務專案Common通用依賴模組抽離:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124631635
在上面搭建專案結構基礎上,首先是實現了單機模式下整合Eureka。
SpringCloud中整合Eureka實現服務註冊(單機Eureka構建):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124688609
Eureka叢集原理
微服務RPC遠端服務呼叫最核心的是高可用,如果註冊中心只有一個,如果除了故障,會導致整個服務環境不可用。
Eureka叢集的實現原理就是互相註冊,相互守望。
注:
部落格:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取程式設計相關電子書、教程推送與免費下載。
實現
1、參考上面搭建7001Eureka服務註冊中心的過程,再新建一個子模組7002
與7001一樣,pom檔案中新增eureka-server的依賴
<!--eureka-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
新建主啟動類EurekaMain7002並添加註解@EnableEurekaServer
package com.badao.springclouddemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServerpublic class EurekaMain7002 { public static void main(String[] args){ SpringApplication.run(EurekaMain7002.class,args); } }
2、修改hosts檔案
注意與之前單機模式下不同,在配置檔案中配置eureka服務端的例項名稱時如果再一臺電腦下避免出現重複問題,
所以修改C:\Windows\System32\drivers\etc下的hosts檔案
新增如下
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
使本機ip能映射出兩個域名。
3、修改配置檔案application.yml
修改Eureka Server7001的application.yml,配置eureka服務端的例項名稱為eureka7001.com
並且將其service-url下的defaultZone指向其它eureka,這裡將其指向7002
server: port: 7001 eureka: instance: hostname: eureka7001.com #eureka服務端的例項名稱 client: register-with-eureka: false #false表示不向註冊中心註冊自己。 fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務 service-url: #叢集指向其它eureka defaultZone: http://eureka7002.com:7002/eureka/
上面講的叢集搭建就是相互註冊,相互守望,如果是搭建了三個叢集,則1指向2和3,2指向1和3這種類推。
然後新建並修改Eureka Server7002的application.yml,配置其eureka服務端的例項名稱為eureka7002.com
然後將其指向7001
server: port: 7002 eureka: instance: hostname: eureka7002.com #eureka服務端的例項名稱 client: register-with-eureka: false #false表示不向註冊中心註冊自己。 fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務 service-url: #叢集指向其它eureka defaultZone: http://eureka7001.com:7001/eureka/
4、修改服務註冊的服務提供者和服務消費者的註冊方式
前面單機模式下,服務註冊的方式是在application.yml中指向一個Eureka Server,現在叢集模式下,需要將其
修改為註冊到多個Eureka Server中
修改服務提供者8001的配置檔案
eureka: client: #表示是否將自己註冊進EurekaServer預設為true。 register-with-eureka: true #是否從EurekaServer抓取已有的註冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡 fetchRegistry: true service-url: #單機版 #defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
同樣修改服務消費者88的配置檔案
eureka: client: #表示是否將自己註冊進EurekaServer預設為true。 register-with-eureka: true #是否從EurekaServer抓取已有的註冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡 fetchRegistry: true service-url: #單機 #defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
5、測試效果
分別啟動Eureka Server7001和7002,然後啟動服務提供者8001和服務消費者88服務
訪問
可以看到和7002互為守望,服務註冊者和消費者也已經註冊成功。
再訪問
可以看到和7001互為守望,服務註冊者和消費者也已經註冊成功。
6、服務提供者叢集搭建
通過上面實現了Eureka Server 叢集的搭建,那麼如果服務提供者也是隻有一個的話,一旦宕機,也達不到叢集的目的。
所以對於服務提供者同樣也需要進行叢集的搭建。
上面已經建立的服務提供者8001服務,參考其新建過程,複製一個一模一樣只有埠為8002的服務
修改其pom檔案中的依賴與8001一致
<dependencies> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency><!-- 引入自己定義的api通用包,可以使用Payment支付Entity --> <groupId>com.badao</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
新建並修改其yml,從8001複製並修改其埠號
server: port: 8002 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 當前資料來源操作型別 driver-class-name: org.gjt.mm.mysql.Driver # mysql驅動包 url:jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 eureka: client: #表示是否將自己註冊進EurekaServer預設為true。 register-with-eureka: true #是否從EurekaServer抓取已有的註冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡 fetchRegistry: true service-url: #單機版 #defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.badao.springclouddemo.entities # 所有Entity別名類所在包
從8001複製主啟動類並修改名稱為8002
然後其他業務類均從8001複製到8002,使服務提供者具備8001和8002兩個服務
為了後面能演示出消費者呼叫服務提供者時負載均衡的效果,在服務提供者的Controller中加入埠的輸出
7、服務消費者修改負載均衡
之前在服務消費者88服務中呼叫服務提供者服務時,url是固定寫死的,這種只適用於單機模式,顯示服務提供者
已經搭建了叢集,所以不能再寫死服務提供者的地址了,而是要通過服務提供者的別名進行訪問
這個別名可以在Eureka中獲取
而服務的別名的設定是在服務提供者的配置檔案中配置
消費者服務在呼叫服務時要支援負載均衡,需要在RestTemplate的配置中使用@LoadBalanced註解賦予負載均衡的能力。
package com.badao.springclouddemo.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
8、測試叢集搭建效果
啟動7001和7002這兩個Eureka Server,然後啟動8001和8002這兩個服務提供者,然後再啟動88服務消費者
通過服務消費者訪問服務提供者的服務,可以通過返回結果中的埠號看到負載均衡的效果