0

我遵循了 spring-data couchbase 的教程,并有一个带有单元测试的成功示例项目,用于持久化许多自定义实体,并实现了一系列视图来查询实体。

当使用“默认”存储桶名称且没有密码作为身份验证时,这在本地开发环境和 ci 环境中都可以正常工作。

超越示例,我想使用不同的存储桶并最终使用密码。

当我创建一个新存储桶(名为“test_bucket”)并更新注入 CouchbaseConfig 的属性(扩展 AbstractCouchbaseConfiguration)以使用此新存储桶代替“默认”时,运行单元测试时出现以下异常。

我还尝试向创建脚本添加密码,并将相同的密码(在这两种情况下为“psswd”字符串)添加到 CouchbaseConfig 中使用的属性,但得到下面所示的相同异常。

那么是否可以使用“默认”以外的另一个存储桶(并且不需要授权)以及如何配置密码以在此存储桶上使用?

我已经验证了存储桶和预期的视图已经从 Admin GUI 在 couchbase 中正确创建。

2015-06-09 16:41:40 INFO  ClasspathLoggingApplicationListener:55 - Application failed to start with classpath: [file:/C:/tools/cmd/cygwin64/home/akirby/workspaces/repos/blackjack/persistence/target/surefire/surefirebooter7615727324811258159.jar]
2015-06-09 16:41:40 INFO  AutoConfigurationReportLoggingInitializer:107 -

Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)

2015-06-09 16:41:40 ERROR SpringApplication:338 - Application startup failed
java.lang.NoSuchMethodError:     org.apache.commons.codec.binary.Base64.encodeBase64String([B)Ljava/lang/String;
    at com.couchbase.client.http.HttpUtil.buildAuthHeader(HttpUtil.java:55)
    at com.couchbase.client.ViewConnection.addOp(ViewConnection.java:205)
    at com.couchbase.client.CouchbaseClient.addOp(CouchbaseClient.java:803)
    at com.couchbase.client.CouchbaseClient.asyncGetView(CouchbaseClient.java:342)
    at com.couchbase.client.CouchbaseClient.getView(CouchbaseClient.java:430)
    at org.springframework.data.couchbase.core.CouchbaseTemplate$2.doInBucket(CouchbaseTemplate.java:223)
    at org.springframework.data.couchbase.core.CouchbaseTemplate$2.doInBucket(CouchbaseTemplate.java:220)
    at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:244)
    at org.springframework.data.couchbase.core.CouchbaseTemplate.queryView(CouchbaseTemplate.java:220)
    at org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.deleteAll(SimpleCouchbaseRepository.java:168)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy50.deleteAll(Unknown Source)
    at com.pubtech.cms.persistence.RepositoryService.doWork(RepositoryService.java:47)
    at com.pubtech.cms.persistence.ApplicationRepository.lambda$commandLineRunner$0(ApplicationRepository.java:83)
    at com.pubtech.cms.persistence.ApplicationRepository$$Lambda$9/594916129.run(Unknown Source)
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:101)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:170)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:110)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
2015-06-09 16:41:40 INFO  GenericWebApplicationContext:862 - Closing org.springframework.web.context.support.GenericWebApplicationContext@6302bbb1: startup date [Tue Jun 09 16:41:33 BST 2015]; root of context hierarchy
2015-06-09 16:41:40 INFO  CouchbaseConnection:87 - Shut down Couchbase client
2015-06-09 16:41:40 INFO  ViewConnection:87 - I/O reactor terminated

当使用需要密码的存储桶名称(存储桶“t1”,密码“pswd”)时,我在日志中看到此身份验证错误,是否存在某种格式。除了应该对 passsord 进行编码的纯文本吗?

2015-06-10 10:55:58 INFO  DefaultListableBeanFactory:822 - Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-06-10 10:55:59 INFO  Version:27 - HV000001: Hibernate Validator 5.1.3.Final
2015-06-10 10:56:00 ERROR SASLStepOperationImpl:93 - Error:  Auth failure
2015-06-10 10:56:00 WARN  BinaryMemcachedNodeImpl:90 - Discarding partially completed op: SASL steps operation
2015-06-10 10:56:00 WARN  AuthThread:90 - Authentication failed to localhost/127.0.0.1:11210, Status: {OperationStatus success=false:  cancelled}
2015-06-10 10:56:02 WARN  AuthThread:90 - Authentication failed to localhost/127.0.0.1:11210, Status: {OperationStatus success=false:  Invalid arguments}
2015-06-10 10:56:02 WARN  AuthThread:90 - Authentication failed to localhost/127.0.0.1:11210, Status: {OperationStatus success=false:  Invalid arguments}

我使用 couchbase-cli 从脚本创建存储桶,使用相同的脚本创建工作“默认”而不工作“test_bucket”,(使用 mvn 过滤器正确注入属性):

# Create Bucket
couchbase-cli bucket-create -c $COUCHBASE_HOST:$COUCHBASE_PORT -u $CB_REST_USERNAME -p $CB_REST_PASSWORD \
   --bucket=$BUCKET_NAME \
   --bucket-type=couchbase \
   --bucket-ramsize=200 \
   --bucket-replica=1 \
   --wait

CouchbaseConfig 类:

..
@Configuration
@EnableCouchbaseRepositories(basePackages = {"com.persistence.db"})
@EnableAutoConfiguration
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {

@Value("${couchbase.bucket:boris}")
private String bucketName;

@Value("${couchbase.bucket.password:nopwd}")
private String password;

@Value("${couchbase.host:127.0.0.1}")
private String ip;

..
4

1 回答 1

0

我认为您遇到了与我遇到的类似的问题,对我来说,问题是在 @Configuration 类中使用 @Value 有一点特殊要求。如果这很重要,我将 YAML 用于我的属性文件。

将此添加到您的课程中(也必须是静态的)

/**
 * this is required for some reason: https://jira.spring.io/browse/SPR-11773
 * 
 * @return
 */
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
于 2015-06-09T18:09:05.913 回答