服务端搭建
maven的依赖,包括服务端和客户端,以及注册到nacos上面
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.xiaobing</groupId><artifactId>xiaobing-back</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>admin-server</artifactId><name>admin-server</name><description>admin-server</description><properties><java.version>11</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties><dependencies><!-- SpringCloud 微服务 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.5</version><type>pom</type><scope>import</scope></dependency><!-- 鉴权 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- springboot的web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 监控服务端依赖 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>${spring-boot-admin.version}</version></dependency><!-- 监控客户端依赖 (监控自己,如不需要监控自己,可以注释) --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.6.2</version></dependency><!--nacos服务注册发现依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><finalName>xiaobing-gateway</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>com.xiaobing.gateway.GatewayApplication</mainClass></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
bootstrap.yml配置文件
server:port: 8088# spring
spring:security:user:name: adminpassword: 123456cloud:nacos:username: nacospassword: nacosdiscovery:server-addr: localhost:8848namespace: xulkgroup: xiaobingboot:admin:client:url: http://localhost:8088 #配置admin-server地址instance:service-host-type: ip #开启ip,模板是电脑名称ui:title: xulk服务状态监控management:endpoints:web:exposure:include: '*' # 暴露所有节点endpoint:health:show-details: always # 开启监控其他环境,例如redis、mysql等logfile:external-file: logs/${spring.application.name}/sys-info.log # 日志文件 这里的配置要和 logback 的路径对应上
management:
endpoints:
web:
exposure:
include: '*' # 暴露所有节点
endpoint:
health:
show-details: always # 开启监控其他环境,例如redis、mysql等
logfile:
external-file: logs/${spring.application.name}/sys-info.log # 日志文件 这里的配置要和 logback 的路径对应上
注意 注意 注意 这个日志文件路径要和 logback的文件路径对应上,要不然读取不到日志信息
logback-spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false"><springProperty scope="context" name="spring.application.name" source="spring.application.name"/><!-- 日志存放路径 --><!-- <property name="log.path" value="/home/xiaobing/logs" />--><property name="log.path" value="logs/${spring.application.name}"/><!-- 日志输出格式 --><!-- <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>--><property name="log.pattern"value="%blue(%d{yyyy-MM-dd HH:mm:ss.SSS}) %highlight(%-5level) %magenta([%thread]) %cyan(%logger{36} - [%M:%L]) - %msg%n"/><!-- 控制台输出 --><appender name="console" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${log.pattern}</pattern></encoder></appender><!-- 系统日志输出 --><appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}/sys-info.log</file><!-- 循环政策:基于时间创建日志文件 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 日志文件名格式 --><fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern><!-- 日志最大的历史 60天 --><maxHistory>60</maxHistory></rollingPolicy><encoder><pattern>${log.pattern}</pattern></encoder><filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 过滤的级别 --><level>INFO</level><!-- 匹配时的操作:接收(记录) --><onMatch>ACCEPT</onMatch><!-- 不匹配时的操作:拒绝(不记录) --><onMismatch>DENY</onMismatch></filter></appender><appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}/sys-error.log</file><!-- 循环政策:基于时间创建日志文件 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 日志文件名格式 --><fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern><!-- 日志最大的历史 60天 --><maxHistory>60</maxHistory></rollingPolicy><encoder><pattern>${log.pattern}</pattern></encoder><filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 过滤的级别 --><level>ERROR</level><!-- 匹配时的操作:接收(记录) --><onMatch>ACCEPT</onMatch><!-- 不匹配时的操作:拒绝(不记录) --><onMismatch>DENY</onMismatch></filter></appender><!-- 用户访问日志输出 --><appender name="admin-server" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}/admin-server.log</file><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 按天回滚 daily --><fileNamePattern>${log.path}/admin-server.%d{yyyy-MM-dd}.log</fileNamePattern><!-- 日志最大的历史 60天 --><maxHistory>60</maxHistory></rollingPolicy><encoder><pattern>${log.pattern}</pattern></encoder></appender><!-- 系统模块日志级别控制 --><logger name="com.xiaobing" level="info"/><!-- Spring日志级别控制 --><logger name="org.springframework" level="warn"/><root level="info"><appender-ref ref="console"/></root><!--系统操作日志--><root level="info"><appender-ref ref="file_info"/><appender-ref ref="file_error"/></root><!--系统用户操作日志--><logger name="admin-server" level="info"><appender-ref ref="admin-server"/></logger>
</configuration>
启动类
启动类要添加注解: @EnableAdminServer
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}
鉴权放行 WebSecurityConfigurer
package com.xiaobing.user.config;import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;/*** 监控权限配置** @author xulk*/
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {private final String adminContextPath;public WebSecurityConfigurer(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(adminContextPath + "/");http.headers().frameOptions().disable().and().authorizeRequests().antMatchers(adminContextPath + "/assets/**", adminContextPath + "/login", adminContextPath + "/actuator/**", adminContextPath + "/instances/**").permitAll().anyRequest().authenticated().and().formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout().logoutUrl(adminContextPath + "/logout").and().httpBasic().and().csrf().disable();}
}
代码目录
访问地址和端口 http://192.168.20.64:8088/login
监控的服务
监控日志
客户端集成
引入依赖
<!-- SpringBoot Admin 客户端 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.6.2</version></dependency><!-- SpringBoot Actuator springboot提供,可以用可以不用 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
添加配置
注意、注意、注意 客户端需要 配置 服务端的ip和端口
# 开发环境
spring:boot:admin:client:url: http://192.168.20.64:8088 #配置服务端admin-server地址instance:service-host-type: ip
management:endpoints:web:exposure:include: '*'endpoint:health:show-details: alwayslogfile:external-file: logs/${spring.application.name}/sys-info.log
logback 日志配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false"><springProperty scope="context" name="spring.application.name" source="spring.application.name"/><!-- 日志存放路径 --><!-- <property name="log.path" value="/home/xiaobing/logs" />--><property name="log.path" value="logs/${spring.application.name}"/><!-- 日志输出格式 --><!-- <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>--><property name="log.pattern"value="%blue(%d{yyyy-MM-dd HH:mm:ss.SSS}) %highlight(%-5level) %magenta([%thread]) %cyan(%logger{36} - [%M:%L]) - %msg%n"/><!-- 控制台输出 --><appender name="console" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${log.pattern}</pattern></encoder></appender><!-- 系统日志输出 --><appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}/sys-info.log</file><!-- 循环政策:基于时间创建日志文件 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 日志文件名格式 --><fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern><!-- 日志最大的历史 60天 --><maxHistory>60</maxHistory></rollingPolicy><encoder><pattern>${log.pattern}</pattern></encoder><filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 过滤的级别 --><level>INFO</level><!-- 匹配时的操作:接收(记录) --><onMatch>ACCEPT</onMatch><!-- 不匹配时的操作:拒绝(不记录) --><onMismatch>DENY</onMismatch></filter></appender><appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}/sys-error.log</file><!-- 循环政策:基于时间创建日志文件 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 日志文件名格式 --><fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern><!-- 日志最大的历史 60天 --><maxHistory>60</maxHistory></rollingPolicy><encoder><pattern>${log.pattern}</pattern></encoder><filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 过滤的级别 --><level>ERROR</level><!-- 匹配时的操作:接收(记录) --><onMatch>ACCEPT</onMatch><!-- 不匹配时的操作:拒绝(不记录) --><onMismatch>DENY</onMismatch></filter></appender><!-- 用户访问日志输出 --><appender name="user" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${log.path}/user.log</file><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 按天回滚 daily --><fileNamePattern>${log.path}/user.%d{yyyy-MM-dd}.log</fileNamePattern><!-- 日志最大的历史 60天 --><maxHistory>60</maxHistory></rollingPolicy><encoder><pattern>${log.pattern}</pattern></encoder></appender><!-- 系统模块日志级别控制 --><logger name="com.xiaobing" level="info"/><!-- Spring日志级别控制 --><logger name="org.springframework" level="warn"/><root level="info"><appender-ref ref="console"/></root><!--系统操作日志--><root level="info"><appender-ref ref="file_info"/><appender-ref ref="file_error"/></root><!--系统用户操作日志--><logger name="user" level="info"><appender-ref ref="user"/></logger>
</configuration>