当前位置:   article > 正文

dubbo生产者在同一台服务器上部署多个jar包时报端口冲突的问题_在服务器上启动多个jar包为什么会把其他jar挤掉

在服务器上启动多个jar包为什么会把其他jar挤掉

##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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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

dubbo:
  application:
    id: demo-provider
    name: demo-provider
  protocol:
    id: dubbo
    name: dubbo
    port: -1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

欢迎关注

个人公众号

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/165965
推荐阅读
相关标签
  

闽ICP备14008679号