Commit 38499fff by Dennis Schulte

Create a starter

parent f7425fd7
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-example</artifactId>
<version>1.0.2-SNAPSHOT</version>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://opensource.org/licenses/Apache-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<spring-boot.version>1.1.4.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>de.codecentric.SpringBootAdminApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
...@@ -18,14 +18,13 @@ package de.codecentric.boot.admin; ...@@ -18,14 +18,13 @@ package de.codecentric.boot.admin;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import de.codecentric.boot.admin.config.WebappConfig; import de.codecentric.boot.admin.config.EnableAdminServer;
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@Import(WebappConfig.class) @EnableAdminServer
public class SpringBootAdmin { public class SpringBootAdminApplication {
/** /**
* Starting point for application to boot. * Starting point for application to boot.
...@@ -34,7 +33,7 @@ public class SpringBootAdmin { ...@@ -34,7 +33,7 @@ public class SpringBootAdmin {
* Passed arguments. * Passed arguments.
*/ */
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBootAdmin.class, args); SpringApplication.run(SpringBootAdminApplication.class, args);
} }
} }
/target
/.settings
/.classpath
/.project
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric</groupId> <groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin</artifactId> <artifactId>spring-boot-admin-server</artifactId>
<version>1.0.1.RELEASE</version> <version>1.0.2-SNAPSHOT</version>
<licenses> <licenses>
<license> <license>
<name>Apache License, Version 2.0</name> <name>Apache License, Version 2.0</name>
...@@ -78,7 +78,6 @@ ...@@ -78,7 +78,6 @@
<artifactId>nvd3</artifactId> <artifactId>nvd3</artifactId>
<version>${nvd3.version}</version> <version>${nvd3.version}</version>
</dependency> </dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
...@@ -141,21 +140,6 @@ ...@@ -141,21 +140,6 @@
<encoding>utf8</encoding> <encoding>utf8</encoding>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>de.codecentric.SpringBootAdmin</mainClass>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
\ No newline at end of file
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* @author Dennis Schulte
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(WebappConfig.class)
public @interface EnableAdminServer {
}
...@@ -20,12 +20,11 @@ import java.util.List; ...@@ -20,12 +20,11 @@ import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import de.codecentric.boot.admin.model.Application; import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.service.ApplicationRegistry; import de.codecentric.boot.admin.service.ApplicationRegistry;
...@@ -33,7 +32,7 @@ import de.codecentric.boot.admin.service.ApplicationRegistry; ...@@ -33,7 +32,7 @@ import de.codecentric.boot.admin.service.ApplicationRegistry;
/** /**
* REST controller for controlling registration of managed applications. * REST controller for controlling registration of managed applications.
*/ */
@Controller @RestController
public class RegistryController { public class RegistryController {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistryController.class); private static final Logger LOGGER = LoggerFactory.getLogger(RegistryController.class);
...@@ -49,7 +48,6 @@ public class RegistryController { ...@@ -49,7 +48,6 @@ public class RegistryController {
* @return The registered application. * @return The registered application.
*/ */
@RequestMapping(value = "/api/applications", method = RequestMethod.POST) @RequestMapping(value = "/api/applications", method = RequestMethod.POST)
@ResponseBody
public Application register(@RequestBody Application app) { public Application register(@RequestBody Application app) {
LOGGER.info("Register application with ID '{}' and URL '{}'", app.getId(), app.getUrl()); LOGGER.info("Register application with ID '{}' and URL '{}'", app.getId(), app.getUrl());
return registry.register(app); return registry.register(app);
...@@ -63,7 +61,6 @@ public class RegistryController { ...@@ -63,7 +61,6 @@ public class RegistryController {
* @return The registered application. * @return The registered application.
*/ */
@RequestMapping(value = "/api/application/{id}", method = RequestMethod.GET) @RequestMapping(value = "/api/application/{id}", method = RequestMethod.GET)
@ResponseBody
public Application get(@PathVariable String id) { public Application get(@PathVariable String id) {
LOGGER.debug("Deliver registered application with ID '{}'", id); LOGGER.debug("Deliver registered application with ID '{}'", id);
return registry.getApplication(id); return registry.getApplication(id);
...@@ -75,7 +72,6 @@ public class RegistryController { ...@@ -75,7 +72,6 @@ public class RegistryController {
* @return List. * @return List.
*/ */
@RequestMapping(value = "/api/applications", method = RequestMethod.GET) @RequestMapping(value = "/api/applications", method = RequestMethod.GET)
@ResponseBody
public List<Application> applications() { public List<Application> applications() {
LOGGER.debug("Deliver all registered applications"); LOGGER.debug("Deliver all registered applications");
return registry.getApplications(); return registry.getApplications();
......
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import de.codecentric.boot.admin.config.EnableAdminServer;
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class TestAdminApplication {
/**
* Starting point for application to boot.
*
* @param args
* Passed arguments.
*/
public static void main(String[] args) {
SpringApplication.run(TestAdminApplication.class, args);
}
}
spring.resources.cachePeriod=3600
server.port=8080
info.id=@pom.artifactId@
info.version=@pom.version@
spring.boot.admin.url=http://localhost:8080
logging.file=/tmp/log.log
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-boot-starter-admin-client</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
spring-boot-starter-admin-client
================================
This [Spring-Boot starter](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-starter-poms "Spring Boot docu") that provides services and controllers that a required if an application show be able to be administrated with the [spring-boot-admin application](https://github.com/codecentric/spring-boot-admin "GitHub project").
This client uses the [AutoConfiguration](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-auto-configuration "Spring Boot docu") feature of Spring Boot to register service and controller beans in the application context.
The main service that is used is a registrar that registeres the application at the spring-boot-admin application by periodically calling a REST-API to check or perform the registration of itself.
The following properties have to be included in the environment (i.e. application.properties) to ensure all features to work properly.
<table>
<tr>
<td>info.id</td><td>The identifier in the registry - this property is published by the /info endpoint</td>
</tr>
<tr>
<td>info.version</td><td>The version number - also published by the /info endpoint</td>
</tr>
<tr>
<td>spring.boot.admin.url</td><td>URL of the spring-boot-admin application to register at</td>
</tr>
<tr>
<td>logging.file</td><td>File path of the logfile of the application</td>
</tr>
</table>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-starter-admin-client</artifactId>
<version>1.0.2-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>1.1.4.RELEASE</version>
</parent>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://opensource.org/licenses/Apache-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import de.codecentric.boot.admin.controller.LogfileController;
import de.codecentric.boot.admin.controller.RefreshController;
import de.codecentric.boot.admin.services.SpringBootAdminRegistratorTask;
import de.codecentric.boot.admin.web.SimpleCORSFilter;
/**
* This configuration adds a registrator bean to the spring context. This bean checks periodicaly, if the using
* application is registered at the spring-boot-admin application. If not, it registers itself.
*/
@Configuration
public class SpringBootAdminClientAutoConfiguration {
/**
* Task that registers the application at the spring-boot-admin application.
*/
@Bean
public SpringBootAdminRegistratorTask registrator() {
return new SpringBootAdminRegistratorTask();
}
/**
* HTTP filter to enable Cross-Origin Resource Sharing.
*/
@Bean
public SimpleCORSFilter corsFilter() {
return new SimpleCORSFilter();
}
/**
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/
@Bean
public ScheduledTaskRegistrar taskRegistrar() {
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
registrar.addFixedRateTask(registrator(), 10000);
return registrar;
}
/**
* Controller to do something with the application logfile(s).
*/
@Bean
@ConditionalOnProperty("logging.file")
public LogfileController logfileController() {
return new LogfileController();
}
/**
* Controller to do a refresh on the application.
*/
@Bean
public RefreshController refreshController() {
return new RefreshController();
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Controller that provides an API for logfiles, i.e. downloading the main logfile configured in environment property
* 'logging.file' that is standard, but optional property for spring-boot applications.
*/
@Controller
public class LogfileController {
private static final Logger LOGGER = LoggerFactory.getLogger(LogfileController.class);
@Autowired
private Environment env;
@RequestMapping("/logfile")
@ResponseBody
public String getLogfile(HttpServletResponse response) {
String path = env.getProperty("logging.file");
if (path == null) {
LOGGER.error("Logfile download failed for missing property 'logging.file'");
return "Logfile download failed for missing property 'logging.file'";
}
Resource file = new FileSystemResource(path);
if (!file.exists()) {
LOGGER.error("Logfile download failed for missing file at path=" + path);
return "Logfile download failed for missing file at path=" + path;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFilename() + "\"");
try {
FileCopyUtils.copy(file.getInputStream(), response.getOutputStream());
} catch (IOException e) {
LOGGER.error("Logfile download failed for path=" + path);
return "Logfile download failed for path=" + path;
}
return null;
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Refresh the application context i.e. to reload changed properties files.
*/
@Controller
public class RefreshController {
private static final Logger LOGGER = LoggerFactory.getLogger(RefreshController.class);
@Autowired
private ConfigurableApplicationContext context;
@RequestMapping(value = "/refresh", method = RequestMethod.POST)
@ResponseBody
public String refresh() {
// Doesn't work in spring-boot at the moment ... (v 1.1.0)
LOGGER.warn("Refreshing application doesn't work at the moment");
return "Refreshing application doesn't work at the moment";
// context.refresh();
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.model;
import java.io.Serializable;
/**
* The domain model for all registered application at the spring boot admin application.
*/
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String url;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return id + " : " + url;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((url == null) ? 0 : url.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Application other = (Application) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (url == null) {
if (other.url != null)
return false;
} else if (!url.equals(other.url))
return false;
return true;
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.services;
import java.net.InetAddress;
import java.net.URL;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.model.Application;
/**
* Scheduler that checks the registration of the application at the spring-boot-admin.
*/
public class SpringBootAdminRegistratorTask implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootAdminRegistratorTask.class);
@Autowired
private Environment env;
@PostConstruct
public void check() {
Assert.notNull(env.getProperty("spring.boot.admin.url"),
"The URL of the spring-boot-admin application is mandatory");
Assert.notNull(env.getProperty("server.port"), "The server port of the application is mandatory");
Assert.notNull(env.getProperty("info.id"), "The id of the application is mandatory");
}
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
String id = env.getProperty("info.id");
int port = env.getProperty("server.port", Integer.class);
String adminUrl = env.getProperty("spring.boot.admin.url");
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ApplicationList list = template.getForObject(adminUrl + "/api/applications", ApplicationList.class);
for (Application app : list) {
if (id.equals(app.getId())) {
// the application is already registered at the admin tool
LOGGER.debug("Application already registered with ID '{}'", id);
return;
}
}
// register the application with the used URL and port
String url = new URL("http", InetAddress.getLocalHost().getCanonicalHostName(), port, "").toString();
Application app = new Application();
app.setId(id);
app.setUrl(url);
template.postForObject(adminUrl + "/api/applications", app, String.class);
LOGGER.info("Application registered itself at the admin application with ID '{}' and URL '{}'", id, url);
} catch (Exception e) {
LOGGER.warn("Failed to register application at spring-boot-admin, message={}", e.getMessage());
}
}
private static class ApplicationList extends ArrayList<Application> {
private static final long serialVersionUID = 1L;
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.web;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
/**
* Request filter to allow Cross-Origin Resource Sharing.
*/
public class SimpleCORSFilter implements Filter {
// Configurable origin for CORS - default: * (all)
@Value("${http.filter.cors.origin:*}")
private String origin;
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
* javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.setHeader("Access-Control-Allow-Origin", origin);
chain.doFilter(request, res);
}
/**
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
// nop
}
/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// nop
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=de.codecentric.boot.admin.config.SpringBootAdminClientAutoConfiguration
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment