我们可以通过RabbitMQ将Spring Cloud Config服务器的发布配置设置用于MicroServices节点。这个解决方案的缺点:
让我们考虑另一个解决方案,如何删除Spring Config Server并将配置数据的分发替换为具有 HazelCast 数据网格的节点。
配置Hazelcast
首先你需要为Hazelcast添加maven依赖:( hazelcast-spring,因为我们将使用Spring缓存API和Hazelcast作为CACHE提供者)
<!-- https:<font><i>//mvnrepository.com/artifact/com.hazelcast/hazelcast-spring --></i></font><font> <dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast-spring</artifactId> </dependency> </font>
然后你需要配置Hazelcast,你有以下选择:
我的配置看起来:
@Bean <b>public</b> Config config() { Config config = <b>new</b> Config(); config.setInstanceName(<font>"HazelcastService"</font><font>); config.setProperty(</font><font>"hazelcast.wait.seconds.before.join"</font><font>,</font><font>"10"</font><font>); config.getGroupConfig().setName(</font><font>"mygroup"</font><font>); config.getGroupConfig().setPassword(</font><font>"mypassword"</font><font>); config.getNetworkConfig().setPortAutoIncrement(<b>true</b>); config.getNetworkConfig().setPort(10555); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(<b>true</b>); SSLConfig sslConfig = <b>new</b> SSLConfig(); sslConfig.setEnabled(false); config.getNetworkConfig().setSSLConfig(sslConfig); <b>return</b> config; } </font>
如果您计划将Hazelcast用作分布式缓存,那么您需要使用Hazelcast实现覆盖cacheManager bean:
@Bean HazelcastInstance hazelcastInstance(){ <b>return</b> Hazelcast.newHazelcastInstance(config()); } @Bean <b>public</b> CacheManager cacheManager(){ <b>return</b> <b>new</b> HazelcastCacheManager(hazelcastInstance()); }
带有HazelCast缓存的MicroServices
有以下Util服务:
<b>package</b> com.example.hazelcast; <b>import</b> org.springframework.cache.annotation.Cacheable; <font><i>/** * Created by tomas.kloucek on 18.1.2017. */</i></font><font> <b>public</b> <b>interface</b> IHazelCastUtilService { @Cacheable(</font><font>"batchSize"</font><font>) <b>int</b> getBatchSize(); } </font>
实现:
<b>public</b> <b>class</b> HazelCastUtilService implements IHazelCastUtilService { @Override <b>public</b> <b>int</b> getBatchSize() { <b>try</b> { System.out.println(<font>"Getting batch size from DAO..."</font><font>); TimeUnit.SECONDS.sleep(5); </font><font><i>// (1)</i></font><font> } <b>catch</b> (InterruptedException e) { e.printStackTrace(); } <b>return</b> 3; } } </font>
让我们在以下的上下文MicroServices之间共享此服务:
Cities MicroService:
@RestController <b>public</b> <b>class</b> CitiesController { <b>final</b> City[] cities = { <b>new</b> City(<font>"Brno"</font><font>, </font><font>"Czech republic"</font><font>), <b>new</b> City(</font><font>"Bern"</font><font>, </font><font>"Switzeland"</font><font>), <b>new</b> City(</font><font>"Berlin"</font><font>, </font><font>"Germany"</font><font>), <b>new</b> City(</font><font>"London"</font><font>, </font><font>"England"</font><font>) }; @Autowired <b>private</b> IHazelCastUtilService hazelCastUtilService; @RequestMapping(</font><font>"/cities"</font><font>) <b>public</b> Cities getCities() { <b>final</b> Cities result = <b>new</b> Cities(); System.out.println(</font><font>"...Getting city from controller!!..."</font><font>); <b>for</b> (<b>int</b> i=0; i < hazelCastUtilService.getBatchSize();i++) { result.getCities().add(cities[i]); } <b>return</b> result; } } </font>
Persons MicroService:
@RestController <b>public</b> <b>class</b> PersonsController { @Autowired <b>private</b> IHazelCastUtilService hazelCastUtilService; <b>final</b> Person[] persons = { <b>new</b> Person(<font>"Tomas"</font><font>, </font><font>"Kloucek"</font><font>, </font><font>"Programmer"</font><font>), <b>new</b> Person(</font><font>"Linus"</font><font>, </font><font>"Torvalds"</font><font>, </font><font>"Linux"</font><font>), <b>new</b> Person(</font><font>"Heinz"</font><font>, </font><font>"Kabutz"</font><font>, </font><font>"Java"</font><font>), <b>new</b> Person(</font><font>"Jonathan"</font><font>, </font><font>"Locke"</font><font>, </font><font>"Wicket"</font><font>) }; @RequestMapping(</font><font>"/persons"</font><font>) <b>public</b> Persons getPersons() { <b>final</b> Persons result = <b>new</b> Persons(); System.out.println(</font><font>"...Getting person from controller!!..."</font><font>); <b>for</b> (<b>int</b> i=0; i < hazelCastUtilService.getBatchSize();i++) { result.getPersons().add(persons[i]); } <b>return</b> result; } } </font>
测试演示:
git clone https:<font><i>//bitbucket.org/tomask79/microservice-spring-hazelcast-caching.git</i></font><font> 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:</font><font><i>//localhost:9761</i></font><font> cd .. cd spring-microservice-service1 java -jar target/service1-0.0.1-SNAPSHOT.war verify at http:</font><font><i>//localhost:9761 that citiesService has been registered</i></font><font> cd .. cd spring-microservice-service2 java -jar target/service2-0.0.1-SNAPSHOT.war verify at http:</font><font><i>//localhost:9761 that personsService has been registered</i></font><font> </font>
为了确保两个 MicroServices形成Hazelcast集群,你需要看到类似的东西:
Members [2] { Member [10.130.48.104]:10555 <b>this</b> 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”设置。太可爱了!