hazelcast集群配置
之前,我们对JPA缓存,机制以及hibernate提供的内容进行了介绍 。
接下来是一个使用Hazelcast作为二级缓存的Hibernate项目。
为此,我们将在JPA中使用一个基本的spring boot项目。 Spring Boot使用Hibernate作为默认的JPA提供程序。
我们的设置将非常接近上一篇文章 。
我们将PostgreSQL与PostgreSQL一起用于我们的sql数据库。
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'
}
通过仔细检查依赖关系,我们可以看到hikari池,postgresql驱动程序,spring数据jpa,当然还有hazelcast。
无需手动创建数据库,我们将利用Spring Boot的数据库初始化功能将其自动化。
我们将在resources文件夹下创建一个名为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);
为了简单起见,避免进行任何其他配置,我们将把数据源,jpa和缓存的配置放在application.yml文件中。
spring:
datasource:
continue-on-error: true
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://172.17.0.2:5432/postgres
driver-class-name: org.postgresql.Driver
username: postgres
password: postgres
hikari:
idle-timeout: 10000
jpa:
properties:
hibernate:
cache:
use_second_level_cache: true
use_query_cache: true
region:
factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory
show-sql: true
配置spring.datasource.continue-on-error至关重要,因为一旦应用程序重新启动,就应该再次尝试创建数据库,因此崩溃是不可避免的。
任何Hibernate特定的属性都驻留在spring.jpa.properties路径中。 我们启用了二级缓存和查询缓存。
另外,我们将show-sql设置为true。 这意味着一旦查询命中数据库,就应通过控制台进行记录。
然后创建我们的员工实体。
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;
}
}
一切都已设置。 Spring Boot将检测到该实体并自行创建EntityManagerFactory。 接下来是员工的存储库类。
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> {
}
最后一个是控制器
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 {
@Autowired
private EmployeeRepository employeeRepository;
@RequestMapping("/employee")
public List<Employee> testIt() {
return employeeRepository.findAll();
}
@RequestMapping("/employee/{employeeId}")
public Employee getEmployee(@PathVariable Long employeeId) {
return employeeRepository.findOne(employeeId);
}
}
一旦我们在http:// localhost:8080 / employee / 1发出请求
控制台将显示在数据库中发出的查询
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=?
第二次发出请求时,由于启用了第二个缓存,因此不会在数据库上发出查询。 取而代之的是,应从第二级缓存中获取实体。
您可以从github下载该项目。
翻译自: https://www.javacodegeeks.com/2017/02/hibernate-caching-hazelcast-basic-configuration.html
hazelcast集群配置