Spring Boot Hazelcast Caching 使用和配置详解

时间:2022-09-22 16:50:44

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

1. project structure

Spring Boot Hazelcast Caching 使用和配置详解

2. maven dependencies

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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框架缓存注释的所有方法。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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 文件中, 例如下面所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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 文件显示

?
1
2
3
4
# application.yml
spring:
 hazelcast:
  config: classpath:config/hazelcast.xml
?
1
2
# application.properties
spring.hazelcast.config=classpath:config/hazelcast.xml

5. 访问controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@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 开启缓存机制.

?
1
2
3
4
5
6
7
8
9
10
@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')”)

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

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

原文链接:https://blog.csdn.net/zhongzunfa/article/details/80551753