Springboot单机多副本运行,解决端口冲突

一、不启动端口方式(仅支持定时、kafka等任务,不支持get/post)

spring:
  main:
    web-application-type: none

eureka客户端唯一id采用随机数

eureka:
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${random.int(10000, 65536)}

二、将随机端口设置为全局系统属性,在配置和代码中引用

public class Application {
    public static void main(String[] args) {
        System.setProperty("my.port", String.valueOf(new Random().nextInt(55536) + 10000));
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
    }
}

配置中引用:

eureka:
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${my.port}

配置类中引用:

复制代码
@Value("${my.port}")
private int myport;

@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> MyCustomizer(){
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            factory.setPort(myport);
        }
    };
}
复制代码

 

posted @   凤凰涅磐欲重生  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示