Commit c2306205 by Nepxion

增加读写分离锁

parent 6029bfbb
......@@ -9,7 +9,7 @@ package com.nepxion.discovery.plugin.config;
* @version 1.0
*/
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.PostConstruct;
......@@ -47,8 +47,8 @@ public class DiscoveryPluginConfig {
}
@Bean
public ReentrantLock reentrantLock() {
return new ReentrantLock();
public ReentrantReadWriteLock reentrantReadWriteLock() {
return new ReentrantReadWriteLock();
}
@Bean
......
......@@ -12,7 +12,7 @@ package com.nepxion.discovery.plugin.config;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
......@@ -37,7 +37,7 @@ public class DiscoveryPluginConfigParser extends Dom4JParser {
private DiscoveryEntity discoveryEntity;
@Autowired
private ReentrantLock reentrantLock;
private ReentrantReadWriteLock reentrantReadWriteLock;
@SuppressWarnings("rawtypes")
@Override
......@@ -70,12 +70,12 @@ public class DiscoveryPluginConfigParser extends Dom4JParser {
}
try {
reentrantLock.lock();
reentrantReadWriteLock.writeLock().lock();
discoveryEntity.setFilterEntity(filterEntity);
discoveryEntity.setVersionEntity(versionEntity);
} finally {
reentrantLock.unlock();
reentrantReadWriteLock.writeLock().unlock();
}
LOG.info("Discovery entity is {}", discoveryEntity);
......
......@@ -10,6 +10,7 @@ package com.nepxion.discovery.plugin.strategy;
*/
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -23,7 +24,13 @@ public class FilterStrategy {
@Autowired
private DiscoveryEntity discoveryEntity;
@Autowired
private ReentrantReadWriteLock reentrantReadWriteLock;
public void apply(String serviceId, String ipAddress) {
try {
reentrantReadWriteLock.readLock().lock();
FilterEntity filterEntity = discoveryEntity.getFilterEntity();
String globalFilterValue = filterEntity.getFilterValue();
validate(globalFilterValue, ipAddress);
......@@ -31,6 +38,9 @@ public class FilterStrategy {
Map<String, String> filterMap = filterEntity.getFilterMap();
String filterValue = filterMap.get(serviceId);
validate(filterValue, ipAddress);
} finally {
reentrantReadWriteLock.readLock().unlock();
}
}
private void validate(String filterValue, String ipAddress) {
......
......@@ -13,7 +13,7 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -29,11 +29,11 @@ public class VersionStrategy {
private DiscoveryEntity discoveryEntity;
@Autowired
private ReentrantLock reentrantLock;
private ReentrantReadWriteLock reentrantReadWriteLock;
public void apply(String consumerServiceId, String providerServiceId, List<ServiceInstance> instances) {
try {
reentrantLock.lock();
reentrantReadWriteLock.readLock().lock();
VersionEntity versionEntity = discoveryEntity.getVersionEntity();
ConsumerEntity consumerEntity = getConsumerEntity(consumerServiceId, versionEntity);
......@@ -52,7 +52,7 @@ public class VersionStrategy {
}
}
} finally {
reentrantLock.unlock();
reentrantReadWriteLock.readLock().unlock();
}
}
......
......@@ -41,7 +41,7 @@ public class DiscoveryPluginConfigSimulator {
InputStream inputStream = getInputStream("src/main/resources/discovery" + index + ".xml");
discoveryPluginConfigPublisher.publish(inputStream);
}
}, 0L, 15000L);
}, 10000L, 15000L);
}
private InputStream getInputStream(String fileName) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment