BAEL-3222 Example Code and update to spring-boot-admin examples (#7804)

This commit is contained in:
Amy DeGregorio 2019-09-16 09:37:38 -04:00 committed by maibin
parent e093daf12d
commit 66f4d44c60
16 changed files with 260 additions and 119 deletions

View File

@ -30,7 +30,7 @@
</dependencyManagement> </dependencyManagement>
<properties> <properties>
<spring.boot.version>1.5.8.RELEASE</spring.boot.version> <spring.boot.version>2.1.7.RELEASE</spring.boot.version>
</properties> </properties>
</project> </project>

View File

@ -18,7 +18,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -59,7 +59,7 @@
</build> </build>
<properties> <properties>
<spring-boot-admin-starter-client.version>1.5.4</spring-boot-admin-starter-client.version> <spring-boot-admin-starter-client.version>2.1.6</spring-boot-admin-starter-client.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version> <spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
</properties> </properties>

View File

@ -1,16 +1,20 @@
#basic auth creddentials #basic auth creddentials
security.user.name=client spring.security.user.name=client
security.user.password=client spring.security.user.password=client
#configs to connect to a secured server #configs to connect to a secured server
spring.boot.admin.url=http://localhost:8080 spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.username=admin #spring.boot.admin.client.instance.service-base-url=http://localhost:8081
spring.boot.admin.password=admin spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
#configs to give secured server info #configs to give secured server info
spring.boot.admin.client.metadata.user.name=${security.user.name} spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.metadata.user.password=${security.user.password} spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
#app config #app config
spring.application.name=spring-boot-admin-client spring.application.name=spring-boot-admin-client
server.port=8081 server.port=8081
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

View File

@ -18,18 +18,14 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<!-- dependencies used to enable admin server and UI --> <!-- dependencies used to enable admin server and UI -->
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
<dependency> <dependency>
<groupId>de.codecentric</groupId> <groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId> <artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin-server.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>${spring-boot-admin-server.version}</version> <version>${spring-boot-admin-server.version}</version>
</dependency> </dependency>
@ -37,7 +33,7 @@
<dependency> <dependency>
<groupId>de.codecentric</groupId> <groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId> <artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>${spring-boot-admin-server.version}</version> <version>${spring-boot-admin-server-ui-login.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -47,6 +43,7 @@
<groupId>com.hazelcast</groupId> <groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId> <artifactId>hazelcast</artifactId>
</dependency> </dependency>
<!--declare the admin server as a client, for self monitoring --> <!--declare the admin server as a client, for self monitoring -->
<dependency> <dependency>
<groupId>de.codecentric</groupId> <groupId>de.codecentric</groupId>
@ -82,8 +79,9 @@
</build> </build>
<properties> <properties>
<spring-boot-admin-server.version>1.5.4</spring-boot-admin-server.version> <spring-boot-admin-server.version>2.1.6</spring-boot-admin-server.version>
<spring-boot-admin-starter-client.version>1.5.4</spring-boot-admin-starter-client.version> <spring-boot-admin-starter-client.version>2.1.6</spring-boot-admin-starter-client.version>
<spring-boot-admin-server-ui-login.version>1.5.7</spring-boot-admin-server-ui-login.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version> <spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
</properties> </properties>

View File

@ -1,6 +1,6 @@
package com.baeldung.springbootadminserver; package com.baeldung.springbootadminserver;
import de.codecentric.boot.admin.config.EnableAdminServer; import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@ -2,8 +2,14 @@ package com.baeldung.springbootadminserver.configs;
import com.hazelcast.config.Config; import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy; import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.ListConfig; import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.MapConfig; import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MergePolicyConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.map.merge.PutIfAbsentMapMergePolicy;
import java.util.Collections;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -12,13 +18,30 @@ public class HazelcastConfig {
@Bean @Bean
public Config hazelcast() { public Config hazelcast() {
return new Config() MapConfig eventStoreMap = new MapConfig("spring-boot-admin-event-store").setInMemoryFormat(InMemoryFormat.OBJECT)
.setProperty("hazelcast.jmx", "true")
.addMapConfig(new MapConfig("spring-boot-admin-application-store")
.setBackupCount(1) .setBackupCount(1)
.setEvictionPolicy(EvictionPolicy.NONE)) .setEvictionPolicy(EvictionPolicy.NONE)
.addListConfig(new ListConfig("spring-boot-admin-event-store") .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
MapConfig sentNotificationsMap = new MapConfig("spring-boot-admin-application-store").setInMemoryFormat(InMemoryFormat.OBJECT)
.setBackupCount(1) .setBackupCount(1)
.setMaxSize(1000)); .setEvictionPolicy(EvictionPolicy.LRU)
.setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
Config config = new Config();
config.addMapConfig(eventStoreMap);
config.addMapConfig(sentNotificationsMap);
config.setProperty("hazelcast.jmx", "true");
config.getNetworkConfig()
.getJoin()
.getMulticastConfig()
.setEnabled(false);
TcpIpConfig tcpIpConfig = config.getNetworkConfig()
.getJoin()
.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1"));
return config;
} }
} }

View File

@ -1,42 +1,40 @@
package com.baeldung.springbootadminserver.configs; package com.baeldung.springbootadminserver.configs;
import de.codecentric.boot.admin.notify.LoggingNotifier; import java.time.Duration;
import de.codecentric.boot.admin.notify.RemindingNotifier;
import de.codecentric.boot.admin.notify.filter.FilteringNotifier; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.concurrent.TimeUnit; import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.notify.LoggingNotifier;
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
@Configuration @Configuration
@EnableScheduling
public class NotifierConfiguration { public class NotifierConfiguration {
@Autowired
private InstanceRepository repository;
// @Autowired private Notifier notifier; // @Autowired private Notifier notifier;
@Bean @Bean
public LoggingNotifier notifier() { public LoggingNotifier notifier() {
return new LoggingNotifier(); return new LoggingNotifier(repository);
} }
@Bean @Bean
public FilteringNotifier filteringNotifier() { public FilteringNotifier filteringNotifier() {
return new FilteringNotifier(notifier()); return new FilteringNotifier(notifier(), repository);
} }
@Bean
@Primary @Primary
@Bean(initMethod = "start", destroyMethod = "stop")
public RemindingNotifier remindingNotifier() { public RemindingNotifier remindingNotifier() {
RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier()); RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier(), repository);
remindingNotifier.setReminderPeriod(TimeUnit.MINUTES.toMillis(5)); remindingNotifier.setReminderPeriod(Duration.ofMinutes(5));
remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(60));
return remindingNotifier; return remindingNotifier;
} }
@Scheduled(fixedRate = 60_000L)
public void remind() {
remindingNotifier().sendReminders();
}
} }

