欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot Hazelcast Caching 使用和配置詳解

 更新時間:2018年09月07日 08:59:58   作者:zhongzunfa  
這篇文章主要介紹了Spring Boot Hazelcast Caching 使用和配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文將展示spring boot 結合 Hazelcast 的緩存使用案例。

1. Project Structure

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方法,將緩存后續(xù)調用的結果。
  • @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 將會自動創(chuàng)建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')”)

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring中Controller應用深入理解

    Spring中Controller應用深入理解

    這篇文章主要介紹了Spring項目中的Controller,Spring Controller本身也是一個Spring Bean,只是它多提供了Web能力,只需要造類上提供@Controller注解即可
    2022-12-12
  • SpringCloud微服務之Hystrix組件實現(xiàn)服務熔斷的方法

    SpringCloud微服務之Hystrix組件實現(xiàn)服務熔斷的方法

    微服務架構特點就是多服務,多數據源,支撐系統(tǒng)應用。這樣導致微服務之間存在依賴關系。這篇文章主要介紹了SpringCloud微服務之Hystrix組件實現(xiàn)服務熔斷的方法,需要的朋友可以參考下
    2019-08-08
  • JAVA后臺轉換成樹結構數據返回給前端的實現(xiàn)方法

    JAVA后臺轉換成樹結構數據返回給前端的實現(xiàn)方法

    這篇文章主要介紹了JAVA后臺轉換成樹結構數據返回給前端的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Java并發(fā)編程總結——慎用CAS詳解

    Java并發(fā)編程總結——慎用CAS詳解

    下面小編就為大家?guī)硪黄狫ava并發(fā)編程總結——慎用CAS詳解。小編覺得挺不錯的, 現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • 解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題

    解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題

    這篇文章主要介紹了解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • IDEA強制清除Maven緩存的方法示例

    IDEA強制清除Maven緩存的方法示例

    這篇文章主要介紹了IDEA強制清除Maven緩存的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • 深入理解Java Socket通信

    深入理解Java Socket通信

    本篇文章主要介紹了深入理解Java Socket,Java中的網絡通信是通過Socket實現(xiàn)的,Socket分為ServerSocket和Socket兩大類,有興趣的可以了解一下
    2017-02-02
  • spring ioc的簡單實例及bean的作用域屬性解析

    spring ioc的簡單實例及bean的作用域屬性解析

    這篇文章主要介紹了spring ioc的簡單實例及bean的作用域屬性解析,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • 高并發(fā)下如何避免重復數據產生技巧

    高并發(fā)下如何避免重復數據產生技巧

    這篇文章主要為大家介紹了高并發(fā)下如何避免重復數據的產生技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • Java視頻格式轉化的實現(xiàn)方法

    Java視頻格式轉化的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了Java視頻格式轉化的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02

最新評論