源于:::1.7分钟学会Spring Boot的CommandLineRunner
http://mp.weixin.qq.com/s/L3QD-BQ0mWkFyW_7QoKL5w
看完 ImportSource的这篇文章,对CommandLineRunner的理解又更加深入了一层,正好项目中有对CommandLineRunner的应用,于是将项目中的实例拿来总结一下,与大家分享,也算是自己对项目的总结。对于CommandLineRunner的介绍和应用就不再重复了,因为ImportSource的文章写的很详细,说实话自己不太适合写文章,只能算是简单的罗列,希望以后写的越来越好。
#1.查看项目的运行环境
这部分是在CommandLineRunner之外的,放在这里可能有点不太合适,是关于配置的一些信息了,还是写出来了,感觉差不是很多。
POM文件内容
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
</profiles>
启动Application
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
public class DiscoveryApplication {
private static final Logger log = LoggerFactory.getLogger(DiscoveryApplication.class);
public static void main(String[] args) {
Object[] obj = { DiscoveryApplication.class };
SpringApplication app = new SpringApplication(obj);
ApplicationContext context = app.run(args);
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
for (String activeProfile : activeProfiles) {
log.info("当前环境: " + activeProfile);
}
}
}
打印log: ===》》》当前环境: dev
改变运行环境命令
打包命令:mvn clean -Ptest package -Dmaven.test.skip=true
运行命令:java –jar discovery.jar
打印log: ===》》》当前环境: test
Ok,CommandLineRunner之外的介绍到此结束。
2.::: CommandLineRunner应用:打印连接池等信息
@Component
public class StartupRunner implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(StartupRunner.class);
@Autowired
private C3p0DataSourceProperties c3p0DataSourceProperties;
@Autowired
private DataSource dataSource;
@Autowired
JedisCluster jedisCluster;
@Autowired
RedisTemplate redisTemplate;
@Override
public void run(String... strings) throws Exception {
log.info("CommandLineRunner.........");
log.info("c3p0DataSourceProperties: " + ReflectionToStringBuilder.toString(c3p0DataSourceProperties));
log.info("Datasource: " + ReflectionToStringBuilder.toString(dataSource));
log.info("CommandLineRunner End");
}
}
出力Log:=========》
StartupRunner run 35 : CommandLineRunner.........
StartupRunner run 36 : c3p0DataSourceProperties: C3p0DataSourceProperties@544733a4[driverClass=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://192.168.0.108:3306/module1?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull,user=root,password=123456789,acquireIncrement=1,initialPoolSize=2,maxIdleTime=2,maxPoolSize=2,minPoolSize=2,acquireRetryDelay=1000,acquireRetryAttempts=60,breakAfterAcquireFailure=false]
StartupRunner run 37 : Datasource: ***********************
StartupRunner run 47 : CommandLineRunner End
EmailApplication main 81 : spring boot使用环境为: dev
3.:::查看当前实例是否正确配置:可以来判断服务发现中注册的服务名称和启动的服务名称是否匹配
@Component
public class StartupRunner implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(StartupRunner.class);
@Autowired
private ConfigClientProperties configClientProperties;
@Autowired
private DiscoveryClient client;
@Override
public void run(String... strings) throws Exception {
log.info("CommandLineRunner.........");
log.info("configClientProperties: " + ReflectionToStringBuilder.toString(configClientProperties));
String serviceId = this.configClientProperties.getDiscovery().getServiceId();
log.info("serviceId:"+serviceId);
List<ServiceInstance> instances = this.client.getInstances(serviceId);
if (instances.isEmpty()) {
log.warn("No instances found of (" + serviceId + ")");
} else{
log.warn("instances found of (" + serviceId + ")");
}
log.info("CommandLineRunner End");
}
}
运行log
CommandLineRunner.........
configClientProperties:***********************
serviceId:configserver
instances found of (configserver)
CommandLineRunner End
Started ConfigServerApplication in 142.344 seconds (JVM running for 143.118)
当前环境: dev, configserver配置:native
the end......................