View File

@ -1,33 +1,56 @@
package com.baeldung.springbootadminserver.configs; package com.baeldung.springbootadminserver.configs;
import java.util.UUID;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
@Configuration @Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServer;
public WebSecurityConfig(AdminServerProperties adminServer) {
this.adminServer = adminServer;
}
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
.formLogin() successHandler.setTargetUrlParameter("redirectTo");
.loginPage("/login.html") successHandler.setDefaultTargetUrl(this.adminServer.getContextPath() + "/");
.loginProcessingUrl("/login")
.permitAll(); http.authorizeRequests()
http .antMatchers(this.adminServer.getContextPath() + "/assets/**")
.logout() .permitAll()
.logoutUrl("/logout"); .antMatchers(this.adminServer.getContextPath() + "/login")
http .permitAll()
.csrf() .anyRequest()
.disable(); .authenticated()
http .and()
.authorizeRequests() .formLogin()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**") .loginPage(this.adminServer.getContextPath() + "/login")
.permitAll(); .successHandler(successHandler)
http .and()
.authorizeRequests() .logout()
.antMatchers("/**") .logoutUrl(this.adminServer.getContextPath() + "/logout")
.authenticated(); .and()
http.httpBasic(); .httpBasic()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(new AntPathRequestMatcher(this.adminServer.getContextPath() + "/instances", HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.getContextPath() + "/instances/*", HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**"))
.and()
.rememberMe()
.key(UUID.randomUUID()
.toString())
.tokenValiditySeconds(1209600);
} }
} }

