18 changed files with 473 additions and 20 deletions
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
# Spring Boot Actuator Sample |
||||
|
||||
You can build this sample using Maven (>3) or Gradle (1.6). |
||||
|
||||
With Maven: |
||||
|
||||
``` |
||||
$ mvn package |
||||
$ java -jar target/*.jar |
||||
``` |
||||
|
||||
Then access the app via a browser (or curl) on http://localhost:8080 (the user name is "user" and look at the INFO log output for the password to login). |
||||
|
||||
With gradle: |
||||
|
||||
``` |
||||
$ gradle build |
||||
$ java -jar build/libs/*.jar |
||||
``` |
||||
|
||||
The gradle build contains an intentionally odd configuration to exclude the security dependencies from the executable JAR. So the app run like this behaves differently than the one run from the Maven-built JAR file. See comments in the `build.gradle` for details. |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<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> |
||||
<parent> |
||||
<!-- Your own application should inherit from spring-boot-starter-parent --> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-samples</artifactId> |
||||
<version>0.5.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-sample-actuator-log4j</artifactId> |
||||
<packaging>jar</packaging> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>${project.groupId}</groupId> |
||||
<artifactId>spring-boot-starter-actuator</artifactId> |
||||
<exclusions> |
||||
<exclusion> |
||||
<groupId>${project.groupId}</groupId> |
||||
<artifactId>spring-boot-starter-logging</artifactId> |
||||
</exclusion> |
||||
</exclusions> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>${project.groupId}</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>${project.groupId}</groupId> |
||||
<artifactId>spring-boot-starter-log4j</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
</project> |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/* |
||||
* Copyright 2012-2013 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 org.springframework.boot.sample.actuator.log4j; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component |
||||
public class HelloWorldService { |
||||
|
||||
@Autowired |
||||
private ServiceProperties configuration; |
||||
|
||||
public String getHelloMessage() { |
||||
return "Hello " + this.configuration.getName(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* Copyright 2012-2013 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 org.springframework.boot.sample.actuator.log4j; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.context.annotation.ComponentScan; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
@Configuration |
||||
@EnableAutoConfiguration |
||||
@EnableConfigurationProperties |
||||
@ComponentScan |
||||
public class SampleActuatorApplication { |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
SpringApplication.run(SampleActuatorApplication.class, args); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2012-2013 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 org.springframework.boot.sample.actuator.log4j; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Map; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
@Controller |
||||
public class SampleController { |
||||
|
||||
@Autowired |
||||
private HelloWorldService helloWorldService; |
||||
|
||||
@RequestMapping("/") |
||||
@ResponseBody |
||||
public Map<String, String> helloWorld() { |
||||
return Collections.singletonMap("message", |
||||
this.helloWorldService.getHelloMessage()); |
||||
} |
||||
|
||||
@RequestMapping("/foo") |
||||
@ResponseBody |
||||
public String foo() { |
||||
throw new IllegalArgumentException("Server error"); |
||||
} |
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* Copyright 2012-2013 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 org.springframework.boot.sample.actuator.log4j; |
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@ConfigurationProperties(name = "service", ignoreUnknownFields = false) |
||||
@Component |
||||
public class ServiceProperties { |
||||
|
||||
private String name = "World"; |
||||
|
||||
public String getName() { |
||||
return this.name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
logging.file: /tmp/logs/app.log |
||||
management.port: 8080 |
||||
management.address: 127.0.0.1 |
||||
endpoints.shutdown.enabled: true |
||||
server.port: 8080 |
||||
server.tomcat.basedir: target/tomcat |
||||
server.tomcat.access_log_pattern: %h %t "%r" %s %b |
||||
security.require_ssl: false |
||||
service.name: Phil |
||||
shell.ssh.enabled: true |
||||
shell.ssh.port: 2222 |
||||
#shell.telnet.enabled: false |
||||
#shell.telnet.port: 1111 |
||||
shell.auth: spring |
||||
#shell.auth: key |
||||
#shell.auth.key.path: ${user.home}/test/id_rsa.pub.pem |
||||
#shell.auth: simple |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
log4j.rootCategory=INFO, CONSOLE |
||||
|
||||
PID=???? |
||||
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] log4j%X{context} - ${PID} %5p [%t] --- %c{1}: %m%n |
||||
|
||||
# CONSOLE is set to be a ConsoleAppender using a PatternLayout. |
||||
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender |
||||
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout |
||||
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN} |
||||
|
||||
log4j.category.org.hibernate.validator.internal.util.Version=WARN |
||||
log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN |
||||
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN |
||||
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR |
||||
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
/* |
||||
* Copyright 2012-2013 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 org.springframework.boot.sample.actuator.log4j; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Map; |
||||
import java.util.concurrent.Callable; |
||||
import java.util.concurrent.Executors; |
||||
import java.util.concurrent.Future; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import org.junit.AfterClass; |
||||
import org.junit.BeforeClass; |
||||
import org.junit.Test; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.http.client.ClientHttpResponse; |
||||
import org.springframework.web.client.DefaultResponseErrorHandler; |
||||
import org.springframework.web.client.RestTemplate; |
||||
|
||||
/** |
||||
* Basic integration tests for service demo application. |
||||
* |
||||
* @author Dave Syer |
||||
*/ |
||||
public class SampleActuatorApplicationTests { |
||||
|
||||
private static ConfigurableApplicationContext context; |
||||
|
||||
@BeforeClass |
||||
public static void start() throws Exception { |
||||
Future<ConfigurableApplicationContext> future = Executors |
||||
.newSingleThreadExecutor().submit( |
||||
new Callable<ConfigurableApplicationContext>() { |
||||
@Override |
||||
public ConfigurableApplicationContext call() throws Exception { |
||||
return SpringApplication |
||||
.run(SampleActuatorApplication.class); |
||||
} |
||||
}); |
||||
context = future.get(60, TimeUnit.SECONDS); |
||||
} |
||||
|
||||
@AfterClass |
||||
public static void stop() { |
||||
if (context != null) { |
||||
context.close(); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testHome() throws Exception { |
||||
@SuppressWarnings("rawtypes") |
||||
ResponseEntity<Map> entity = getRestTemplate().getForEntity( |
||||
"http://localhost:8080", Map.class); |
||||
assertEquals(HttpStatus.OK, entity.getStatusCode()); |
||||
@SuppressWarnings("unchecked") |
||||
Map<String, Object> body = entity.getBody(); |
||||
assertEquals("Hello Phil", body.get("message")); |
||||
} |
||||
|
||||
|
||||
private RestTemplate getRestTemplate() { |
||||
RestTemplate restTemplate = new RestTemplate(); |
||||
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
||||
@Override |
||||
public void handleError(ClientHttpResponse response) throws IOException { |
||||
} |
||||
}); |
||||
return restTemplate; |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<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> |
||||
<parent> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starters</artifactId> |
||||
<version>0.5.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-starter-log4j</artifactId> |
||||
<packaging>jar</packaging> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.slf4j</groupId> |
||||
<artifactId>jcl-over-slf4j</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.slf4j</groupId> |
||||
<artifactId>jul-to-slf4j</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.slf4j</groupId> |
||||
<artifactId>slf4j-log4j12</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>log4j</groupId> |
||||
<artifactId>log4j</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
</project> |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
provides: logback-classic,jcl-over-slf4j,jul-to-slf4j |
||||
Loading…
Reference in new issue