欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot Hazelcast Caching 使用和配置详解

程序员文章站 2024-02-24 14:39:16
本文将展示spring boot 结合 hazelcast 的缓存使用案例。 1. project structure 2. maven dependencie...

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

1. project structure

Spring Boot Hazelcast Caching 使用和配置详解

2. maven dependencies

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>

  <groupid>com.zzf</groupid>
  <artifactid>spring-boot-hazelcast</artifactid>
  <version>1.0-snapshot</version>

  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.1.release</version>
  </parent>

  <dependencies>

    <!-- spring boot  -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-cache</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-actuator</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>

    <!-- hazelcast jar -->
    <dependency>
      <groupid>com.hazelcast</groupid>
      <artifactid>hazelcast-all</artifactid>
      <version>3.10.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

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 文件中, 例如下面所示:

<?xml version="1.0" encoding="utf-8"?>
<hazelcast
    xsi:schemalocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config.xsd"
    xmlns="http://www.hazelcast.com/schema/config"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">

  <map name="instruments">
    <max-size>200</max-size>
    <eviction-policy>lfu</eviction-policy>
    <time-to-live-seconds>20</time-to-live-seconds>
  </map>
</hazelcast>

具体的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  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')”)

本文参考地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。