我有一堆小服务,方便地称为微服务;-),它们都具有相同的启动行为。它们被安排在包装中 - 每一个服务一个。包命名是这样的:
com.foo.bar.Application
其中com.foo是域部分,bar是实际的服务名称,Application是带有main方法的主实例类。
现在我想要做的是在启动时打印出标准化的日志消息,显示用户可能查询的URL以获取有关服务的一些信息。根据定义,获取服务信息的URL应如下构造:
IP:PORT/bar/admin/info
我尝试使用getClass()等获取此bar.name,但这不起作用。所以我尝试了这个:
LOG.info("Access URLs:\n----------------------------------------------------------\n" +
"\tLocal: \t\thttp://127.0.0.1:{}/" + getMyClass() + "/info\n" +
"\tExternal: \thttp://{}:{}/" + getMyClass() + "/info\n----------------------------------------------------------",
env.getProperty("server.port"),
InetAddress.getLocalHost().getHostAddress(),
env.getProperty("server.port")
);
/**
* Get the name of the application class as a static value, so we can show it in the log
*/
public static final Class[] getClassContext() {
return new SecurityManager() {
protected Class[] getClassContext(){return super.getClassContext();}
}.getClassContext();
};
public static final Class getMyClass() { return getClassContext()[2];}
但正如您可能已经猜到的那样,这打印得太多了:
class com.foo.bar.Application
我想要的是
bar
我怎样才能实现这一点?
顺便说一下,我使用Java 8和Spring启动 - 如果有帮助的话
致以最诚挚的问候,
克里斯
答案 0 :(得分:2)
您可以先尝试从类中检索包对象,然后再读取其名称。以下代码将为您提供packageName
变量中的完整包含包名称(例如“com.foo.bar”)以及{{1中的直接父名称(例如bar
)变量:
directParent
所以,如果你把它放在这个片段之后,你的日志语句就可以使用其中一个变量。
答案 1 :(得分:1)
这样的事情:
String pack = getClass().getPackage().getName();
String[] split = pack.split("\\.");
pack = split[split.length-1];
答案 2 :(得分:0)
@ComponentScan(basePackages = "com.foo.fileexportservice")
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class, LiquibaseAutoConfiguration.class})
@EnableEurekaClient
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.foo.fileexportservice")
public class Application {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
private static final String ERROR_MSG = "You have misconfigured your application! ";
@Inject
private Environment env;
/**
* Main method, used to run the application.
*/
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(Application.class);
app.setShowBanner(true);
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
addDefaultProfile(app, source);
Environment env = app.run(args).getEnvironment();
LOG.info("Access URLs:\n----------------------------------------------------------\n" +
"\tLocal: \t\thttp://127.0.0.1:{}/" + getPackageName() + "/admin/info\n" +
"\tExternal: \thttp://{}:{}/" + getPackageName() + "/admin/info\n----------------------------------------------------------",
env.getProperty("server.port"),
InetAddress.getLocalHost().getHostAddress(),
env.getProperty("server.port")
);
}
/**
* Get the name of the application class as a static value, so we can show it in the log
*/
private static final String getPackageName() {
String packageName = getMyClass().getPackage().getName();
String directParent;
if(packageName.contains(".")) {
directParent = packageName.substring(1 + packageName.lastIndexOf("."));
} else {
directParent = packageName;
}
return directParent;
};
private static final Class[] getClassContext() {
return new SecurityManager() {
protected Class[] getClassContext(){return super.getClassContext();}
}.getClassContext();
};
private static final Class getMyClass() { return getClassContext()[2];}
/**
* If no profile has been configured, set by default the "dev" profile.
*/
private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) {
if (!source.containsProperty("spring.profiles.active") &&
!System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {
app.setAdditionalProfiles(Constants.SPRING_PROFILE_DEVELOPMENT);
}
}
}