要解决Apache Geode + Spring Boot中成员主机状态可疑的问题,可以按照以下步骤进行操作:
org.springframework.boot
spring-boot-starter-data-geode
spring:
data:
geode:
locators: localhost[10334]
log-level: config
cache:
xml:
location: classpath:cache.xml
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import org.apache.geode.cache.GemFireCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.cache.config.EnableGemfireCaching;
import org.springframework.data.gemfire.cache.GemfireCacheManager;
@Configuration
@EnableGemfireCaching
public class CacheConfig {
@Autowired
private GemFireCache gemFireCache;
@Bean
public GemfireCacheManager cacheManager() {
GemfireCacheManager cacheManager = new GemfireCacheManager();
cacheManager.setCache(gemFireCache);
return cacheManager;
}
}
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class CacheService {
@Cacheable("myCache")
public String getCachedData(String key) {
// 从数据库或其他数据源获取数据并返回
return "Cached Data";
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CacheController {
@Autowired
private CacheService cacheService;
@GetMapping("/cache/{key}")
public String getCachedData(@PathVariable String key) {
return cacheService.getCachedData(key);
}
}
这样,你就可以通过访问http://localhost:8080/cache/{key}
来获取缓存数据了。如果成员主机意外关闭,Apache Geode会自动重新连接,并恢复缓存数据。