springboot dubbo多模块中2个模块都既是提供者又是消费者,第二个启动会报错Address already in use: bind 20880,xml配置端口不是20880也会报这个错

springboot dubbo多模块中2个模块都既是提供者又是消费者,第二个启动会报错Address already in use: bind 20880

错误的配置xml

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
    <dubbo:application name="dubbo"/>

    <!--消费者-->
    <!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
    <dubbo:consumer check="false" />
    <!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
    <dubbo:reference id="bService" check="false"  interface="com.example.dubboapi.service.BService"/>
    <dubbo:annotation package="com.example.dubboa.*"/>

    <!--提供者-->
    <!-- port值-1 表示让dubbo自行找一个可用的port -->
    <dubbo:protocol port="-1" name="dubbo"/>
    <dubbo:service protocol="dubbo" ref="aService" interface="com.example.dubboapi.service.AService"/>
    <bean id="aService" class="com.example.dubboa.service.AServiceImpl"/>

</beans>

具体报错如下:

com.alibaba.dubbo.rpc.RpcException: Fail to start server(url: dubbo://172.16.1.112:20880/com.example.dubboapi.service.AService?anyhost=true&application=dubbo&bind.ip=172.16.1.112&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.6.0&generic=false&heartbeat=60000&interface=com.example.dubboapi.service.AService&methods=sayA&pid=63756&side=provider&timestamp=1561975401431) Failed to bind NettyServer on /172.16.1.112:20880, cause: Failed to bind to: /0.0.0.0:20880
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:277) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.openServer(DubboProtocol.java:254) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.export(DubboProtocol.java:241) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:68) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:93) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.registry.integration.RegistryProtocol.doLocalExport(RegistryProtocol.java:181) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:121) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:66) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:91) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:505) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:357) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:316) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:215) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:121) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:50) ~[dubbo-2.6.0.jar:2.6.0]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:888) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at com.example.dubboa.DubboaApplication.main(DubboaApplication.java:16) [classes/:na]
Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /172.16.1.112:20880, cause: Failed to bind to: /0.0.0.0:20880
	at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:68) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.transport.netty.NettyServer.<init>(NettyServer.java:61) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.transport.netty.NettyTransporter.bind(NettyTransporter.java:31) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.Transporter$Adaptive.bind(Transporter$Adaptive.java) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.Transporters.bind(Transporters.java:56) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:42) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:70) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:275) ~[dubbo-2.6.0.jar:2.6.0]
	... 31 common frames omitted
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: /0.0.0.0:20880
	at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:303) ~[netty-3.2.5.Final.jar:na]
	at com.alibaba.dubbo.remoting.transport.netty.NettyServer.doOpen(NettyServer.java:92) ~[dubbo-2.6.0.jar:2.6.0]
	at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:63) ~[dubbo-2.6.0.jar:2.6.0]
	... 38 common frames omitted
Caused by: java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_201]
	at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_201]
	at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_201]
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_201]
	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_201]
	at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.bind(NioServerSocketPipelineSink.java:148) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleServerSocket(NioServerSocketPipelineSink.java:100) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:74) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.Channels.bind(Channels.java:468) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.AbstractChannel.bind(AbstractChannel.java:192) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.bootstrap.ServerBootstrap$Binder.channelOpen(ServerBootstrap.java:348) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.Channels.fireChannelOpen(Channels.java:176) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.socket.nio.NioServerSocketChannel.<init>(NioServerSocketChannel.java:85) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory.newChannel(NioServerSocketChannelFactory.java:142) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory.newChannel(NioServerSocketChannelFactory.java:90) ~[netty-3.2.5.Final.jar:na]
	at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:282) ~[netty-3.2.5.Final.jar:na]
	... 40 common frames omitted

解决:将端口配置放到前面,如下:

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
    <!-- port值-1 表示让dubbo自行找一个可用的port -->
    <dubbo:protocol port="-1" name="dubbo"/>
    <dubbo:application name="dubbo"/>

    <!--消费者-->
    <!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
    <dubbo:consumer check="false" />
    <!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
    <dubbo:reference id="bService" check="false"  interface="com.example.dubboapi.service.BService"/>
    <dubbo:annotation package="com.example.dubboa.*"/>

    <!--提供者-->
    <dubbo:service protocol="dubbo" ref="aService" interface="com.example.dubboapi.service.AService"/>
    <bean id="aService" class="com.example.dubboa.service.AServiceImpl"/>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值