int port = ((TomcatEmbeddedServletContainer)((AnnotationConfigEmbeddedWebApplicationContext)context).getEmbeddedServletContainer()).getPort();
发现以下内容有想要的东西:
String id = ((AnnotationConfigEmbeddedWebApplicationContext) configContext).getId();
我的代码(覆写MongoLockProvider为MyMongoLockProvider是因为小公司一台服务器两个实例..):
关于定时任务锁
@Autowired ConfigurableApplicationContext configContext; //定时任务锁配置 @Bean public LockProvider lockProvider(@Autowired MongoClient mongo) { String serverId = configContext.getId(); return new MyMongoLockProvider(mongo, database,serverId); } @Bean public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) { return ScheduledLockConfigurationBuilder .withLockProvider(lockProvider) .withPoolSize(10) .withDefaultLockAtMostFor(Duration.ofMinutes(10)) .build(); }