使用HazelCast實現Spring Config Server配置
我們可以透過RabbitMQ將Spring Cloud Config伺服器的釋出配置設定用於MicroServices節點。這個解決方案的缺點:
- Spring Cloud Config僅支援GIT repo以儲存配置
- 當配置伺服器上的資料發生更改時,您需要手動將事件傳送到所有MicroServices節點
讓我們考慮另一個解決方案,如何刪除Spring Config Server並將配置資料的分發替換為具有HazelCast資料網格的節點。
配置Hazelcast
首先你需要為Hazelcast新增maven依賴:( hazelcast-spring,因為我們將使用Spring快取API和Hazelcast作為CACHE提供者)
<!-- https://mvnrepository.com/artifact/com.hazelcast/hazelcast-spring --> <dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast-spring</artifactId> </dependency> |
然後你需要配置Hazelcast,你有以下選擇:
- 配置com.hazelcast.config.Config 或
- 將hazelcast.xml配置放到classpath中。請參閱此文件 或
- 如果您對hazelcast.xml名稱不滿意,請在檔案中設定-Dhazelcast.config變數
我的配置看起來:
@Bean public Config config() { Config config = new Config(); config.setInstanceName("HazelcastService"); config.setProperty("hazelcast.wait.seconds.before.join","10"); config.getGroupConfig().setName("mygroup"); config.getGroupConfig().setPassword("mypassword"); config.getNetworkConfig().setPortAutoIncrement(true); config.getNetworkConfig().setPort(10555); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true); SSLConfig sslConfig = new SSLConfig(); sslConfig.setEnabled(false); config.getNetworkConfig().setSSLConfig(sslConfig); return config; } |
- 始終使用Hazelcast群組。不要干涉他人。檢查GroupConfig。
- 始終使用埠增量。有時某些環境中的埠可能會被佔用。
如果您計劃將Hazelcast用作分散式快取,那麼您需要使用Hazelcast實現覆蓋cacheManager bean:
@Bean HazelcastInstance hazelcastInstance(){ return Hazelcast.newHazelcastInstance(config()); } @Bean public CacheManager cacheManager(){ return new HazelcastCacheManager(hazelcastInstance()); } |
帶有HazelCast快取的MicroServices
有以下Util服務:
package com.example.hazelcast; import org.springframework.cache.annotation.Cacheable; /** * Created by tomas.kloucek on 18.1.2017. */ public interface IHazelCastUtilService { @Cacheable("batchSize") int getBatchSize(); } |
實現:
public class HazelCastUtilService implements IHazelCastUtilService { @Override public int getBatchSize() { try { System.out.println("Getting batch size from DAO..."); TimeUnit.SECONDS.sleep(5); // (1) } catch (InterruptedException e) { e.printStackTrace(); } return 3; } } |
讓我們在以下的上下文MicroServices之間共享此服務:
Cities MicroService:
@RestController public class CitiesController { final City[] cities = { new City("Brno", "Czech republic"), new City("Bern", "Switzeland"), new City("Berlin", "Germany"), new City("London", "England") }; @Autowired private IHazelCastUtilService hazelCastUtilService; @RequestMapping("/cities") public Cities getCities() { final Cities result = new Cities(); System.out.println("...Getting city from controller!!..."); for (int i=0; i < hazelCastUtilService.getBatchSize();i++) { result.getCities().add(cities[i]); } return result; } } |
Persons MicroService:
@RestController public class PersonsController { @Autowired private IHazelCastUtilService hazelCastUtilService; final Person[] persons = { new Person("Tomas", "Kloucek", "Programmer"), new Person("Linus", "Torvalds", "Linux"), new Person("Heinz", "Kabutz", "Java"), new Person("Jonathan", "Locke", "Wicket") }; @RequestMapping("/persons") public Persons getPersons() { final Persons result = new Persons(); System.out.println("...Getting person from controller!!..."); for (int i=0; i < hazelCastUtilService.getBatchSize();i++) { result.getPersons().add(persons[i]); } return result; } } |
測試演示:
git clone https://bitbucket.org/tomask79/microservice-spring-hazelcast-caching.git mvn clean install (in the root folder with pom.xml) cd spring-microservice-registry java -jar target/registry-0.0.1-SNAPSHOT.war verify that NetFlix Eureka is running at http://localhost:9761 cd .. cd spring-microservice-service1 java -jar target/service1-0.0.1-SNAPSHOT.war verify at http://localhost:9761 that citiesService has been registered cd .. cd spring-microservice-service2 java -jar target/service2-0.0.1-SNAPSHOT.war verify at http://localhost:9761 that personsService has been registered |
為了確保兩個 MicroServices形成Hazelcast叢集,你需要看到類似的東西:
Members [2] { Member [10.130.48.104]:10555 this Member [10.130.48.104]:10556 } |
訪問: http://localhost:8081/cities
看到下面輸出:
...Getting city from controller!!... Getting batch size from DAO... |
訪問http://localhost:8082/persons呼叫第二個微服務:
...Getting person from controller!!... |
因為之前已經由城市MicroService快取了“batchSize”設定。太可愛了!
相關文章
- Spring Cloud Config 實現配置中心SpringCloud
- 使用Hazelcast排程Spring tasksASTSpring
- 在Kubernetes上使用Spring Boot實現Hazelcast分散式快取 – PiotrSpring BootAST分散式快取
- spring-cloud-config-serverSpringCloudServer
- Spring Cloud Config 配置中心SpringCloud
- Spring Cloud Config 實現配置中心,看這一篇就夠了SpringCloud
- Spring Boot 參考指南(Hazelcast)Spring BootAST
- Spring Boot整合Hazelcast實現叢集與分散式記憶體快取Spring BootAST分散式記憶體快取
- 非spring boot (即spring) 使用/整合 Spring cloud Config 分散式配置中心Spring BootCloud分散式
- Spring Cloud實戰系列(六) - 分散式配置中心Spring Cloud ConfigSpringCloud分散式
- spring-cloud-config-server——Environment RepositorySpringCloudServer
- 分散式配置中心spingcloud-config-server分散式GCCloudServer
- spring cloud:config-server中@RefreshScope的"陷阱"SpringCloudServer
- 使用Spring Boot實現資料庫整合配置案例Spring Boot資料庫
- Spring Authorization Server 實現授權中心SpringServer
- Hazelcast JET在Spring Boot上執行ASTSpring Boot
- spring-cloud-config-server——Environment Repository(Git Backend)SpringCloudServerGit
- 使用Spring Integration和Hazelcast進行叢集領導者選舉SpringAST
- 使用Hazelcast作為Spring資料儲存庫的開源案例ASTSpring
- Spring如何實現可插拔配置?Spring
- 配置PXE Server實現網路啟動Server
- spring-cloud-config-server——Environment Repository(File System Backend)SpringCloudServer
- [Spring Cloud Tutorial翻譯系列二]Spring Cloud Config Server與git整合SpringCloudServerGit
- 使用react-router-config配置路由React路由
- Spring Cloud Config客戶端使用SpringCloud客戶端
- Spring Cloud Config—客戶端使用SpringCloud客戶端
- spring data mongodb配置+月庫實現SpringMongoDB
- Spring Cloud Config 分散式配置中心【Finchley 版】SpringCloud分散式
- spring boot使用logback實現多環境日誌配置Spring Boot
- Spring Cloud(八)高可用的分散式配置中心 Spring Cloud ConfigSpringCloud分散式
- Consul Config 使用Git做版本控制的實現Git
- 使用配置中心config client,不拉取配置問題client
- iGO實現之路 —— ConfigGo
- 微服務SpringCloud之Spring Cloud Config配置中心Git微服務SpringGCCloudGit
- spring cloud config將配置儲存在資料庫中SpringCloud資料庫
- .NET Core微服務之基於Steeltoe使用Spring Cloud Config統一管理配置微服務SpringCloud
- 使用Spring Boot實現模組化Spring Boot
- Spring cloud config 使用gitHub或者gitee連線SpringCloudGithubGitee