dubbo port -1将dubbo端口设为-1dubbo随机端口

方法一:

将端口改为其它端口都不起作用,可以将生产者端的端口改为-1,使用随机端口


方法二:

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

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 cookie
 */
public class DynamicDubboPortReader implements ApplicationContextAware{
    @Autowired
    private ApplicationContext applicationContext;
    
    private int port = 20880;
    
    @PostConstruct
    public void init(){
        Map map = applicationContext.getBeansOfType(ProtocolConfig.class);
        for(Entry 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;
    }

}

你可能感兴趣的:(dubbo)