ERR未知命令'GEOADD'来自java spring中的嵌入式redis

时间:2018-05-03 12:11:01

标签: java spring spring-boot redis spring-data-redis

我在Java Spring Framework中进行单元测试,其中代码需要使用GEOADD操作来过滤距离原点25公里内的位置。

实现工作正常(连接到真正的redis'节点)但单元测试(连接到embeded redis)显示以下错误

org.springframework.dao.InvalidDataAccessApiUsageException: ERR unknown command 'GEOADD'; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'GEOADD'

这是我的pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/>
</parent>

...

<dependencies>

...

<dependency>
        <groupId>com.github.kstyrc</groupId>
        <artifactId>embedded-redis</artifactId>
        <version>0.6</version>
        <scope>test</scope>
    </dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>

在junit test中初始化嵌入式redis

public class EmbeddedRedisServer {

    static RedisServer redisServer = null;

    static {
        try {
            redisServer = new RedisServer(6479);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static RedisServer redisServer() throws IOException {
        return redisServer;
    }

}

测试用例

@BeforeClass
public static void init() throws Exception {
    EmbeddedRedisServer.redisServer().start();
    System.setProperty("spring.cloud.bootstrap.enabled", "false");
}

@Test
public void testFindNearByPoint() {
    for (CurrentGeoLocation currentGeoLocation : getCurrentLocationList()) {
        currentGeoLocationService.createOrUpdate(currentGeoLocation);
    }
    List<CurrentGeoLocation> list = nearByDriverService.findNearByPoint(13.392900, 52.491560, null);
    Assert.assertThat(list, hasSize(2));
}

@AfterClass
public static void destroy() throws IOException {
    EmbeddedRedisServer.redisServer().stop();
}

需要测试的方法

public List<CurrentGeoLocation> findNearByPoint(final Double longitude, final Double latitude,
                                                                                  final List<Long> driverIds) {
    redisTemplate.delete(GeoTrackingConstants.GEO_KEY);
    List<CurrentGeoLocation> geoLocationList = new ArrayList<>();
    final GeoOperations<String, CurrentGeoLocation> geoOpt = redisTemplate.opsForSet().getOperations().opsForGeo();
    List<CurrentGeoLocation> currentGeoLocations =(CollectionUtils.isEmpty(driverIds))? currentGeoLocationService.findAll()
                                                                      : currentGeoLocationService.findAll(driverIds);
    for (CurrentGeoLocation currentGeoLocation : currentGeoLocations) {
        Point point = new Point(currentGeoLocation.getLongitude(), currentGeoLocation.getLatitude());
        geoOpt.geoAdd(GeoTrackingConstants.GEO_KEY, point, currentGeoLocation);
    }
    Point originPoint = new Point(longitude, latitude);
    Circle circle = new Circle(originPoint, new Distance(radius, RedisGeoCommands.DistanceUnit.KILOMETERS));
    final GeoResults<RedisGeoCommands.GeoLocation<CurrentGeoLocation>> result =
            (CollectionUtils.isEmpty(driverIds))? geoOpt.geoRadius(GeoTrackingConstants.GEO_KEY, circle)
            :  geoOpt.geoRadius(GeoTrackingConstants.GEO_KEY, circle, RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending());
    List<GeoResult<RedisGeoCommands.GeoLocation<CurrentGeoLocation>>> locations = result.getContent();
    for (GeoResult<RedisGeoCommands.GeoLocation<CurrentGeoLocation>> location : locations) {
        if (location.getContent().getName() != null) {
            geoLocationList.add(location.getContent().getName());
        }
    }
    return geoLocationList;
}

有什么建议吗?谢谢

1 个答案:

答案 0 :(得分:4)

我想也许是因为您的redis版本太低,只有redis 3.2支持地理操作。