• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring Boot Hazelcast Caching 使用和配置详解

java 搞代码 4年前 (2022-01-05) 55次浏览 已收录 0个评论

这篇文章主要介绍了Spring Boot Hazelcast Caching 使用和配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文将展示spring boot 结合 Hazelcast 的缓存使用案例。

1. Project Structure

2. Maven Dependencies

   4.0.0com.zzfspring-boot-hazelcast1.0-SNAPSHOT org.springframework.bootspring-boot-starter-parent2.0.1.RELEASE <!-- spring boot  --> org.springframework.bootspring-boot-starter-cache org.springframework.bootspring-boot-starter-actuator org.springframework.bootspring-boot-starter-web<!-- hazelcast jar --> com.hazelcasthazelcast-all3.10.1   org.springframework.bootspring-boot-maven-plugin

3. Hazelcast Caching Service

通过使用

  • @cachable注释来注释play方法,将缓存后续调用的结果。
  • @CacheEvict(allEntries=true)清除缓存中的所有条目。
  • @CacheConfig(cachenames=”instruments”)注册了带有指定缓存的spring框架缓存注释的所有方法。
 @Service @CacheConfig(cacheNames = "instruments") public class MusicService { private static Logger log = LoggerFactory.getLogger(MusicService.class); @CacheEvict(allEntries = true) public void clearCache(){} // 表示的是属性为 trombone 就进行缓存 @Cacheable(condition = "#instrument.equals('trombone')") public String play(String instrument){ log.info("Executing: " + this.getClass().getSimpleName() + ".play(\"" + instrument + "\");"); return "playing " + instrument + "!"; } }

4. Hazelcast Caching Configuration

如果类路径下存在hazelcast, spring boot 将会自动创建Hazelcast 的实例。

下面将介绍两种加载的方式:

  • 使用java 配置的方式
  • 使用hazelcast.xml XML 的配置

4.1 Hazelcast Java Configuration

 @Configuration public class HazelcastConfiguration { @Bean public Config hazelcastConfig(){ return new Config().setInstanceName("hazelcast-instance") .addMapConfig( new MapConfig() .setName("instruments") .setMaxSizeConfig(new MaxSizeConfig(200, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE)) .setEvictionPolicy(EvictionPolicy.LRU) .setTimeToLiveSeconds(20) ); } }

4.2 Hazelcast XML Configuration

可以使用xml 配置 Hazelcast , 在src/main/resources 添加一个文件hazelcast.xml

spring boot 将会自动注入配置文件, 当然也可以指定路径路径, 使用属性spring.hazelcast.config 配置在yml 或者properties 文件中, 例如下面所示:

   <map name="instruments"> 200LFU20</map>

具体的application.properties 和 application.yml 文件显示

 # application.yml spring: hazelcast: config: classpath:config/hazelcast.xml 
 # application.properties spring.hazelcast.config=classpath:config/hazelcast.xml

5. 访问controller

 @Controller public class HazelcastController { private Logger logger = LoggerFactory.getLogger(HazelcastController.class); @Autowired private MusicService musicService; @Autowired private CacheManager cacheManager; @RequestMapping("/hezelcast") @ResponseBody public void getHazelcast(){ logger.info("Spring Boot Hazelcast Caching Example Configuration"); logger.info("Using cache manager: " + cacheManager.getClass().getName()); // 清空缓存 musicService.clearCache(); // 调用方法 play("trombone"); play("guitar"); play("trombone"); play("guitar"); play("bass"); play("trombone"); } private void play(String instrument){ logger.info("Calling: " + MusicService.class.getSimpleName() + ".play(\"" + instrument + "\");"); musicService.play(instrument); } }

6. Bootstrap Hazelcast Caching Application

使用注解@EnableCaching 开启缓存机制.

 @EnableCaching @SpringBootApplication public class HazelcastApplication{ private Logger logger = LoggerFactory.getLogger(HazelcastApplication.class); public static void main(String[] args) { SpringApplication.run(HazelcastApplication.class, args); } }

7. 访问和解释

2018-06-02 22:15:18.488  INFO 41728 — [nio-8080-exec-4] c.h.i.p.impl.PartitionStateManager       : [192.168.1.1]:5701 [dev] [3.10.1] Initializing cluster partition table arrangement…
2018-06-02 22:15:18.521  INFO 41728 — [nio-8080-exec-4] c.z.s.h.controller.HazelcastController   : Calling: MusicService.play(“trombone”);
2018-06-02 22:15:18.558  INFO 41728 — [nio-8080-exec-4] c.z.s.hazelcast.service.MusicService     : Executing: MusicService.play(“trombone”);
2018-06-02 22:15:18.563  INFO 41728 — [nio-8080-exec-4] c.z.s.h.controller.HazelcastController   : Calling: MusicService.play(“guitar”);
2018-06-02 22:15:18.563  INFO 41728 — [nio-8080-exec-4] c.z.s.hazelcast.service.MusicService     : Executing: MusicService.play(“guitar”);
2018-06-02 22:15:18.563  INFO 41728 — [nio-8080-exec-4] c.z.s.h.controller.HazelcastController   : Calling: MusicService.play(“trombone”);
2018-06-02 22:15:18.564  INFO 41728 — [nio-8080-exec-4] c.z.s.h.controller.HazelcastController   : Calling: MusicService.play(“guitar”);
2018-06-02 22:15:18.565  INFO 41728 — [nio-8080-exec-4] c.z.s.hazelcast.service.MusicService     : Executing: MusicService.play(“guitar”);
2018-06-02 22:15:18.565  INFO 41728 — [nio-8080-exec-4] c.z.s.h.controller.HazelcastController   : Calling: MusicService.play(“bass”);
2018-06-02 22:15:18.565 来源gaodaima#com搞(代@码网 INFO 41728 — [nio-8080-exec-4] c.z.s.hazelcast.service.MusicService     : Executing: MusicService.play(“bass”);
2018-06-02 22:15:18.566  INFO 41728 — [nio-8080-exec-4] c.z.s.h.controller.HazelcastController   : Calling: MusicService.play(“trombone”);

从上面的可以看到 只有trombone , 才会直接访问缓存信息, 正是在MusicService 类中的方法play 上时候注解进行过滤:
@Cacheable(condition = “#instrument.equals(‘trombone’)”)

本文参考地址: https://memorynotfound.com/spring-boot-hazelcast-caching-example-configuration/

以上就是Spring Boot Hazelcast Caching 使用和配置详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring Boot Hazelcast Caching 使用和配置详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址