sofastack / sofa-rpc

SOFARPC is a high-performance, high-extensibility, production-level Java RPC framework.
https://www.sofastack.tech/sofa-rpc/docs/Home
Apache License 2.0
3.81k stars 1.17k forks source link

Using the consul registration center, it is not working properly #751

Closed nevenchen closed 4 years ago

nevenchen commented 4 years ago

使用consul注册中心,能够注册: image

但是调用的时候抛出异常: image

调用代码如下: ConsumerConfig<HelloSyncService> consumerConfig = new ConsumerConfig<HelloSyncService>() .setRegistry(registryConfig) .setInterfaceId(HelloSyncService.class.getName()) .setProtocol("bolt"); HelloSyncService helloService = consumerConfig.refer();

sofastack-bot[bot] commented 4 years ago

Hi @nevenchen, we detect non-English characters in the issue. This comment is an auto translation by @sofastack-robot to help other users to understand this issue.

We encourage you to describe your issue in English which is more friendly to other users.

Use the consul registration center to be able to register: image But throw an exception when called: ! image The calling code is as follows: ConsumerConfig <HelloSyncService> consumerConfig = new ConsumerConfig <HelloSyncService> () .setRegistry(registryConfig) .setInterfaceId(HelloSyncService.class.getName()) .setProtocol(&quot;bolt&quot;); HelloSyncService helloService = consumerConfig.refer(); - SOFARPC version: 5.5.5 - JVM version (eg java -version): 1.8 - OS version (eg uname -a): windows

ScienJus commented 4 years ago

@nevenchen 想确认一下,看注册信息,provider 侧应该设置了 consul service name,在这种场景下,consumer 侧也应该设置相同的 service name,但是看示例代码貌似没有设置,可以试试:

consumer.setParameter(ConsulConstants.CONSUL_SERVICE_NAME_KEY, "consul_HelloService")
nevenchen commented 4 years ago

现在Application, consumer name都提供了,依然不行。 服务发布: `public static void main(String[] args) { RegistryConfig registryConfig = new RegistryConfig() .setProtocol("consul") .setAddress("122.112.xxx.93:8500"); RegistryConfig localRegistryConfig = new RegistryConfig() .setProtocol("local") .setFile("D:\workspace_java\ipark-edge-data-export\src\test\resources\localRegistry.reg");

    ServerConfig serverConfig = new ServerConfig()
            .setProtocol("bolt")
            .setPort(12200)
            .setDaemon(false);

    ProviderConfig<HelloSyncService> providerConfig = new ProviderConfig<HelloSyncService>()
            .setRegistry(registryConfig)
            .setRegistry(localRegistryConfig)
            .setInterfaceId(HelloSyncService.class.getName())
            .setUniqueId("HelloService")
            .setRef(new HelloSyncServiceImpl())
            .setApplication(new ApplicationConfig().setAppName("Sofa-Demo"))
            .setParameter("consulServiceName", "HelloService")
            .setServer(serverConfig);

    providerConfig.export();
    SpringApplication.run(IpeDataExportApplication.class, args);
}`

消费: `@Test public void testConsul() { LOGGER.debug("go........."); RegistryConfig registryConfig = new RegistryConfig() .setProtocol("consul") .setAddress("122.112.xxx.93:8500") .setRegister(true); ConsulRegistry registry = (ConsulRegistry) RegistryFactory.getRegistry(registryConfig); registry.init();

    ConsumerConfig<HelloSyncService> consumerConfig = new ConsumerConfig<HelloSyncService>()
            .setRegistry(registryConfig)
            .setInterfaceId(HelloSyncService.class.getName())
            .setProtocol("bolt")
            .setApplication(new ApplicationConfig().setAppName("Sofa-Demo"))
            .setParameter(ConsulConstants.CONSUL_SERVICE_NAME_KEY, "consul_HelloService");
    HelloSyncService helloService = consumerConfig.refer();
    LOGGER.info(helloService.saySync("Hy jude"));

    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}`

依赖: `

org.springframework.boot spring-boot-starter
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>rpc-sofa-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ecwid.consul</groupId>
        <artifactId>consul-api</artifactId>
        <version>1.4.2</version>
    </dependency><dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofa-rpc-all</artifactId>
    <version>5.6.0</version>
</dependency>

    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>bolt</artifactId>
        <version>1.5.2</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>registry-client-all</artifactId>
        <version>5.2.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>`
nevenchen commented 4 years ago

抛出异常如下:

`com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-02306: 没有获得服务[ipark.edge.ipedataexport.api.HelloSyncService:1.0]的调用地址,请检查服务是否已经推送

at com.alipay.sofa.rpc.client.AbstractCluster.noAvailableProviderException(AbstractCluster.java:423)
at com.alipay.sofa.rpc.client.AbstractCluster.select(AbstractCluster.java:358)
at com.alipay.sofa.rpc.client.FailoverCluster.doInvoke(FailoverCluster.java:64)
at com.alipay.sofa.rpc.client.AbstractCluster.invoke(AbstractCluster.java:285)
at com.alipay.sofa.rpc.client.ClientProxyInvoker.invoke(ClientProxyInvoker.java:83)
at ipark.edge.ipedataexport.api.HelloSyncService_proxy_0.saySync(HelloSyncService_proxy_0.java)`
nevenchen commented 4 years ago

@ScienJus

nevenchen commented 4 years ago

两边都要加上下面这个: .setUniqueId("HelloService")

nevenchen commented 4 years ago

又有新异常,现在不论是本地注册,还是consul注册,都能找到地址,但是都报下面这个异常,这是什么原因呢? ` com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-02312: 当前服务[ipark.edge.datachannel.facade.IParkDisDataExporter:1.0:EXPORTER]的地址[bolt://10.0.75.1:12200?serialization=hessian2&source=local,]不可用,或指定的地址不在可用的地址列表中

at com.alipay.sofa.rpc.client.AbstractCluster.unavailableProviderException(AbstractCluster.java:433)
at com.alipay.sofa.rpc.client.AbstractCluster.select(AbstractCluster.java:394)
at com.alipay.sofa.rpc.client.FailoverCluster.doInvoke(FailoverCluster.java:64)
at com.alipay.sofa.rpc.client.AbstractCluster.invoke(AbstractCluster.java:285)
at com.alipay.sofa.rpc.client.ClientProxyInvoker.invoke(ClientProxyInvoker.java:83)
at ipark.edge.datachannel.facade.IParkDisDataExporter_proxy_0.exportData(IParkDisDataExporter_proxy_0.java)
at ipark.edge.datachannel.consul.LocalRegistryTest.testLocalRegistry(LocalRegistryTest.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

`

@ScienJus

ScienJus commented 4 years ago

这个应该和注册中心的实现无关了,可以看下注册中心中 provider 是否是这个 ip,以及在 consumer 侧是否可连通。

nevenchen commented 4 years ago

原因找到了,首先是provider需要export(), 然后就是可能是我setHost有问题,默认不设置就好: image