Hazelcast近缓存服务器 – 客户端Spring Boot

huangapple go评论73阅读模式
英文:

Hazelcast Nearcache Server - Client Spring Boot

问题

以下是翻译好的部分:

我们正在我们的应用程序中构建一个新的Hazelcast缓存的Server Client模型。

我们使用的是Open JDK 14、Spring Boot 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);
    }
}

客户端Hazelcast配置

@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);
        
        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 Cache无法工作,并且客户端也被迫关闭。当服务器关闭时,我们丢失了事务消息。

我们不希望在服务器关闭时失去客户端应用程序。并且如果服务器中的数据发生更改,则由于.setInvalidateOnChange(true),客户端会自动反映这些更改。

我们还尝试过https://hazelcast.com/blog/non-stop-client-with-near-cache/,但没有成功。

当我关闭服务器时,我会得到以下异常,Near Cache无法工作。

(异常信息省略,内容过长)

请注意,由于内容较长,部分内容可能被省略。如果您有特定的问题或需要更多信息,请随时告诉我。

英文:

We are building up a new Server Client Model of Hazelcast cache in our application.

We are using Open JDK 14
Spring Boot - 2.3.2
Hazelcast - 3.12.8

Server Hazelcast COnfiguration

@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);
}
}

Client Hazel cast Configuration

@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);
}
}

Requirement - This configuration works perfectly if both server and client were up and running. If Server is down, Near is not working and client also forced to down. We were losing our transaction message when server is down.

We don't want to lose our client application even though the server is down. And If data is changed in server it automatically reflects in client because of .setInvalidateOnChange(true) .

We also tried https://hazelcast.com/blog/non-stop-client-with-near-cache/ . But didn't work.

When I shutdown the server I am getting below exception, Near cache is not working.

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] 

答案1

得分: 2

博客文章针对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();
// 这是为了在集群宕机时获得HazelcastClientOfflineException,而不是阻塞行为。
connectionStrategyConfig.setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
// 此配置确保客户端在成员上线时会重新连接。
// 客户端首先会尝试快速连接,然后每隔10秒尝试连接一次。
// 通过降低MaxBackoffMillis,可以减少连接时间间隔。
connectionRetryConfig.setInitialBackoffMillis(1000)
        .setMaxBackoffMillis(10000).setMultiplier(2).setJitter(0.2);
// 这是为了确保客户端永远不会关闭。
connectionRetryConfig.setFailOnMaxBackoff(false);
connectionRetryConfig.setEnabled(true);
clientConfig.setConnectionStrategyConfig(connectionStrategyConfig);

HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

IMap<Object, Object> map = client.getMap("test");

// 使用2000个条目填充映射
for (int i = 0; i < 2000; i++) {
    map.put(i, i);
}

// 为演示目的填充前1000个条目的客户端近期缓存
for (int i = 0; i < 1000; i++) {
    map.get(i);
}

// 关闭集群以演示近期缓存在没有集群的情况下工作
instance.shutdown();

Random random = new Random();
// 从近期缓存中返回,不会抛出异常
System.out.println("获取缓存条目:" + map.get(random.nextInt(1000)));

try {
    // 尝试获取不在缓存中的键,应该抛出异常,但不会阻塞线程
    map.get(10001);
} catch (HazelcastClientOfflineException e) {
    // 在键不在近期缓存中时,您可以退回到计划B。
    System.out.println("获取异常:" + e);
}

// 或者可以使用`getAll(keys)`调用所有在近期缓存中确实存在的键
HashSet<Object> objects = new HashSet<>();
for (int i = 0; i < 1000; i++) {
    objects.add(i);
}

// 从近期缓存中返回,不会抛出异常。
System.out.println("大小应该为1000:" + map.getAll(objects).size());

请注意,如果在集群宕机时您的键尚未在近期缓存中,则预计会出现HazelcastClientOfflineException。因此,您的实现应该主动等待HazelcastClientOfflineException,并在缓存中没有数据时回退到计划B。

英文:

the blog post is for the 4.x series. https://hazelcast.com/blog/non-stop-client-with-near-cache/ .

Here is a complete example of 3.12.x series.
I am adding comments inline to the example to explain the configurations and the behavior.

    HazelcastInstance instance = Hazelcast.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
NearCacheConfig clientNearCacheConfig = new NearCacheConfig(&quot;test&quot;)
.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&lt;Object, Object&gt; map = client.getMap(&quot;test&quot;);
//populate the map with 2000 items
for (int i = 0; i &lt; 2000; i++) {
map.put(i, i);
}
//populates the client nearcache for first 1000 items for demonstration purposes
for (int i = 0; i &lt; 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(&quot;get a cached entry &quot; + 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(&quot;Get exception  &quot; + e);
}
//Alternatively `getAll(keys)` can be called with all the keys that we are sure in the nearcache
HashSet&lt;Object&gt; objects = new HashSet&lt;&gt;();
for (int i = 0; i &lt; 1000; i++) {
objects.add(i);
}
//Returns from  nearcache without throwing exception.
System.out.println(&quot;Size should be 1000 : &quot; + map.getAll(objects).size());

Note that HazelcastClientOfflineException is expected if your key is not in the near cache yet when the clusters are down. So your implementation should actively wait for HazelcastClientOfflineException and fallback to plan B when the data is not available on the cache.

huangapple
  • 本文由 发表于 2020年9月22日 00:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63996363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定