日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

hazelcast集群配置_使用HazelCast进行Hibernate缓存:基本配置

發(fā)布時(shí)間:2023/12/3 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hazelcast集群配置_使用HazelCast进行Hibernate缓存:基本配置 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

hazelcast集群配置

之前,我們對(duì)JPA緩存,機(jī)制以及hibernate提供的內(nèi)容進(jìn)行了介紹 。

接下來(lái)是一個(gè)使用Hazelcast作為二級(jí)緩存的Hibernate項(xiàng)目。

為此,我們將在JPA中使用一個(gè)基本的spring boot項(xiàng)目。 Spring Boot使用Hibernate作為默認(rèn)的JPA提供程序。
我們的設(shè)置將非常接近上一篇文章 。
我們將PostgreSQL與PostgreSQL一起用于我們的sql數(shù)據(jù)庫(kù)。

group 'com.gkatzioura' version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")} }apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot'repositories {mavenCentral() }dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'compile group: 'org.postgresql', name:'postgresql', version:'9.4-1206-jdbc42'compile group: 'org.springframework', name: 'spring-jdbc'compile group: 'com.zaxxer', name: 'HikariCP', version: '2.6.0'compile group: 'com.hazelcast', name: 'hazelcast-hibernate5', version: '1.2'compile group: 'com.hazelcast', name: 'hazelcast', version: '3.7.5'testCompile group: 'junit', name: 'junit', version: '4.11' }

通過(guò)仔細(xì)檢查依賴關(guān)系,我們可以看到hikari池,postgresql驅(qū)動(dòng)程序,spring數(shù)據(jù)jpa,當(dāng)然還有hazelcast。

無(wú)需手動(dòng)創(chuàng)建數(shù)據(jù)庫(kù),我們將利用Spring Boot的數(shù)據(jù)庫(kù)初始化功能將其自動(dòng)化。

我們將在resources文件夾下創(chuàng)建一個(gè)名為schema.sql的文件。

create schema spring_data_jpa_example;create table spring_data_jpa_example.employee(id SERIAL PRIMARY KEY,firstname TEXT NOT NULL,lastname TEXT NOT NULL, email TEXT not null,age INT NOT NULL,salary real,unique(email) );insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('Test','Me','test@me.com',18,3000.23);

為了簡(jiǎn)單起見(jiàn),避免進(jìn)行任何其他配置,我們將把數(shù)據(jù)源,jpa和緩存的配置放在application.yml文件中。

spring:datasource:continue-on-error: truetype: com.zaxxer.hikari.HikariDataSourceurl: jdbc:postgresql://172.17.0.2:5432/postgresdriver-class-name: org.postgresql.Driverusername: postgrespassword: postgreshikari:idle-timeout: 10000jpa:properties:hibernate:cache:use_second_level_cache: trueuse_query_cache: trueregion:factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactoryshow-sql: true

配置spring.datasource.continue-on-error至關(guān)重要,因?yàn)橐坏?yīng)用程序重新啟動(dòng),就應(yīng)該再次嘗試創(chuàng)建數(shù)據(jù)庫(kù),因此崩潰是不可避免的。

任何Hibernate特定的屬性都駐留在spring.jpa.properties路徑中。 我們啟用了二級(jí)緩存和查詢緩存。

另外,我們將show-sql設(shè)置為true。 這意味著一旦查詢命中數(shù)據(jù)庫(kù),就應(yīng)通過(guò)控制臺(tái)進(jìn)行記錄。

然后創(chuàng)建我們的員工實(shí)體。

package com.gkatzioura.hibernate.enitites;import javax.persistence.*;/*** Created by gkatzioura on 2/6/17.*/ @Entity @Table(name = "employee", schema="spring_data_jpa_example") public class Employee {@Id@Column(name = "id")@GeneratedValue(strategy = GenerationType.SEQUENCE)private Long id;@Column(name = "firstname")private String firstName;@Column(name = "lastname")private String lastname;@Column(name = "email")private String email;@Column(name = "age")private Integer age;@Column(name = "salary")private Integer salary;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSalary() {return salary;}public void setSalary(Integer salary) {this.salary = salary;} }

一切都已設(shè)置。 Spring Boot將檢測(cè)到該實(shí)體并自行創(chuàng)建EntityManagerFactory。 接下來(lái)是員工的存儲(chǔ)庫(kù)類。

package com.gkatzioura.hibernate.repository;import com.gkatzioura.hibernate.enitites.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.CrudRepository;/*** Created by gkatzioura on 2/11/17.*/ public interface EmployeeRepository extends JpaRepository<Employee,Long> { }

最后一個(gè)是控制器

package com.gkatzioura.hibernate.controller;import com.gkatzioura.hibernate.enitites.Employee; import com.gkatzioura.hibernate.repository.EmployeeRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** Created by gkatzioura on 2/6/17.*/ @RestController public class EmployeeController {@Autowiredprivate EmployeeRepository employeeRepository;@RequestMapping("/employee")public List<Employee> testIt() {return employeeRepository.findAll();}@RequestMapping("/employee/{employeeId}")public Employee getEmployee(@PathVariable Long employeeId) {return employeeRepository.findOne(employeeId);}}

一旦我們?cè)趆ttp:// localhost:8080 / employee / 1發(fā)出請(qǐng)求

控制臺(tái)將顯示在數(shù)據(jù)庫(kù)中發(fā)出的查詢

Hibernate: select employee0_.id as id1_0_0_, employee0_.age as age2_0_0_, employee0_.email as email3_0_0_, employee0_.firstname as firstnam4_0_0_, employee0_.lastname as lastname5_0_0_, employee0_.salary as salary6_0_0_ from spring_data_jpa_example.employee employee0_ where employee0_.id=?

第二次發(fā)出請(qǐng)求時(shí),由于啟用了第二個(gè)緩存,因此不會(huì)在數(shù)據(jù)庫(kù)上發(fā)出查詢。 取而代之的是,應(yīng)從第二級(jí)緩存中獲取實(shí)體。

您可以從github下載該項(xiàng)目。

翻譯自: https://www.javacodegeeks.com/2017/02/hibernate-caching-hazelcast-basic-configuration.html

hazelcast集群配置

總結(jié)

以上是生活随笔為你收集整理的hazelcast集群配置_使用HazelCast进行Hibernate缓存:基本配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。