Commit c2306205 by Nepxion

增加读写分离锁

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