Spring Data GemFire - 原因:java.lang.IllegalStateException:尚未创建连接池 "DEFAULT"
Spring Data GemFire - Caused by: java.lang.IllegalStateException: The connection pool "DEFAULT" has not been created
我正在尝试在我的项目中设置 spring-data-gemfire。要求很基本:我只需要连接到一个 gemfire 服务器并检索数据。到目前为止,我已经在我的项目中完成了以下配置/数据设置。
build.gradle
dependencies {
implementation (
'org.springframework.boot:spring-boot-starter',
'org.springframework.boot:spring-boot-starter-web',
'org.springframework.boot:spring-boot-starter-actuator',
'org.springframework.boot:spring-boot-starter-data-rest',
'org.springframework.data:spring-data-gemfire:2.1.8.RELEASE',
'org.springframework.boot:spring-boot-starter-data-gemfire:1.5.22.RELEASE',
'com.gemstone.gemfire:gemfire:8.2.0',
'com.gemstone.gemfire:gfSecurityImpl:8.1.0',
'com.dish.mad:gemfire-util:0.0.10',
"io.pivotal.spring.cloud:spring-cloud-services-starter-config-client:$configClientVersion",
"io.springfox:springfox-swagger-ui:$springfoxSwaggerVersion",
"io.springfox:springfox-swagger2:$springfoxSwaggerVersion",
"org.projectlombok:lombok:$lombokVersion"
)
testImplementation (
'org.springframework.boot:spring-boot-starter-test',
"com.jayway.restassured:rest-assured:$restassuredVersion",
"com.jayway.restassured:json-schema-validator:$restassuredVersion",
"org.mock-server:mockserver-integration-testing:$mockserverVersion",
"org.mock-server:mockserver-netty:$mockserverVersion",
"org.hamcrest:hamcrest-library:$hamcrestVersion"
)
annotationProcessor "org.projectlombok:lombok:$lombokVersion"
}
configurations{
implementation.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
}
GemfireConfigProperties.java
@Bean
public GemfireConnector gemfireConnector(GemfireConnectionCreator gemfireConnectionCreator) {
return gemfireConnectionCreator.create(getLocatorList());
}
@Bean
public GemfireConnectionCreator gemfireConnectionCreator(){
return new GemfireConnectionCreator(gemfireUsername,gemfirePassword);
}
@Bean
public GemFireCache gemFireCache(){
return new ClientCacheCreation();
}
private List<Locator> getLocatorList() {
List<String> locators = Arrays.asList(locatorString.split(","));
return locators.stream()
.map(this::parseLocator)
.collect(toList());
}
private Locator parseLocator(String locator) {
String regex = "^(([a-zA-Z0-9.-]+))\[([0-9]+)\]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(locator);
if (!matcher.find()) {
throw new RuntimeException("Invalid format for locator " + locator + ", should be of form <domainName>[<portNumber>]");
}
String host = matcher.group(1);
if (!DomainValidator.getInstance().isValid(host) && !InetAddressValidator.getInstance().isValid(host)) {
throw new RuntimeException("Invalid format for locator " + locator + ", should be of form <domainName>[<portNumber>]");
}
int port = Integer.valueOf(matcher.group(3));
return new Locator(host, port);
}
GemfireConnectionCreator.java
private String userName;
private String password;
private List<Locator> currentLocators;
private GemfireConnector currentConnector;
public GemfireConnectionCreator(String userName, String password) {
this.userName = userName;
this.password = password;
}
public synchronized GemfireConnector create(List<Locator> locators) {
if (currentLocators == null) {
GemfireConnector gemfireConnector = createGemfireConnector(locators);
this.currentLocators = locators;
this.currentConnector = gemfireConnector;
return gemfireConnector;
} else {
if (locators.stream().anyMatch(l -> currentLocators.contains(l))) {
return currentConnector;
} else {
this.currentConnector.close();
GemfireConnector gemfireConnector = createGemfireConnector(locators);
this.currentLocators = locators;
this.currentConnector = gemfireConnector;
return gemfireConnector;
}
}
}
private GemfireConnector createGemfireConnector(List<Locator> locators) {
System.out.println("Create GemfireConnector "+locators);
return new GemfireConnector(locators, this.userName, this.password);
}
GemfireConnector.java
private static ClientCache clientCache;
private static final ConcurrentHashMap<String, Region> regionCache = new ConcurrentHashMap<>();
public GemfireConnector(List<Locator> locators, String username, String password) {
clientCache = getClientCache(locators, username, password);
}
private ClientCache getClientCache(List<Locator> locators, String username, String password) {
System.out.println("Connecting to the following locators: "+ locators);
if (clientCache == null) {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "SpringGemFireCacheClient");
gemfireProperties.setProperty("security-client-auth-init", "templates.security.UserPasswordAuthInit.create");
gemfireProperties.setProperty("security-username", username);
gemfireProperties.setProperty("security-password", password);
ClientCacheFactory clientCacheFactory = new ClientCacheFactory(gemfireProperties);
clientCacheFactory.setPoolFreeConnectionTimeout(1000);
clientCacheFactory.setPoolReadTimeout(1000);
clientCacheFactory.setPoolMinConnections(4);
clientCacheFactory.setPoolRetryAttempts(10);
locators.forEach(locator -> clientCacheFactory.addPoolLocator(locator.getHost(), locator.getPort()));
clientCache = clientCacheFactory.create();
}
return clientCache;
}
public void close() {
clientCache.close();
}
public <K, V> Region getRegionConnection(String regionName) {
if (regionCache.containsKey(regionName)) {
System.out.println(regionName + " found, so reusing the existing instance");
return regionCache.get(regionName);
} else {
ClientRegionFactory<K, V> regionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
Region<K, V> newRegion = regionFactory.create(regionName);
regionCache.put(regionName, newRegion);
System.out.println(regionName + " not found, creating new region " + regionName + " " + newRegion.getName() + " " + newRegion.getAttributes());
return newRegion;
}
}
这一行
regionFactory.create(regionName);
正在抛出此异常:
java.lang.IllegalStateException: The connection pool "DEFAULT" has not been created
你能帮我找出这个错误吗?
原来我将 regionConnector bean 包装在组件而不是配置中。现在已修复。
我正在尝试在我的项目中设置 spring-data-gemfire。要求很基本:我只需要连接到一个 gemfire 服务器并检索数据。到目前为止,我已经在我的项目中完成了以下配置/数据设置。
build.gradle
dependencies {
implementation (
'org.springframework.boot:spring-boot-starter',
'org.springframework.boot:spring-boot-starter-web',
'org.springframework.boot:spring-boot-starter-actuator',
'org.springframework.boot:spring-boot-starter-data-rest',
'org.springframework.data:spring-data-gemfire:2.1.8.RELEASE',
'org.springframework.boot:spring-boot-starter-data-gemfire:1.5.22.RELEASE',
'com.gemstone.gemfire:gemfire:8.2.0',
'com.gemstone.gemfire:gfSecurityImpl:8.1.0',
'com.dish.mad:gemfire-util:0.0.10',
"io.pivotal.spring.cloud:spring-cloud-services-starter-config-client:$configClientVersion",
"io.springfox:springfox-swagger-ui:$springfoxSwaggerVersion",
"io.springfox:springfox-swagger2:$springfoxSwaggerVersion",
"org.projectlombok:lombok:$lombokVersion"
)
testImplementation (
'org.springframework.boot:spring-boot-starter-test',
"com.jayway.restassured:rest-assured:$restassuredVersion",
"com.jayway.restassured:json-schema-validator:$restassuredVersion",
"org.mock-server:mockserver-integration-testing:$mockserverVersion",
"org.mock-server:mockserver-netty:$mockserverVersion",
"org.hamcrest:hamcrest-library:$hamcrestVersion"
)
annotationProcessor "org.projectlombok:lombok:$lombokVersion"
}
configurations{
implementation.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
}
GemfireConfigProperties.java
@Bean
public GemfireConnector gemfireConnector(GemfireConnectionCreator gemfireConnectionCreator) {
return gemfireConnectionCreator.create(getLocatorList());
}
@Bean
public GemfireConnectionCreator gemfireConnectionCreator(){
return new GemfireConnectionCreator(gemfireUsername,gemfirePassword);
}
@Bean
public GemFireCache gemFireCache(){
return new ClientCacheCreation();
}
private List<Locator> getLocatorList() {
List<String> locators = Arrays.asList(locatorString.split(","));
return locators.stream()
.map(this::parseLocator)
.collect(toList());
}
private Locator parseLocator(String locator) {
String regex = "^(([a-zA-Z0-9.-]+))\[([0-9]+)\]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(locator);
if (!matcher.find()) {
throw new RuntimeException("Invalid format for locator " + locator + ", should be of form <domainName>[<portNumber>]");
}
String host = matcher.group(1);
if (!DomainValidator.getInstance().isValid(host) && !InetAddressValidator.getInstance().isValid(host)) {
throw new RuntimeException("Invalid format for locator " + locator + ", should be of form <domainName>[<portNumber>]");
}
int port = Integer.valueOf(matcher.group(3));
return new Locator(host, port);
}
GemfireConnectionCreator.java
private String userName;
private String password;
private List<Locator> currentLocators;
private GemfireConnector currentConnector;
public GemfireConnectionCreator(String userName, String password) {
this.userName = userName;
this.password = password;
}
public synchronized GemfireConnector create(List<Locator> locators) {
if (currentLocators == null) {
GemfireConnector gemfireConnector = createGemfireConnector(locators);
this.currentLocators = locators;
this.currentConnector = gemfireConnector;
return gemfireConnector;
} else {
if (locators.stream().anyMatch(l -> currentLocators.contains(l))) {
return currentConnector;
} else {
this.currentConnector.close();
GemfireConnector gemfireConnector = createGemfireConnector(locators);
this.currentLocators = locators;
this.currentConnector = gemfireConnector;
return gemfireConnector;
}
}
}
private GemfireConnector createGemfireConnector(List<Locator> locators) {
System.out.println("Create GemfireConnector "+locators);
return new GemfireConnector(locators, this.userName, this.password);
}
GemfireConnector.java
private static ClientCache clientCache;
private static final ConcurrentHashMap<String, Region> regionCache = new ConcurrentHashMap<>();
public GemfireConnector(List<Locator> locators, String username, String password) {
clientCache = getClientCache(locators, username, password);
}
private ClientCache getClientCache(List<Locator> locators, String username, String password) {
System.out.println("Connecting to the following locators: "+ locators);
if (clientCache == null) {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "SpringGemFireCacheClient");
gemfireProperties.setProperty("security-client-auth-init", "templates.security.UserPasswordAuthInit.create");
gemfireProperties.setProperty("security-username", username);
gemfireProperties.setProperty("security-password", password);
ClientCacheFactory clientCacheFactory = new ClientCacheFactory(gemfireProperties);
clientCacheFactory.setPoolFreeConnectionTimeout(1000);
clientCacheFactory.setPoolReadTimeout(1000);
clientCacheFactory.setPoolMinConnections(4);
clientCacheFactory.setPoolRetryAttempts(10);
locators.forEach(locator -> clientCacheFactory.addPoolLocator(locator.getHost(), locator.getPort()));
clientCache = clientCacheFactory.create();
}
return clientCache;
}
public void close() {
clientCache.close();
}
public <K, V> Region getRegionConnection(String regionName) {
if (regionCache.containsKey(regionName)) {
System.out.println(regionName + " found, so reusing the existing instance");
return regionCache.get(regionName);
} else {
ClientRegionFactory<K, V> regionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
Region<K, V> newRegion = regionFactory.create(regionName);
regionCache.put(regionName, newRegion);
System.out.println(regionName + " not found, creating new region " + regionName + " " + newRegion.getName() + " " + newRegion.getAttributes());
return newRegion;
}
}
这一行
regionFactory.create(regionName);
正在抛出此异常:
java.lang.IllegalStateException: The connection pool "DEFAULT" has not been created
你能帮我找出这个错误吗?
原来我将 regionConnector bean 包装在组件而不是配置中。现在已修复。