获取容器端口(定时任务锁配置)

https://stackoverflow.com/questions/24643483/how-to-find-port-of-spring-boot-container-when-running-a-spock-test-using-proper


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

猜你喜欢

转载自1971161579.iteye.com/blog/2404558