View File

@ -1,16 +1,19 @@
spring.application.name=spring-boot-admin-server spring.application.name=spring-boot-admin-server
security.user.name=admin spring.security.user.name=admin
security.user.password=admin spring.security.user.password=admin
#configs to connect to self register the admin server as a client #configs to connect to self register the admin server as a client
spring.boot.admin.url=http://localhost:8080 spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.username=${security.user.name} spring.boot.admin.client.username=${spring.security.user.name}
spring.boot.admin.password=${security.user.password} spring.boot.admin.client.password=${spring.security.user.password}
#configs to give secured server info #configs to give secured server info
spring.boot.admin.client.metadata.user.name=${security.user.name} spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.metadata.user.password=${security.user.password} spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#mail notifications #mail notifications
#spring.mail.host=smtp.gmail.com #spring.mail.host=smtp.gmail.com

View File

@ -1,9 +1,11 @@
package com.baeldung.springbootadminserver; package com.baeldung.springbootadminserver;
import com.baeldung.springbootadminserver.configs.NotifierConfiguration; import com.baeldung.springbootadminserver.configs.NotifierConfiguration;
import de.codecentric.boot.admin.notify.Notifier;
import de.codecentric.boot.admin.notify.RemindingNotifier; import de.codecentric.boot.admin.server.notify.Notifier;
import de.codecentric.boot.admin.notify.filter.FilteringNotifier; import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,47 +1,56 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<project <project 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" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
xmlns="http://maven.apache.org/POM/4.0.0" <modelVersion>4.0.0</modelVersion>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <parent>
<modelVersion>4.0.0</modelVersion> <groupId>com.baeldung</groupId>
<parent> <artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId> <version>0.0.1-SNAPSHOT</version>
<artifactId>parent-boot-2</artifactId> <relativePath>../parent-boot-2</relativePath>
<version>0.0.1-SNAPSHOT</version> </parent>
<relativePath>../parent-boot-2</relativePath> <groupId>com.baeldung</groupId>
</parent> <artifactId>spring-boot-management</artifactId>
<groupId>com.baeldung</groupId> <version>0.0.1-SNAPSHOT</version>
<artifactId>spring-boot-management</artifactId> <name>spring-boot-management</name>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-management</name>
<properties> <properties>
<java.version>1.8</java.version> <start-class>com.baeldung.spring.boot.management.logging.LoggingApplication</start-class>
</properties> <java.version>1.8</java.version>
<spring-boot-admin-starter-client.version>2.1.6</spring-boot-admin-starter-client.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin-starter-client.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -0,0 +1,11 @@
package com.baeldung.spring.boot.management.logging;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoggingApplication {
public static void main(String[] args) {
SpringApplication.run(LoggingApplication.class);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.boot.management.logging;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/log")
public class LoggingController {
private Log log = LogFactory.getLog(LoggingController.class);
@GetMapping
public String log() {
log.trace("This is a TRACE level message");
log.debug("This is a DEBUG level message");
log.info("This is an INFO level message");
log.warn("This is a WARN level message");
log.error("This is an ERROR level message");
return "See the log for details";
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.spring.boot.management.logging;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.ignoringAntMatchers("/actuator/**");
}
}

View File

@ -1,3 +1,22 @@
management.endpoints.web.exposure.include=httptrace management.endpoints.web.exposure.include=httptrace,loggers,health,info,metrics
management.trace.http.include=RESPONSE_HEADERS management.trace.http.include=RESPONSE_HEADERS
management.endpoint.loggers.enabled=true
#basic auth creddentials
spring.security.user.name=client
spring.security.user.password=client
#configs to connect to a secured server
spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
#configs to give secured server info
spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
#app config
spring.application.name=spring-boot-management
server.port=8081

View File

@ -0,0 +1,13 @@
<configuration scan="true" scanPeriod="15 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.baeldung.spring.boot.management.logging" level="INFO" />
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>