spring-cloud整合admin2.0实现云监控

简介:
项目当中已经整合日志和拦截器的配置,同时解决监控服务自己无法监控自己的问题。
一.在服务端配置
1.导入依赖

<?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-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.io.demo</groupId>
    <artifactId>demo-admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-admin</name>
    <description>demo云控制平台</description>
    <properties>
        <spring-boot-admin.version>2.1.5</spring-boot-admin.version>
        <skipTests>true</skipTests>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <!--客户端配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <!--服务端配置-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <finalName>${project.artifactId}-8080</finalName><!--打jar包去掉版本号-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.在项目application.properties文件中配置

#固定配置
spring.application.name=demo-admin
##eureka
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.instance.health-check-url-path=/actuator/health
eureka.client.registry-fetch-interval-seconds=5
eureka.instance.metadata-map.startup=${random.int}
##management
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#logging
logging.file=${spring.application.name}.log
logging.level.org.springframework.web=info
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
#admin配置
spring.boot.admin.client.instance.metadata.tags.environment = dev
info.tags.environment = dev
server.port=8080
#注册服务配置
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
spring.security.user.name=demo
spring.security.user.password=demo
spring.boot.admin.client.username=${spring.security.user.name}
spring.boot.admin.client.password=${spring.security.user.password}
  1. 在项目的DemoApplication启动类中配置
@EnableAdminServer

4.配置拦截器

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl("/");
        http.authorizeRequests()
            .antMatchers("/assets/**").permitAll()
                .antMatchers("/actuator/**").permitAll()
            .antMatchers("/login").permitAll()
            .anyRequest().authenticated() 
            .and()
        .formLogin().loginPage("/login").successHandler(successHandler).and()
        .logout().logoutUrl("/logout").and()
        .httpBasic().and() 
        .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .ignoringAntMatchers(
                "/instances",
                "/actuator/**"
            );
        // @formatter:on
    }
}

5.解释
上面为什么没有admin.client.url地址的配置,其中使用注册服务eureka,当服务端注册到eureka后,自动会将eurekas
所有的注册服务装载到admin里面去。所以项目中使用了eureka就不需要配置客户端链接地址。
二.客户端配置
在需要监控的服务当中:
1.导入依赖

<!--客户端配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>

2.在项目application.properties文件中配置

#logging
logging.file=${spring.application.name}.log
logging.level.org.springframework.web=info
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
##eureka
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.instance.health-check-url-path=/actuator/health
eureka.client.registry-fetch-interval-seconds=5
eureka.instance.metadata-map.startup=${random.int}
##management
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#spring admin
spring.boot.admin.client.username=demo
spring.boot.admin.client.password=demo
spring.boot.admin.client.instance.metadata.tags.environment = dev
info.tags.environment = dev

三.启动服务端
打开地址为:http://127.0.0.1:8080
用户名:demo
密码:demo
就可以看到服务端自己的监控。
四.启动客户端
等待一会,就可以在服务端的监控页面就可以看到你当前客户端的监控

上一篇:spring-boot项目整合socket同时启用stomp模式


下一篇:spring-boot整合hazelcast实现高频交易撮合引擎缓存部分入门级