我正在尝试使用cassandra数据库将shedlock集成到我的春季启动项目中。
但是我在应用程序启动时得到以下错误。
2022-11-18 17:35:29,162 [main] ERRR o.s.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lockProvider'
Caused by: java.lang.NullPointerException: table can not be null
at java.base/java.util.Objects.requireNonNull(Objects.java:233)
at net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider$Configuration.<init>(CassandraLockProvider.java:69)
at net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider$Configuration$Builder.build(CassandraLockProvider.java:157)
应用程序。yml文件
server:
port: 8080
data:
cassandra:
port: ${CASSANDRA_PORT:9042}
username: ${CASSANDRA_USERNAME:cassandra}
password: ${CASSANDRA_PASSWORD:cassandra}
keyspace-name: ${CASSANDRA_KEYSPACE_NAME:my_keyspace}
schema-action: recreate
local-datacenter: ${CASSANDRA_LOCAL_DATACENTER:datacenter123}
contact-points: ${CASSANDRA_HOST:localhost}
配置类
import com.datastax.oss.driver.api.core.CqlSession;
import net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider;
import net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider.Configuration;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@org.springframework.context.annotation.Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "10m")
public class MyAppSchedulerConfiguration {
@Bean
public CassandraLockProvider lockProvider(CqlSession cqlSession) {
return new CassandraLockProvider(Configuration.builder().withCqlSession(cqlSession).build());
}
}
pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<shedlock.version>4.42.0</shedlock.version>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>${shedlock.version}</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-cassandra</artifactId>
<version>${shedlock.version}</version>
</dependency>
Cassandra脚本执行
CREATE KEYSPACE shedlock with replication={'class':'SimpleStrategy', 'replication_factor':1} and durable_writes=true;
CREATE TABLE shedlock.lock (name text PRIMARY KEY, lockUntil timestamp, lockedAt timestamp, lockedBy text);
我已经通过cqlsh验证了锁表是在cassandra中创建的。
在我看来,到目前为止的问题是,CqlSession bean是不可用的,由于某种原因,应该是。
我已经尝试了官方文档页面中提到的配置。
我试过降级springboot和shedlock的版本,但是没有用。**Springboot **版本更改2.5.X(同样的错误)2.2.13.释放(同样的错误)2.3.X(相同错误)
我认为你应该这样指定tableName
@Bean
public CassandraLockProvider lockProvider(CqlSession cqlSession) {
return new CassandraLockProvider(Configuration.builder().withCqlSession(cqlSession).withTableName("lock").build());
}
在使用Configuration builder时,似乎没有设置默认的表名。