# hazelcast 不完全指南
### 实现了同实例缓存共吸共享,没有测试不同实例(IP) 缓存共享,还需要测试 已经找到了可能的配置项
- configuration
```java
@Configuration
@EnableCaching
public class HazelcastConfiguration {
@Bean
public Config hazelCastConfig() {
Config config = new Config("demo");
RestApiConfig restApiConfig = new RestApiConfig();
restApiConfig.setEnabled(true);
MulticastConfig multicastConfig = new MulticastConfig();
multicastConfig.setEnabled(true);
TcpIpConfig tcpIpConfig = new TcpIpConfig();
tcpIpConfig.addMember("localhost");
JoinConfig joinConfig = new JoinConfig();
joinConfig.setTcpIpConfig(tcpIpConfig);
NetworkConfig networkConfig = new NetworkConfig();
networkConfig.setRestApiConfig(restApiConfig);
networkConfig.setPort(5801);
networkConfig.setJoin(joinConfig);
config.setNetworkConfig(networkConfig);
return config;
}
}
```
其中 Config 的构造参数
```java
/**
* @param evictionConfig {@link EvictionConfiguration} for
* requested {@link EvictionPolicyEvaluator} implementation
* @param classLoader the {@link java.lang.ClassLoader} to be
* used while creating custom {@link EvictionPolicyComparator}
* if it is specified in the config
* @return {@link
* EvictionPolicyComparator} instance if it is defined, otherwise
* returns null to indicate there is no comparator defined
*/
public static EvictionPolicyComparator getEvictionPolicyComparator(EvictionConfiguration evictionConfig,
ClassLoader classLoader) {
// 1. First check comparator class name
String evictionPolicyComparatorClassName = evictionConfig.getComparatorClassName();
if (!isNullOrEmpty(evictionPolicyComparatorClassName)) {
try {
return ClassLoaderUtil.newInstance(classLoader, evictionPolicyComparatorClassName);
} catch (Exception e) {
throw rethrow(e);
}
}
// 2. Then check comparator implementation
EvictionPolicyComparator comparator = evictionConfig.getComparator();
if (comparator != null) {
return comparator;
}
// 3. As a last resort, try to pick an out-of-the-box comparator implementation
return pickOutOfTheBoxComparator(evictionConfig.getEvictionPolicy());
}
```
其中如果在EvictionConfig中设置了Compare了在2步骤返回比较器,否则查找默认的规则比较器
```java
/**
* Returns the instance name uniquely identifying the hazelcast instance
* created by this configuration. This name is used in different scenarios,
* such as identifying the hazelcast instance when running multiple
* instances in the same JVM.
*
* @return the hazelcast instance name
*/
```
`` RestApiConfig 为开启 Rest API 可以 call 通RestApi 但是提示没有 Provider,具体功能还需要研究``
其中 MulticastConfig enable 为
```java
/**
* Enables or disables the multicast discovery mechanism
*
* @param enabled {@code true} to enable, {@code false} to disable
* @return the updated MulticastConfig
*/
```
可以通过 MulticastConfig 的注释看到
```java
/**
* Contains the configuration for the multicast discovery mechanism.
* <p>
* With the multicast discovery mechanism Hazelcast allows Hazelcast members to find each other using multicast. So
* Hazelcast members do not need to know concrete addresses of members, they just multicast to everyone listening.
* <p>
* It depends on your environment if multicast is possible or allowed; you need to have a look at the
* TCP/IP cluster: {@link TcpIpConfig}.
*/
```
the multicast discovery need config TCPConfig
![](.README_images/TCPConfigUseage.png)
![](.README_images/JoinConfigUsage.png)
根据上两张图可以看到 TCPConfig 是在JoinConfig里面 而JoinConfig 在NetworkConfig中配置
所以尝试addMember(Member member) 方法 添加localhost
![](.README_images/e33acc9b.png)
![](.README_images/db0824db.png)
![](.README_images/4e62725d.png)
![](.README_images/23a94175.png)
![](.README_images/29034b91.png)
我们可以从上面的查找流程看到写localhost是可以被解析的
hazelcast 会先从 Spring IOC容器中查找 Config 如果没有 还会查找resource下面的 hazelcast.yml or hazelcast.xml ... 因为我们已经注入了IOC所以不需要配置文件
## Testing
注册两个实例(同IP)
![](.README_images/47b46008.png)
![](.README_images/dc67b918.png)
分别启动在8080 和 8081 两个web 监听
![](.README_images/23492fda.png)
可以看到他已经启动了一个实例 我们接下来启动另外一个
![](.README_images/6c9f0916.png)
现在看一看 controller里面的代码
![](.README_images/46fea94d.png)
很常规的一个 耗时 spring cache 的测试方法
![](.README_images/9ff2fd8b.png)
![](.README_images/e2f6bc76.png)
可以看到这里进入了耗时方法 接下来请求8081
![](.README_images/46ea5d96.png)
![](.README_images/222a13ce.png)
并且 8081 没有进入耗时方法,说明 spring cache aop 的 cache manager 右 hazelcast 接管 并且进入判断
其他的 benchmark 还有 不同IP 需要条件测试
没有合适的资源?快使用搜索试试~ 我知道了~
hazelcast-demo:带有hazelcast缓存演示的Spring Boot
共31个文件
png:16个
java:7个
jar:1个
需积分: 40 5 下载量 167 浏览量
2021-04-09
00:21:56
上传
评论
收藏 1.36MB ZIP 举报
温馨提示
hazelcast 不完全指南 实现了同实例缓存共吸共享,没有测试不同实例(IP) 缓存共享,还需要测试 已经找到了可能的配置项 configuration @Configuration @EnableCaching public class HazelcastConfiguration { @Bean public Config hazelCastConfig() { Config config = new Config("demo"); RestApiConfig restApiConfig = new RestApiConfig(); restApiConfig.setEnabled(true); MulticastConfig multicastConfig = new MulticastConfig(
资源详情
资源评论
资源推荐
收起资源包目录
hazelcast-demo-master.zip (31个子文件)
hazelcast-demo-master
mvnw.cmd 6KB
mvnw 10KB
src
test
java
com
bigduu
demo
HazelcastDemoApplicationTests.java 223B
main
resources
application.yml 1B
java
com
bigduu
demo
HazelcastDemoApplication.java 334B
controller
DemoController.java 463B
evictionpolicy
FIFOEvictionPolicyComparator.java 1012B
configration
CacheTopicConfiguration.java 1KB
HazelcastConfiguration.java 1KB
.README_images
e2f6bc76.png 69KB
db0824db.png 96KB
222a13ce.png 169KB
46ea5d96.png 26KB
29034b91.png 44KB
JoinConfigUsage.png 109KB
6c9f0916.png 173KB
23492fda.png 174KB
9ff2fd8b.png 29KB
4e62725d.png 69KB
47b46008.png 63KB
TCPConfigUseage.png 147KB
e33acc9b.png 100KB
23a94175.png 79KB
dc67b918.png 57KB
46fea94d.png 51KB
.mvn
wrapper
MavenWrapperDownloader.java 5KB
maven-wrapper.jar 50KB
maven-wrapper.properties 218B
README.md 5KB
pom.xml 3KB
.gitignore 395B
共 31 条
- 1
三渔
- 粉丝: 23
- 资源: 4544
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0