Spring Data Hazelcast存储库无法正常工作

时间:2017-09-18 12:39:04

标签: spring spring-boot spring-data hazelcast spring-data-keyvalue

我尝试将我的Springboot(v1.4.2)应用程序与Hazelcast Cluster连接以查找存储的数据。为此,我使用了hazelcast-client实例,并创建了一个HazelcastRepository来搜索我的数据。

问题是我的存储库找不到任何数据。当我使用findAll()进行测试时,它总是返回一个空列表;当我使用findOne()进行测试时,它总是返回null:

以下是我的服务/存储库/域/配置代码:

public void checkMatchingEmojis() {

    final Pattern pattern = Pattern.compile(sEmojiRegex);
    final Matcher matcher = pattern.matcher(sEmojiTest);
    int foundEmojiCount = 0;
    while (matcher.find()) {
        System.out.println("Full match: " + matcher.group(0));
        foundEmojiCount++;
    }
    System.out.println("*******************************************");
    System.out.println("Input Emoji count = 1627");
    System.out.println("Captured Emoji count = " + foundEmojiCount);
    System.out.println("*******************************************");

}

这是我的回购和域名:

@Service
public class MyService {

    private static final Logger log = LoggerFactory.getLogger(MyService .class);

    private final MyHCRepository repository;

    @Autowired
    public MyService (final MyHCRepository repository) {
        this.repository = repository;
    }

    public String get(final String name) {
        List<Element> elements= repository.findByName(name);
        return elements.toString();
    }
}

这是我的hazelcast-client配置:

@Repository
public interface MyHCRepository extends HazelcastRepository<Element, Integer> {
    List<Element> findByName(String name);
}

@Data
@KeySpace
public class Element implements Serializable {

    private Integer id;
    private String name;
}

在我的pom中,我使用了这些依赖项:

@Configuration
@EnableHazelcastRepositories
public class HazelcastConfiguration {

    @Bean
    @Qualifier("client")
    public HazelcastInstance hazelcastClientInstance() {
        final ClientConfig clientConfig = new ClientConfig();
        final ClientNetworkConfig networkConfig = new ClientNetworkConfig();
        networkConfig.setAddresses(singletonList("127.0.0.1"));
        clientConfig.setNetworkConfig(networkConfig);
        return HazelcastClient.newHazelcastClient(clientConfig);
    }

    @Bean
    public KeyValueTemplate keyValueTemplate() {
        return new KeyValueTemplate(new HazelcastKeyValueAdapter(hazelcastClientInstance()));
    }
}

我没有错误,没有例外。这就像我的Hazelcast集群中没有数据,但我知道有。如果我连接并从Hazelcast-Cluster加载地图,请使用我的客户端:

<dependencies>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-client</artifactId>
            <version>3.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>spring-data-hazelcast</artifactId>
            <version>1.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-data-keyvalue</artifactId>
                    <groupId>org.springframework.data</groupId>
                </exclusion>
                <exclusion>
                    <groupId>com.hazelcast</groupId>
                    <artifactId>hazelcast</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <artifactId>spring-data-keyvalue</artifactId>
            <groupId>org.springframework.data</groupId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

然后我得到了我的数据但是如果我尝试使用Hazelcast存储库,我什么都找不到。 有人能告诉我什么是错的吗?

1 个答案:

答案 0 :(得分:1)

一切都很好,我发现了问题。我只需要将我的Map的名称与我的Domain类(Element)中的@Keyspace Annotation相关联 - &gt; @Keyspace(&#34; myMapName&#34)