Skip to content

Commit

Permalink
v1.1 版本发布,详细信息查看发行版说明
Browse files Browse the repository at this point in the history
  • Loading branch information
郑杰 committed Dec 25, 2018
1 parent 55d4824 commit 4765785
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 856 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- 权限管理 权限细化到接口
- 菜单管理 已实现动态路由,后端可配置化
- 系统日志 记录用户访问监控异常信息
- 实时控制台 显示logback实时日志
- 系统缓存管理 将redis的操作可视化,提供对redis的基本操作
- Sql监控 采用 druid 监控数据库访问性能

Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--websocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!--Spring boot end-->

<!--jedis-->
Expand Down
845 changes: 12 additions & 833 deletions sql/eladmin.sql

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/main/java/me/zhengjie/AppRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;

/**
* @author jie
* @date 2018/11/15 9:20:19
*/
@SpringBootApplication
//开启定时任务
@EnableScheduling
@EnableTransactionManagement
@EnableWebSocketMessageBroker
public class AppRun {

public static void main(String[] args) {

SpringApplication.run(AppRun.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected void configure(HttpSecurity httpSecurity) throws Exception {
.authorizeRequests()

.antMatchers("/auth/**").permitAll()
.antMatchers("/websocket/**").anonymous()

.antMatchers("/druid/**").anonymous()
// swagger start
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/me/zhengjie/monitor/config/LogFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.zhengjie.monitor.config;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import me.zhengjie.monitor.domain.LogMessage;

import java.text.DateFormat;
import java.util.Date;

/**
* 定义Logfilter拦截输出日志
* @author jie
* @date 2018-12-24
*/
public class LogFilter extends Filter<ILoggingEvent>{

@Override
public FilterReply decide(ILoggingEvent event) {
String exception = "";
IThrowableProxy iThrowableProxy1 = event.getThrowableProxy();
if(iThrowableProxy1!=null){
exception = "<span class='excehtext'>"+iThrowableProxy1.getClassName()+" "+iThrowableProxy1.getMessage()+"</span></br>";
for(int i=0; i<iThrowableProxy1.getStackTraceElementProxyArray().length;i++){
exception += "<span class='excetext'>"+iThrowableProxy1.getStackTraceElementProxyArray()[i].toString()+"</span></br>";
}
}
LogMessage loggerMessage = new LogMessage(
event.getMessage()
, DateFormat.getDateTimeInstance().format(new Date(event.getTimeStamp())),
event.getThreadName(),
event.getLoggerName(),
event.getLevel().levelStr,
exception
);
LoggerQueue.getInstance().push(loggerMessage);
return FilterReply.ACCEPT;
}
}
52 changes: 52 additions & 0 deletions src/main/java/me/zhengjie/monitor/config/LoggerQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.zhengjie.monitor.config;

import me.zhengjie.monitor.domain.LogMessage;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* 创建一个阻塞队列,作为日志系统输出的日志的一个临时载体
* @author jie
* @date 2018-12-24
*/
public class LoggerQueue {

//队列大小
public static final int QUEUE_MAX_SIZE = 10000;

private static LoggerQueue alarmMessageQueue = new LoggerQueue();
//阻塞队列
private BlockingQueue blockingQueue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE);

private LoggerQueue() {
}

public static LoggerQueue getInstance() {
return alarmMessageQueue;
}

/**
* 消息入队
*
* @param log
* @return
*/
public boolean push(LogMessage log) {
return this.blockingQueue.add(log);//队列满了就抛出异常,不阻塞
}

/**
* 消息出队
*
* @return
*/
public LogMessage poll() {
LogMessage result = null;
try {
result = (LogMessage) this.blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
27 changes: 27 additions & 0 deletions src/main/java/me/zhengjie/monitor/config/VisitsScheduling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.zhengjie.monitor.config;

import me.zhengjie.monitor.service.VisitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* @author jie
* @date 2018-12-25
*/
@Component
@Async
public class VisitsScheduling {

@Autowired
private VisitsService visitsService;

/**
* 每天0:01运行
*/
@Scheduled(cron = "1 0 0 * * ?")
public void save(){
visitsService.save();
}
}
64 changes: 64 additions & 0 deletions src/main/java/me/zhengjie/monitor/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.zhengjie.monitor.config;


import lombok.extern.slf4j.Slf4j;
import me.zhengjie.monitor.domain.LogMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* 配置WebSocket消息代理端点,即stomp服务端
* @author jie
* @date 2018-12-24
*/
@Slf4j
@Configuration
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Autowired
private SimpMessagingTemplate messagingTemplate;

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket")
.setAllowedOrigins("*")
.withSockJS();
}

/**
* 推送日志到/topic/pullLogger
*/
@PostConstruct
public void pushLogger(){
ExecutorService executorService= Executors.newFixedThreadPool(2);
Runnable runnable=new Runnable() {

@Override
public void run() {
while (true) {
try {
LogMessage log = LoggerQueue.getInstance().poll();
if(log!=null){
if(log.getClassName().equals("jdbc.resultsettable")){
log.setBody("<br><pre>"+log.getBody()+"</pre>");
}
if(messagingTemplate!=null){
messagingTemplate.convertAndSend("/topic/logMsg",log);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
executorService.submit(runnable);
executorService.submit(runnable);
}
}
20 changes: 20 additions & 0 deletions src/main/java/me/zhengjie/monitor/domain/LogMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.zhengjie.monitor.domain;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
* @author 郑杰
* @date 2018-12-24
*/
@Data
@AllArgsConstructor
public class LogMessage {

private String body;
private String timestamp;
private String threadName;
private String className;
private String level;
private String exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class VisitsController {

@PostMapping(value = "/visits")
public ResponseEntity create(){
visitsService.save(RequestHolder.getHttpServletRequest());
visitsService.count(RequestHolder.getHttpServletRequest());
return new ResponseEntity(HttpStatus.CREATED);
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/me/zhengjie/monitor/service/VisitsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
*/
public interface VisitsService {

/**
* 提供给定时任务,每天0点执行
*/
void save();

/**
* 新增记录
* @param request
*/
@Async
void save(HttpServletRequest request);
void count(HttpServletRequest request);

/**
* 获取数据
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package me.zhengjie.monitor.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.common.utils.IpUtil;
import me.zhengjie.common.utils.RequestHolder;
import me.zhengjie.common.utils.TimeUtil;
import me.zhengjie.monitor.domain.Logging;
import me.zhengjie.monitor.domain.Visits;
import me.zhengjie.monitor.repository.LoggingRepository;
import me.zhengjie.monitor.repository.VisitsRepository;
Expand All @@ -15,7 +12,6 @@
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -37,20 +33,28 @@ public class VisitsServiceImpl implements VisitsService {
private LoggingRepository loggingRepository;

@Override
public void save(HttpServletRequest request) {
public void save() {
LocalDate localDate = LocalDate.now();
Visits visits = visitsRepository.findByDate(localDate.toString());
if(visits != null){
visits.setPvCounts(visits.getPvCounts()+1);
long ipCounts = loggingRepository.findIp(localDate.toString(), localDate.plusDays(1).toString());
visits.setIpCounts(ipCounts);
}else {
if(visits == null){
visits = new Visits();
visits.setWeekDay(TimeUtil.getWeekDay());
visits.setPvCounts(1L);
visits.setIpCounts(1L);
visits.setDate(localDate.toString());
}
}

@Override
public void count(HttpServletRequest request) {

// 部署到线上后,可将save()删除
save();
LocalDate localDate = LocalDate.now();
Visits visits = visitsRepository.findByDate(localDate.toString());
visits.setPvCounts(visits.getPvCounts()+1);
long ipCounts = loggingRepository.findIp(localDate.toString(), localDate.plusDays(1).toString());
visits.setIpCounts(ipCounts);
visitsRepository.save(visits);
}

Expand All @@ -59,13 +63,6 @@ public Object get() {
Map map = new HashMap();
LocalDate localDate = LocalDate.now();
Visits visits = visitsRepository.findByDate(localDate.toString());
if(visits == null){
try {
save(RequestHolder.getHttpServletRequest());
} catch (Exception e) {
log.error(e.getMessage());
}
}
List<Visits> list = visitsRepository.findAllVisits(localDate.minusDays(6).toString(),localDate.plusDays(1).toString());

long recentVisits = 0, recentIp = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface MenuRepository extends JpaRepository<Menu, Long>, JpaSpecificat
* @param roleSet
* @return
*/
Set<Menu> findByRoles(Set<Role> roleSet);
Set<Menu> findByRolesOrderBySoft(Set<Role> roleSet);

/**
* findByPid
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/zhengjie/system/rest/MenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public ResponseEntity getMenus(@PathVariable Long id){
@GetMapping(value = "/menus/build")
public ResponseEntity buildMenus(HttpServletRequest request){
User user = userService.findByName(jwtTokenUtil.getUserName(request));

List<MenuDTO> menuDTOList = menuService.findByRoles(user.getRoles());
return new ResponseEntity(menuService.buildMenus((List<MenuDTO>)menuService.buildTree(menuDTOList).get("content")),HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MenuDTO findById(long id) {

@Override
public List<MenuDTO> findByRoles(Set<Role> roles) {
Set<Menu> menus = menuRepository.findByRoles(roles);
Set<Menu> menus = menuRepository.findByRolesOrderBySoft(roles);
return menus.stream().map(menuMapper::toDto).collect(Collectors.toList());
}

Expand Down Expand Up @@ -141,6 +141,7 @@ public List<MenuVo> buildMenus(List<MenuDTO> menuDTOS) {
if(menuDTO.getPid().equals(0L)){
//一级目录需要加斜杠,不然访问不了
menuVo.setPath("/" + menuDTO.getPath());
menuVo.setName(null);
}
menuVo.setComponent(StrUtil.isEmpty(menuDTO.getComponent())?"Layout":menuDTO.getComponent());
}
Expand Down
Loading

0 comments on commit 4765785

Please sign in to comment.