使用HazelCast实现Spring Config Server配置

栏目: Java · 发布时间: 5年前

内容简介:我们可以通过RabbitMQ将Spring Cloud Config服务器的发布配置设置用于MicroServices节点。这个解决方案的缺点:让我们考虑另一个解决方案,如何删除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:<font><i>//mvnrepository.com/artifact/com.hazelcast/hazelcast-spring --></i></font><font>
<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-spring</artifactId>
</dependency>
</font>

然后你需要配置Hazelcast,你有以下选择:

  • 配置com.hazelcast.config.Config 或
  • 将hazelcast.xml配置放到classpath中。请参阅此 文档  或
  • 如果您对hazelcast.xml名称不满意,请在文件中设置-Dhazelcast.config变量

我的配置看起来:

@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群组。不要干涉他人。检查GroupConfig。
  • 始终使用端口增量。有时某些环境中的端口可能会被占用。

如果您计划将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”设置。太可爱了!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Unreal Engine 4 Scripting with C++ Cookbook

Unreal Engine 4 Scripting with C++ Cookbook

Sherif, William、Stephen Whittle / 2016-10-24

Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to us......一起来看看 《Unreal Engine 4 Scripting with C++ Cookbook》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具