我正在尝试在 kubernetes 中使用 ehcache 运行我的 grails 应用程序。 当我在本地系统中使用 2 个容器运行 docker 映像时,它能够在 2 个容器之间复制缓存。 下面是容器1的ehcache心跳接收线程的日志语句:
2019-12-10 16:14:49,776 [Multicast Heartbeat Receiver Thread] DEBUG net.sf.ehcache.distribution.MulticastKeepaliveHeartbeatReceiver - rmiUrls received //172.17.0.2:40001/categories|//172.17.0.2:40001/items
下面是容器2的ehcache心跳接收线程的日志语句:
2019-12-10 16:32:22,200 [Multicast Heartbeat Receiver Thread] DEBUG net.sf.ehcache.distribution.MulticastKeepaliveHeartbeatReceiver - rmiUrls received //172.17.0.3:40001/categories|//172.17.0.3:40001/items
在具有 2 个 Pod 的 Kubernetes 集群中部署相同的应用程序。但应用程序无法在 2 个 pod 之间复制缓存。
我无法在任何 Pod 中找到上述日志语句。它无法找到同行。以下是来自 kubernetes pod 的日志:
2019-12-10 04:25:24,136 [localhost-startStop-1] DEBUG net.sf.ehcache.Cache - Initialised cache: categories
2019-12-10 04:25:24,136 [localhost-startStop-1] DEBUG net.sf.ehcache.distribution.RMIBootstrapCacheLoader - Attempting to acquire cache peers for cache categories to bootstrap from. Will wait up to 10100ms for cache to join cluster.
2019-12-10 04:25:36,048 [Multicast Heartbeat Sender Thread] DEBUG net.sf.ehcache.distribution.PayloadUtil - Cache peers for this CacheManager to be advertised: //192.168.108.40:40001/categories|//192.168.108.40:40001/items
2019-12-10 04:25:46,147 [localhost-startStop-1] DEBUG net.sf.ehcache.distribution.RMIBootstrapCacheLoader - cache peers: []
2019-12-10 04:25:46,147 [localhost-startStop-1] DEBUG net.sf.ehcache.distribution.RMIBootstrapCacheLoader - Empty list of cache peers for cache categories. No cache peer to bootstrap from.
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" defaultTransactionTimeoutInSeconds="15" dynamicConfig="true" maxBytesLocalDisk="0" maxBytesLocalH
<diskStore path="/etc/ehcache/storage" />
<defaultCache
maxElementsInMemory="10000"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cacheManagerPeerProviderFactory class='net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory'
properties="timeToLive=32,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446,peerDiscovery=automatic"
propertySeparator=','
/>
<cacheManagerPeerListenerFactory class='net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory'
properties="port=40001,socketTimeoutMillis=10000"
propertySeparator=','
/>
<cache name="categories"
eternal="true"
overflowToDisk="false"
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class='net.sf.ehcache.distribution.RMICacheReplicatorFactory'
properties="replicateAsynchronously=true"
propertySeparator=','
/>
<bootstrapCacheLoaderFactory class='net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory'
properties="bootstrapAsynchronously=false"
propertySeparator=','
/>
</cache>
<cache name="items"
eternal="true"
overflowToDisk="false"
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class='net.sf.ehcache.distribution.RMICacheReplicatorFactory'
properties="replicateAsynchronously=true"
propertySeparator=','
/>
<bootstrapCacheLoaderFactory class='net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory'
properties="bootstrapAsynchronously=false"
propertySeparator=','
/>
</cache>
</ehcache>
Kubernetes 部署配置:
apiVersion: v1
kind: Service
metadata:
name: "mykart-service"
spec:
ports:
- port: 8080
targetPort: mykart
nodePort: 30074
name: mykart
selector:
app: hushly
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mykart-deployment
spec:
selector:
matchLabels:
app: mykart
replicas: 2
template:
metadata:
labels:
app: mykart
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: nodegroupType
operator: In
values:
- micro-service
volumes:
- name: config-volume
secret:
secretName: mykart-config
containers:
- name: mykart
image: mykart.dkr.ecr.us-east-1.amazonaws.com/mykart:12
volumeMounts:
- name: config-volume
readOnly: true
mountPath: "/etc/mykart-config"
ports:
- name: mykart
containerPort: 8080
由于安全问题,Ehcache 3.0 放弃了对 RMI 的支持。