dubbo 服务在 Tomcat 中启动的问题

  1. tomcat 管理页面,reload 或者 stop 服务,然后再 start,服务就会端口占用。

原因:Tomcat 停止的时候,没有释放掉占用的资源。

解决方法:Tomcat 停止的时候,执行ProtocolConfig.destroyAll();
参考:
https://github.com/alibaba/dubbo/issues/333
web.xml 中配置监听器:


    <listener>
        <listener-class>com.common.listen.InitDeviceListener</listener-class>
    </listener>


@Component
public class InitDeviceListener implements ServletContextListener {
    Logger logger = LoggerFactory.getLogger(InitDeviceListener.class);
    private String rootPath;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
       // tomcat 启动时执行
        
    }
    
    

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //tomcat结束时执行
         ProtocolConfig.destroyAll();
    }
    
}

  1. provider 和 consumer 启动要有顺序。如果 provider 没有启动,先启动 consumer 会报错。

具体现象:


nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoService': FactoryBean threw exception on object creation; 

nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.modules.service.dubbo.DemoService. No provider available for the service …………

原因:
provider 没有启动,因此在zookeeper没有这个服务,就会报这个异常。
解决方法:使用懒加载调用服务。


@Lazy
@Autowired
DemoService demoService;

注意:这种方法可以让 provider 和 consumer 启动没有先后顺序。但如果 consumer 启动后,还没有等 provider 启动,就调用了接口,同样会报上面的异常。如果在报错后,再启动 provider,已经不管用了,因为 consumer 已经注入了服务,会报下面的异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.modules.service.dubbo.DemoService' available: Optional dependency not present for lazy injection point

因此,比较好的方式是:****不管先后顺序,先把两个服务都启动起来,然后再调用

  1. provider 中断,consumer 调用服务会报异常。

具体现象:

com.alibaba.dubbo.rpc.RpcException: Forbid consumer …………

原因:zookeeper发现相关服务接口不存在提供者的时候,禁用了消费者调用该服务接口。
参考:http://blog.sina.com.cn/s/blog_4adc4b090102x12u.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,256评论 19 139
  • 计算机采用二进制来表示整数,计算机的整数表示方法有Unsigned Integers 和 Signed Integ...
    要上班的斌哥阅读 1,636评论 0 2
  • 山水美术馆展出的毕加索展特别好,囊括了毕加索大师的各个时期,各种风格,各类艺术形式的作品,加之其好友的摄影作品对他...
    亓盼阅读 920评论 0 0