我使用Kubernetes(GKE)中部署的嵌入式Hazelcast来在Spring Boot应用程序中启用二级缓存休眠。 Spingboot应用程序通过使用spanner-jdbc(https://github.com/olavloite/spanner-jdbc)连接到Spanner。 我向k8s部署了1个服务(副本数= 2),但是位于2个Pod中的hazelcast实例无法稳定连接(服务器启动时可以工作,但之后似乎断开连接,请查看以下日志)。这导致2级缓存无法在2个容器之间共享。任何人都可以帮助这种情况?谢谢。
<groupId>com.hazelcast.poc</groupId>
<artifactId>hazelcast-poc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hazelcast-poc</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>nl.topicus</groupId>
<artifactId>spanner-jdbc</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>nl.topicus</groupId>
<artifactId>spanner-hibernate</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-hibernate5</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-kubernetes</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Springboot Application.java:
public static void main(String[] args) throws IOException {
CloudSpannerDriver.setLogLevel(CloudSpannerDriver.DEBUG);
Writer writer = new FileWriter("jdbc.log", false);
DriverManager.setLogWriter(new PrintWriter(writer));SpringApplication.run(Application.class, args);
}
@Bean
public Config hazelCastConfig(){
Config config = new Config();
config
.addMapConfig(
new MapConfig()
.setName("employees")
.setMaxSizeConfig(new MaxSizeConfig(200, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
.setEvictionPolicy(EvictionPolicy.LRU)
.setTimeToLiveSeconds(20000));
JoinConfig joinConfig = config.getNetworkConfig().getJoin();
joinConfig.getMulticastConfig().setEnabled(false);
joinConfig.getKubernetesConfig().setEnabled(true).setProperty("namespace", "default");
return config;
}
}
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hazelcast-embedded-0
spec:
replicas: 2
selector:
matchLabels:
app: hazelcast-embedded-0
template:
metadata:
labels:
app: hazelcast-embedded-0
spec:
volumes:
- name: hazelcast-key
secret:
secretName: hazelcast-key
containers:
- name: hazelcast-embedded
image: gcr.io/testproject-221915/hazelcast- poc@sha256:5a103d7e2e56c46e9e42617232e472891bda5d121139fe7e41057596bd71bf00
imagePullPolicy: Always
volumeMounts:
- name: hazelcast-key
mountPath: /var/secrets/google
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /var/secrets/google/key.json
ports:
- containerPort: 5701
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: hazelcast-embedded-0
spec:
type: LoadBalancer
selector:
app: hazelcast-embedded-0
ports:
- name: hazelcast
port: 5701
- name: app
port: 8080
扳手数据源文件:
@Bean
public LocalContainerEntityManagerFactoryBean spannerEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(spannerDataSource());
em.setPersistenceUnitName("SpannerPU");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "nl.topicus.hibernate.dialect.CloudSpannerDialect");
properties.put("hibernate.temp.use_jdbc_metadata_defaults", false);
properties.put("hibernate.show_sql", showSql);
properties.put("hibernate.format_sql", formatSql);
properties.put("hibernate.cache.use_query_cache", true);
properties.put("hibernate.cache.use_second_level_cache", true);
properties.put("hibernate.cache.region.factory_class", "com.hazelcast.hibernate.HazelcastCacheRegionFactory");
if("development".equals(profileActive)) {
properties.put("hibernate.generate_statistics", statistic);
properties.put("org.hibernate.stat", hibernateStat);
}
em.setJpaPropertyMap(properties);
return em;
}
Application.properties
spanner.datasource.className=nl.topicus.jdbc.CloudSpannerDriver
spanner.datasource.url=jdbc:cloudspanner://;Project=testproject-
221915;Instance=test-hazelcast;Database=hazelcast
spanner.hibernate.dialect=
nl.topicus.hibernate.dialect.CloudSpannerDialect
spanner.hibernate.show-sql=true
spanner.hibernate.format_sql=true
spanner.datasource.hikari.maximum-pool-size=2 #depend on
microservice
spanner.datasource.hikari.minimum-idle=2
spanner.datasource.hikari.connection-timeout=60000
spring.profiles.active=dev
hibernate.generate_statistics=true
org.hibernate.stat=DEBUG
如您所见,第二个日志只有1个成员,而第一个日志有2个成员。 我通过发出服务的休息请求进行测试,然后将其分发到每个Pod,测试结果有时是在两个Pod之间共享缓存,有时不是。
我希望第二个Hibernate缓存应该在服务中的2个容器之间共享
答案 0 :(得分:1)
您正在创建两个Hazelcast实例
在9:36:44.990,一个kubernetes发现实例
在9:36:53.706的多播发现实例
可能再次成为this
设置hibernate.cache.hazelcast.instance_name
系统属性,并且config.setInstanceName("whatever")
配对应该匹配