Hazelcast Nearcache 服务器 - 客户端 Spring 启动
Hazelcast Nearcache Server - Client Spring Boot
我们正在我们的应用程序中构建一个新的 Hazelcast 缓存服务器客户端模型。
我们正在使用 Open JDK 14
Spring 启动 - 2.3.2
Hazelcast - 3.12.8
服务器 Hazelcast 配置
@Configuration
public class HazlecastConfiguration{
@Bean
public
HazelcastInstance hazlecastInstance() {
EvictionConfig evictionConfig = new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.NONE)
.setMaximumSizePolicy(MaxSizePolicy.ENTRY_COUNT)
.setSize(5000);
NearCacheConfig nearCacheConfig = new NearCacheConfig()
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setInvalidateOnChange(true)
.setTimeToLiveSeconds(600)
.setEvictionConfig(evictionConfig);
Config config = new Config();
config.getMapConfig("cacheMapName")
.setInMemoryFormat(InMemoryFormat.BINARY)
.setNearCacheConfig(nearCacheConfig);
NetworkConfig network = config.getNetworkConfig();
network.setPortAutoIncrement(true);
network.setPort(14571);
network.setPublicAddress(IPADDRESS+":14571");
config.setNetworkConfig(network);
config.getManagementCenterConfig().setEnabled(true);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().setEnabled(true);
return Hazelcast.newHazelcastInstance(config);
}
}
客户端 Hazel 投射配置
@Configuration
public class HazlecastClientConfig {
@Bean
public HazelcastInstance hazelcastInstance()
{
ClientConfig clientConfig= new ClientConfig();
ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
networkConfig.setAddresses(IPADDRESS);
networkConfig.addAddress(IPADDRESSLIST);
clientConfig.setNetworkConfig(networkConfig);
//clientConfig.getNetworkConfig().setConnectionAttemptLimit(5);
//clientConfig.getNetworkConfig().setConnectionAttemptPeriod((int) 1.8e+6);
EvictionConfig evictionConfig = new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.NONE)
.setMaximumSizePolicy(MaxSizePolicy.ENTRY_COUNT).setSize((int) 1.8e+6);
NearCacheConfig nearCacheConfig = clientConfig.getNearCacheConfig(MAPNAME);
if(nearCacheConfig == null)
{
nearCacheConfig = new NearCacheConfig()
.setName(NEARCACHENAME) .setInMemoryFormat(InMemoryFormat.OBJECT)
.setInvalidateOnChange(true)
.setEvictionConfig(evictionConfig);
}
clientConfig.addNearCacheConfig(nearCacheConfig);
ClientConnectionStrategyConfig connectionStrategyConfig =
clientConfig.getConnectionStrategyConfig();
ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
connectionRetryConfig.setInitialBackoffMillis(10000)
.setMaxBackoffMillis((int) 1.8e+6) .setMultiplier(5) .setJitter(0.2);
connectionRetryConfig.setFailOnMaxBackoff(false);
connectionRetryConfig.setEnabled(true);
clientConfig.setConnectionStrategyConfig(connectionStrategyConfig);
return HazelcastClient.newHazelcastClient(clientConfig);
}
}
要求 - 如果服务器和客户端都已启动且 运行,此配置将完美运行。如果服务器宕机,Near 不工作,客户端也被迫宕机。当服务器关闭时,我们正在丢失我们的交易消息。
我们不想在服务器出现故障时丢失我们的客户端应用程序。并且如果服务器中的数据发生更改,它会自动反映在客户端中,因为 .setInvalidateOnChange(true)
.
我们也尝试了 https://hazelcast.com/blog/non-stop-client-with-near-cache/ 。但是没用。
当我关闭服务器时出现以下异常,Near 缓存不工作。
2020-09-23 13:19:56.841 INFO 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Trying to connect to cluster with name: dev
2020-09-23 13:19:56.841 INFO 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Trying to connect to [MYSERVERIPADDRESS]:14571 as owner member
2020-09-23 13:19:57.005 ERROR 17240 --- [nio-8089-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.hazelcast.client.HazelcastClientOfflineException: Client is offline.] with root cause
com.hazelcast.client.HazelcastClientOfflineException: Client is offline.
at com.hazelcast.client.connection.nio.DefaultClientConnectionStrategy.beforeGetConnection(DefaultClientConnectionStrategy.java:66)~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.checkAllowed(ClientConnectionManagerImpl.java:300) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getConnection(ClientConnectionManagerImpl.java:272) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getOrTriggerConnect(ClientConnectionManagerImpl.java:263) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.SmartClientInvocationService.getOrTriggerConnect(SmartClientInvocationService.java:73) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.SmartClientInvocationService.invokeOnRandomTarget(SmartClientInvocationService.java:58) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.ClientInvocation.invokeOnSelection(ClientInvocation.java:167) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.ClientInvocation.invoke(ClientInvocation.java:146) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.ClientProxy.invoke(ClientProxy.java:251) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.proxy.ClientMapProxy.values(ClientMapProxy.java:1254) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.radial.rfil.XrefUIController.getAllXrefs(XrefUIController.java:37) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
at ------ submitted from ------.(Unknown Source) ~[na:na]
at com.hazelcast.client.spi.impl.ClientInvocationFuture.resolveAndThrowIfException(ClientInvocationFuture.java:96) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.ClientInvocationFuture.resolveAndThrowIfException(ClientInvocationFuture.java:33) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.spi.impl.AbstractInvocationFuture.get(AbstractInvocationFuture.java:155) ~[hazelcast-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.ClientProxy.invoke(ClientProxy.java:252) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.proxy.ClientMapProxy.values(ClientMapProxy.java:1254) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.radial.rfil.XrefUIController.getAllXrefs(XrefUIController.java:37) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Exception during initial connection to [SERVERIPADDRESS]:14571: com.hazelcast.core.HazelcastException: java.net.SocketException: Connection refused: no further information to address /10.140.127.248:14571
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Unable to get live cluster connection, attempt 1.
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Unable to connect to any address for cluster: dev. The following addresses were tried: [[SERVERIP]:14571]
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Could not connect to any cluster, shutting down the client: Unable to connect to any cluster.
2020-09-23 13:19:57.843 INFO 17240 --- [clientShutdown-] com.hazelcast.core.LifecycleService : hz.client_1 [dev] [3.12.8] HazelcastClient 3.12.8 (20200625 - 35a975e) is SHUTTING_DOWN
2020-09-23 13:19:57.845 INFO 17240 --- [clientShutdown-] com.hazelcast.core.LifecycleService : hz.client_1 [dev] [3.12.8] HazelcastClient 3.12.8 (20200625 - 35a975e) is SHUTDOWN
2020-09-23 13:19:57.871 ERROR 17240 --- [nio-8089-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.hazelcast.client.HazelcastClientNotActiveException: Client is not active.] with root cause
com.hazelcast.client.HazelcastClientNotActiveException: Client is not active.
at com.hazelcast.client.impl.clientside.HazelcastClientProxy.getClient(HazelcastClientProxy.java:314) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.impl.clientside.HazelcastClientProxy.getMap(HazelcastClientProxy.java:121) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.radial.rfil.XrefUIController.getAllXrefs(XrefUIController.java:29) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
博客 post 是针对 4.x 系列的。 https://hazelcast.com/blog/non-stop-client-with-near-cache/.
这里是3.12.x系列的完整示例。
我正在向示例中添加内联注释来解释配置和行为。
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
NearCacheConfig clientNearCacheConfig = new NearCacheConfig("test")
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setInvalidateOnChange(false);
clientConfig.addNearCacheConfig(clientNearCacheConfig);
ClientConnectionStrategyConfig connectionStrategyConfig = clientConfig.getConnectionStrategyConfig();
//This is to get HazelcastClientOfflineExceotion when the cluster is down instead of a blocking behavior.
connectionStrategyConfig.setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
//This configuration is to make sure that the client will connect back to members when they are up.
// The client will try to connect fast at first. Then it will try to connect every 10 seconds.
// You can reduce this by making MaxBackoffMillis lower.
connectionRetryConfig.setInitialBackoffMillis(1000)
.setMaxBackoffMillis(10000).setMultiplier(2).setJitter(0.2);
//This is to make sure that client will not close ever.
connectionRetryConfig.setFailOnMaxBackoff(false);
connectionRetryConfig.setEnabled(true);
clientConfig.setConnectionStrategyConfig(connectionStrategyConfig);
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<Object, Object> map = client.getMap("test");
//populate the map with 2000 items
for (int i = 0; i < 2000; i++) {
map.put(i, i);
}
//populates the client nearcache for first 1000 items for demonstration purposes
for (int i = 0; i < 1000; i++) {
map.get(i);
}
//shutting down the cluster to demonstrate near cache works without the cluster
instance.shutdown();
Random random = new Random();
//Returns from nearcache without throwing exception
System.out.println("get a cached entry " + map.get(random.nextInt(1000)));
try {
// Try to get a non cached key, should result with exception without blocking the thread
map.get(10001);
} catch (HazelcastClientOfflineException e) {
//Here you can fall back to plan B when the key is not in the near cache.
System.out.println("Get exception " + e);
}
//Alternatively `getAll(keys)` can be called with all the keys that we are sure in the nearcache
HashSet<Object> objects = new HashSet<>();
for (int i = 0; i < 1000; i++) {
objects.add(i);
}
//Returns from nearcache without throwing exception.
System.out.println("Size should be 1000 : " + map.getAll(objects).size());
请注意,如果您的密钥在集群关闭时尚未在近缓存中,则预计会出现 HazelcastClientOfflineException。因此,您的实现应该主动等待 HazelcastClientOfflineException 并在数据在缓存中不可用时回退到计划 B。
我们正在我们的应用程序中构建一个新的 Hazelcast 缓存服务器客户端模型。
我们正在使用 Open JDK 14 Spring 启动 - 2.3.2 Hazelcast - 3.12.8
服务器 Hazelcast 配置
@Configuration
public class HazlecastConfiguration{
@Bean
public
HazelcastInstance hazlecastInstance() {
EvictionConfig evictionConfig = new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.NONE)
.setMaximumSizePolicy(MaxSizePolicy.ENTRY_COUNT)
.setSize(5000);
NearCacheConfig nearCacheConfig = new NearCacheConfig()
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setInvalidateOnChange(true)
.setTimeToLiveSeconds(600)
.setEvictionConfig(evictionConfig);
Config config = new Config();
config.getMapConfig("cacheMapName")
.setInMemoryFormat(InMemoryFormat.BINARY)
.setNearCacheConfig(nearCacheConfig);
NetworkConfig network = config.getNetworkConfig();
network.setPortAutoIncrement(true);
network.setPort(14571);
network.setPublicAddress(IPADDRESS+":14571");
config.setNetworkConfig(network);
config.getManagementCenterConfig().setEnabled(true);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().setEnabled(true);
return Hazelcast.newHazelcastInstance(config);
}
}
客户端 Hazel 投射配置
@Configuration
public class HazlecastClientConfig {
@Bean
public HazelcastInstance hazelcastInstance()
{
ClientConfig clientConfig= new ClientConfig();
ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
networkConfig.setAddresses(IPADDRESS);
networkConfig.addAddress(IPADDRESSLIST);
clientConfig.setNetworkConfig(networkConfig);
//clientConfig.getNetworkConfig().setConnectionAttemptLimit(5);
//clientConfig.getNetworkConfig().setConnectionAttemptPeriod((int) 1.8e+6);
EvictionConfig evictionConfig = new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.NONE)
.setMaximumSizePolicy(MaxSizePolicy.ENTRY_COUNT).setSize((int) 1.8e+6);
NearCacheConfig nearCacheConfig = clientConfig.getNearCacheConfig(MAPNAME);
if(nearCacheConfig == null)
{
nearCacheConfig = new NearCacheConfig()
.setName(NEARCACHENAME) .setInMemoryFormat(InMemoryFormat.OBJECT)
.setInvalidateOnChange(true)
.setEvictionConfig(evictionConfig);
}
clientConfig.addNearCacheConfig(nearCacheConfig);
ClientConnectionStrategyConfig connectionStrategyConfig =
clientConfig.getConnectionStrategyConfig();
ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
connectionRetryConfig.setInitialBackoffMillis(10000)
.setMaxBackoffMillis((int) 1.8e+6) .setMultiplier(5) .setJitter(0.2);
connectionRetryConfig.setFailOnMaxBackoff(false);
connectionRetryConfig.setEnabled(true);
clientConfig.setConnectionStrategyConfig(connectionStrategyConfig);
return HazelcastClient.newHazelcastClient(clientConfig);
}
}
要求 - 如果服务器和客户端都已启动且 运行,此配置将完美运行。如果服务器宕机,Near 不工作,客户端也被迫宕机。当服务器关闭时,我们正在丢失我们的交易消息。
我们不想在服务器出现故障时丢失我们的客户端应用程序。并且如果服务器中的数据发生更改,它会自动反映在客户端中,因为 .setInvalidateOnChange(true)
.
我们也尝试了 https://hazelcast.com/blog/non-stop-client-with-near-cache/ 。但是没用。
当我关闭服务器时出现以下异常,Near 缓存不工作。
2020-09-23 13:19:56.841 INFO 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Trying to connect to cluster with name: dev
2020-09-23 13:19:56.841 INFO 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Trying to connect to [MYSERVERIPADDRESS]:14571 as owner member
2020-09-23 13:19:57.005 ERROR 17240 --- [nio-8089-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.hazelcast.client.HazelcastClientOfflineException: Client is offline.] with root cause
com.hazelcast.client.HazelcastClientOfflineException: Client is offline.
at com.hazelcast.client.connection.nio.DefaultClientConnectionStrategy.beforeGetConnection(DefaultClientConnectionStrategy.java:66)~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.checkAllowed(ClientConnectionManagerImpl.java:300) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getConnection(ClientConnectionManagerImpl.java:272) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.connection.nio.ClientConnectionManagerImpl.getOrTriggerConnect(ClientConnectionManagerImpl.java:263) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.SmartClientInvocationService.getOrTriggerConnect(SmartClientInvocationService.java:73) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.SmartClientInvocationService.invokeOnRandomTarget(SmartClientInvocationService.java:58) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.ClientInvocation.invokeOnSelection(ClientInvocation.java:167) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.ClientInvocation.invoke(ClientInvocation.java:146) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.ClientProxy.invoke(ClientProxy.java:251) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.proxy.ClientMapProxy.values(ClientMapProxy.java:1254) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.radial.rfil.XrefUIController.getAllXrefs(XrefUIController.java:37) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
at ------ submitted from ------.(Unknown Source) ~[na:na]
at com.hazelcast.client.spi.impl.ClientInvocationFuture.resolveAndThrowIfException(ClientInvocationFuture.java:96) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.impl.ClientInvocationFuture.resolveAndThrowIfException(ClientInvocationFuture.java:33) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.spi.impl.AbstractInvocationFuture.get(AbstractInvocationFuture.java:155) ~[hazelcast-3.12.8.jar:3.12.8]
at com.hazelcast.client.spi.ClientProxy.invoke(ClientProxy.java:252) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.proxy.ClientMapProxy.values(ClientMapProxy.java:1254) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.radial.rfil.XrefUIController.getAllXrefs(XrefUIController.java:37) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Exception during initial connection to [SERVERIPADDRESS]:14571: com.hazelcast.core.HazelcastException: java.net.SocketException: Connection refused: no further information to address /10.140.127.248:14571
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Unable to get live cluster connection, attempt 1.
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Unable to connect to any address for cluster: dev. The following addresses were tried: [[SERVERIP]:14571]
2020-09-23 13:19:57.842 WARN 17240 --- [ient_1.cluster-] c.h.c.c.nio.ClusterConnectorService : hz.client_1 [dev] [3.12.8] Could not connect to any cluster, shutting down the client: Unable to connect to any cluster.
2020-09-23 13:19:57.843 INFO 17240 --- [clientShutdown-] com.hazelcast.core.LifecycleService : hz.client_1 [dev] [3.12.8] HazelcastClient 3.12.8 (20200625 - 35a975e) is SHUTTING_DOWN
2020-09-23 13:19:57.845 INFO 17240 --- [clientShutdown-] com.hazelcast.core.LifecycleService : hz.client_1 [dev] [3.12.8] HazelcastClient 3.12.8 (20200625 - 35a975e) is SHUTDOWN
2020-09-23 13:19:57.871 ERROR 17240 --- [nio-8089-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.hazelcast.client.HazelcastClientNotActiveException: Client is not active.] with root cause
com.hazelcast.client.HazelcastClientNotActiveException: Client is not active.
at com.hazelcast.client.impl.clientside.HazelcastClientProxy.getClient(HazelcastClientProxy.java:314) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.hazelcast.client.impl.clientside.HazelcastClientProxy.getMap(HazelcastClientProxy.java:121) ~[hazelcast-client-3.12.8.jar:3.12.8]
at com.radial.rfil.XrefUIController.getAllXrefs(XrefUIController.java:29) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
博客 post 是针对 4.x 系列的。 https://hazelcast.com/blog/non-stop-client-with-near-cache/.
这里是3.12.x系列的完整示例。 我正在向示例中添加内联注释来解释配置和行为。
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
NearCacheConfig clientNearCacheConfig = new NearCacheConfig("test")
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setInvalidateOnChange(false);
clientConfig.addNearCacheConfig(clientNearCacheConfig);
ClientConnectionStrategyConfig connectionStrategyConfig = clientConfig.getConnectionStrategyConfig();
//This is to get HazelcastClientOfflineExceotion when the cluster is down instead of a blocking behavior.
connectionStrategyConfig.setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
//This configuration is to make sure that the client will connect back to members when they are up.
// The client will try to connect fast at first. Then it will try to connect every 10 seconds.
// You can reduce this by making MaxBackoffMillis lower.
connectionRetryConfig.setInitialBackoffMillis(1000)
.setMaxBackoffMillis(10000).setMultiplier(2).setJitter(0.2);
//This is to make sure that client will not close ever.
connectionRetryConfig.setFailOnMaxBackoff(false);
connectionRetryConfig.setEnabled(true);
clientConfig.setConnectionStrategyConfig(connectionStrategyConfig);
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<Object, Object> map = client.getMap("test");
//populate the map with 2000 items
for (int i = 0; i < 2000; i++) {
map.put(i, i);
}
//populates the client nearcache for first 1000 items for demonstration purposes
for (int i = 0; i < 1000; i++) {
map.get(i);
}
//shutting down the cluster to demonstrate near cache works without the cluster
instance.shutdown();
Random random = new Random();
//Returns from nearcache without throwing exception
System.out.println("get a cached entry " + map.get(random.nextInt(1000)));
try {
// Try to get a non cached key, should result with exception without blocking the thread
map.get(10001);
} catch (HazelcastClientOfflineException e) {
//Here you can fall back to plan B when the key is not in the near cache.
System.out.println("Get exception " + e);
}
//Alternatively `getAll(keys)` can be called with all the keys that we are sure in the nearcache
HashSet<Object> objects = new HashSet<>();
for (int i = 0; i < 1000; i++) {
objects.add(i);
}
//Returns from nearcache without throwing exception.
System.out.println("Size should be 1000 : " + map.getAll(objects).size());
请注意,如果您的密钥在集群关闭时尚未在近缓存中,则预计会出现 HazelcastClientOfflineException。因此,您的实现应该主动等待 HazelcastClientOfflineException 并在数据在缓存中不可用时回退到计划 B。