运行时异常:无法找到名为

问题描述 投票:0回答:2

我正在尝试使用 Quarkus Redis 客户端的

quarkus.redis.hosts-provider-name
属性 - https://quarkus.io/guides/redis

我定义了以下类:

package gas.redis;

import io.quarkus.redis.client.RedisHostsProvider;

public class QuoteRedisHostsProvider implements RedisHostsProvider {

    @Override
    public Set<URI> getHosts() {
        System.out.println("In QuoteRedisHostsProvider");
        String redis_url = System.getenv("REDIS_URL");
        URI redisURI = null;
        try {
            redisURI = new URI(redis_url);
        }
        catch(Exception e) {
            e.printStackTrace();
            try {
                redisURI = new URI("redis://localhost:6379");
            } catch (URISyntaxException ignored) {
            }

        }
        
        return Set.of(redisURI);
    }
    
}

我已将其添加到

application.properties
,如下所示:

quarkus.redis.hosts-provider-name=gas.redis.QuoteRedisHostsProvider

但在运行时我遇到以下异常:

Caused by: java.lang.RuntimException: unable to find redis host provider named: gas.redis.QuoteRedisHostsProvider
        at io.quarkus.redis.client.runtime.RedisClientUtil.findProvider(RedisClientUtil.java:137)
        at io.quarkus.redis.client.runtime.RedisClientUtil.buildOptions(RedisClientUtil.java:34)
        at io.quarkus.redis.client.runtime.RedisClientsProducer$1.apply(RedisClientsProducer.java:35)
        at io.quarkus.redis.client.runtime.RedisClientsProducer$1.apply(RedisClientsProducer.java:29)
        at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
        at io.quarkus.redis.client.runtime.RedisClientsProducer.getRedisAPIContainer(RedisClientsProducer.java:29)
        at io.quarkus.redis.client.runtime.RedisClientRecorder.getRedisAPIContainer(RedisClientRecorder.java:87)
        at io.quarkus.redis.client.runtime.RedisClientRecorder$2.get(RedisClientRecorder.java:29)
        at io.quarkus.redis.client.runtime.RedisClientRecorder$2.get(RedisClientRecorder.java:26)
        at io.quarkus.redis.client.RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_Bean.create(RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_Bean.zig:160)
        at io.quarkus.redis.client.RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_Bean.create(RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_Bean.zig:176)
        at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:96)
        at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:29)
        at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:26)
        at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
        at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
        at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
        at io.quarkus.arc.impl.ClientProxies.getApplicationScopedDelegate(ClientProxies.java:17)
        at io.quarkus.redis.client.RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_ClientProxy.arc$delegate(RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_ClientProxy.zig:68)
        at io.quarkus.redis.client.RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_ClientProxy.toString(RedisClient_761b9a6e5f634178e3291b09c1921f229025da0c_Synthetic_ClientProxy.zig:2207)
        at java.base/java.lang.String.valueOf(String.java:2941)
        at java.base/java.lang.StringBuilder.append(StringBuilder.java:168)
        at gas.StockQuote.initializeApp(StockQuote.java:124)
        at gas.StockQuote.getAllCachedQuotes(StockQuote.java:180)
        at gas.StockQuote_Subclass.getAllCachedQuotes$$superforward1(StockQuote_Subclass.zig:410)
        at gas.StockQuote_Subclass$$function$$6.apply(StockQuote_Subclass$$function$$6.zig:24)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
        at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:62)
        at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.monitor(InvocationInterceptor.java:49)
        at io.quarkus.arc.runtime.devconsole.InvocationInterceptor_Bean.intercept(InvocationInterceptor_Bean.zig:521)
        at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
        at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
        at gas.StockQuote_Subclass.getAllCachedQuotes(StockQuote_Subclass.zig:962)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:170)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
        at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:660)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:524)
        at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:474)
        at org.jboss.resteasy.core.ResourceMethodInvoker$$Lambda$1216/0000000000000000.get(Unknown Source)
        at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:476)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:434)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:408)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:69)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:492)

我错过了什么? 如果相反,相同的代码可以工作

host-provider-name
我将通过
quarkus.redis.hosts
属性显式设置主机...

redis quarkus
2个回答
1
投票

好吧,经过一番调查,我发现属性

quarkus.redis.hosts-provider-name
指的是 bean 名称而不是 class 名称,并且实现该接口的类必须是 cdi bean。

因此您需要添加 bean 的范围和名称:

@Named("QuoteRedisHostsProvider")
@ApplicationScoped
public class QuoteRedisHostsProvider implements RedisHostsProvider {
...

然后你只需将 bean 的名称作为属性:

quarkus.redis.hosts-provider-name=QuoteRedisHostsProvider

0
投票

我遇到了与上述相同的问题。就我而言,我使用的是 io.vertx.mutiny.redis.client.Redis 客户端。

正如建议的那样,属性 quarkus.redis.hosts-provider-name 指的是 bean 名称而不是类名称,并且实现该接口的类必须是 cdi bean。

但是在这种情况下正确的注释似乎是

@Identifier("QuoteRedisHostsProvider")

相反

@Identifier("QuoteRedisHostsProvider")
© www.soinside.com 2019 - 2024. All rights reserved.