dubbo生产者在同一台服务器上部署多个jar包时报端口冲突的问题

springboot+dubbo+zookeeper 专栏收录该内容
4 篇文章 0 订阅

##dubbo 生产者在同一台服务器上部署多个jar包时报端口冲突的问题

报错信息如下:

Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /*.*.*.*:20880, cause: Failed to bind to: /0.0.0.0:20880
    at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:73)
    at com.alibaba.dubbo.remoting.transport.netty.NettyServer.<init>(NettyServer.java:63)
    at com.alibaba.dubbo.remoting.transport.netty.NettyTransporter.bind(NettyTransporter.java:33)
    at com.alibaba.dubbo.remoting.Transporter$Adpative.bind(Transporter$Adpative.java)
    at com.alibaba.dubbo.remoting.Transporters.bind(Transporters.java:48)
    at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:41)
    at com.alibaba.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:63)
    at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:329)
    ... 34 more
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:272)
    at com.alibaba.dubbo.remoting.transport.netty.NettyServer.doOpen(NettyServer.java:94)
    at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:68)
    ... 41 more
Caused by: java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:437)
    at sun.nio.ch.Net.bind(Net.java:429)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.jboss.netty.channel.socket.nio.NioServerBoss$RegisterTask.run(NioServerBoss.java:193)
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.processTaskQueue(AbstractNioSelector.java:366)
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:290)
    at org.jboss.netty.channel.socket.nio.NioServerBoss.run(NioServerBoss.java:42)
    ... 3 more

方法一:此时将端口改为其它端口都不起作用,可以将生产者端的端口改为-1,使用随机端口,可以解决问题

dubbo:
  application:
    id: demo-provider
    name: demo-provider
  protocol:
    id: dubbo
    name: dubbo
    port: -1

方法二:使用如下方法,启动时获取随机端口

package com.ciming.util;

import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.config.ProtocolConfig;
/**
 * 一个机器部署两个dubbo生产者服务会产生端口占用问题,为了解决这个问题,在加载dubbo配置文件之前,先设置没被占用的端口
 * @author typ
 */
public class DynamicDubboPortReader implements ApplicationContextAware{
    @Autowired
    private ApplicationContext applicationContext;
    
    private int port = 20880;
    
    @PostConstruct
    public void init(){
        Map<String, ProtocolConfig> map = applicationContext.getBeansOfType(ProtocolConfig.class);
        for(Entry<String,ProtocolConfig> con: map.entrySet()){
            port = NetUtils.getAvailablePort();
            System.out.println("=========="+port);
            con.getValue().setPort(port);
        }
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ConfigurableApplicationContext)applicationContext;
    }

}

欢迎关注

个人公众号

  • 0
    点赞
  • 3
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值