sofastack / sofa-rpc-boot-projects

SOFABoot projects for SOFARPC, include starter and samples.
https://github.com/sofastack/sofa-rpc
Apache License 2.0
292 stars 117 forks source link

关于sofa-boot配置的疑问 #21

Closed dingzhaoxiang closed 6 years ago

dingzhaoxiang commented 6 years ago

您好,我按照快速入门的教程配置完后,报的 “om.example.demo.HelloSyncService:1.0]的调用地址,请检查服务是否已经推送”'这个错误,反复按照教程配了好几遍,还是不行。。。请问您对这个问题有没有解决的思路或者一点提示?非常感谢~

NeGnail commented 6 years ago

@dingzhaoxiang 你的服务发布和引用的xml配置可以发我看一下吗。

dingzhaoxiang commented 6 years ago

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sofa="http://sofastack.io/schema/sofaboot" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd" default-autowire="byName">

<bean id="helloSyncServiceImpl" class="com.example.demo.HelloSyncServiceImpl"/>
<sofa:service ref="helloSyncServiceImpl" interface="com.example.demo.HelloSyncService">
    <sofa:binding.bolt/>
</sofa:service>

<sofa:reference id="helloSyncServiceReference" interface="com.example.demo.HelloSyncService">
    <sofa:binding.bolt/>
</sofa:reference>

太感谢了!

dingzhaoxiang commented 6 years ago

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sofa="http://sofastack.io/schema/sofaboot" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd" default-autowire="byName">

NeGnail commented 6 years ago

@dingzhaoxiang 这个配置看起来是没有问题的。是否在 Spring Boot 的启动类中使用了 @ImportResource 注解将上面这个xml配置文件引入。另外查看一下日志目录下的/health-check/common-error.log日志是否存在错误信息。

leizhiyuan commented 6 years ago
@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SofaBootRpcSamplesApplication {
    public static void main(String[] args) {
          SpringApplication springApplication = new SpringApplication(SofaBootRpcSamplesApplication.class);
          ApplicationContext applicationContext = springApplication.run(args);          

          HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext
            .getBean("helloSyncServiceReference");

          System.out.println(helloSyncServiceReference.saySync("sync"));
    }
}

文档中这个

@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })

你的 xml 放的路径也是这个么?

dingzhaoxiang commented 6 years ago

是的呢,我把xml放在了resource下,Applicationd的配置也跟您教程中的一样,明天我看下log有没有报错,非常感谢您的耐心解答~

dingzhaoxiang commented 6 years ago

之前试过,如果xml路径不对的话,会报找不到bean的错误

leizhiyuan commented 6 years ago

@dingzhaoxiang 能否将你的代码提交到你的仓库分支上?我来本地看下?

dingzhaoxiang commented 6 years ago

好哒,没问题,明早发给您看下,实验室关门了(。ò ∀ ó。)

dingzhaoxiang commented 6 years ago

您好,我早上又对照log看了一下,还是没看出问题=。=。所以把代码上传了,地址是: https://github.com/dingzhaoxiang/demo.git

麻烦您帮忙指正一下,太感谢了!

leizhiyuan commented 6 years ago

@dingzhaoxiang 你好,我本地运行你这个工程是没有问题的.

20180425094213

你的本地环境是 windows 还是? 包括 jdk 版本, mvn , ide 版本,帮忙帮忙发一下,我这边好复现?

dingzhaoxiang commented 6 years ago

windows,jdk1.8.0_60,mvn:3.5.0,ide:idea 2017.3 在您的环境是能跑的话,那可能是我哪里环境配的不对=。=

leizhiyuan commented 6 years ago

@dingzhaoxiang C:\Users\Administrator\localFileRegistry\localRegistry.reg 你这个文件中的内容能否贴一下?

dingzhaoxiang commented 6 years ago

com.example.demo.HelloSyncService:1.0@rest rest://169.254.0.64:8341?serialization=hessian2&source=local
com.example.demo.HelloSyncService:1.0@DEFAULT bolt://169.254.0.64:22000?serialization=hessian2&source=local

leizhiyuan commented 6 years ago

@dingzhaoxiang 我们得找个 windows 机器来复现下.稍后回复你.

dingzhaoxiang commented 6 years ago

好哒,太感谢了!

NeGnail commented 6 years ago

@dingzhaoxiang 我用 windows 机器以及 jdk1.8 和 mvn3.5.0 来尝试复现你遇到的问题。在我的机器上这个问题会偶然出现。原因是SOFARPC Starter 在启动应用的时候是先对服务进行引用(标志着从此时开始,之后会随时接受注册中心推送的地址建立相应的连接),然后再启动所要发布的服务对应的 server ,这本身是没有问题的。

由于这个示例工程是在同一个工程中对同一个服务进行发布和发起调用,所以会有可能出现这样的顺序:进行服务引用->获得注册中心推送的地址并建立连接->启动 server 。这种情况下因为对应的端口还没有被打开,所以这个连接在建立的时候会被认为是不可用的。紧接着发起的调用就会因为没有可用地址而出现此异常。

如果 server 的启动顺序在获得注册中心推送的地址并建立连接之前,就不会出现这个问题。而这个顺序取决于 spring 上下文的刷新时间以及本地注册中心推送地址花费的时间等,是不确定的。从我多次尝试的结果来看,在 mac 机器上,启动 server 的顺序总是在之前,而在 windows 机器上,启动 server 的顺序有时候会在之后。

我通过在 System.out.println(helloSyncServiceReference.saySync("sync"));这行代码之前加入Thread.sleep(5000)成功进行了调用。因为建立连接失败的连接会在默认的 10s 间隔后进行重试,在调用之前进行等待 5s 就是为了让连接能够获得重试,这个时候由于 server 已经开启,所以这个连接会重试成功,被放入到可用列表中。最终调用就会成功。你可以用这个方式尝试看是否可以解决这个异常。

下个版本我们也会加入一个 lazy 属性,在发起第一次调用的时候才会去建立连接,这样也可以避免你遇到的这个问题。

dingzhaoxiang commented 6 years ago

原来如此,您讲的非常清楚,我明白了。 非常感谢您这两天来的耐心解答,祝您工作顺利~希望贵公司越来越好!