网关总数同步
This commit is contained in:
parent
259f1b757a
commit
8e69ca0526
|
@ -0,0 +1,10 @@
|
||||||
|
DROP TABLE IF EXISTS `t_api_count_history`;
|
||||||
|
CREATE TABLE `t_api_count_history` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`version` bigint COMMENT '数据版本号,从0递增',
|
||||||
|
`current_count`bigint COMMENT '当前总数,只有最新一条有效',
|
||||||
|
`history_count`bigint COMMENT '保存历史重启的数量',
|
||||||
|
`create_time` datetime NULL DEFAULT NULL,
|
||||||
|
`update_time` datetime NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
|
) ENGINE = InnoDB COMMENT = '网关历史调用总数';
|
|
@ -5,6 +5,7 @@ import cn.hutool.core.net.URLEncoder;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import io.renren.modules.gateway.service.MonitorServiceV2;
|
||||||
import io.renren.modules.monitor.entity.Result;
|
import io.renren.modules.monitor.entity.Result;
|
||||||
import io.renren.modules.resource.dao.AttrDao;
|
import io.renren.modules.resource.dao.AttrDao;
|
||||||
import io.renren.modules.resource.dao.ResourceDao;
|
import io.renren.modules.resource.dao.ResourceDao;
|
||||||
|
@ -62,6 +63,9 @@ public class MonitorController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private SysDeptDao sysDeptDao;
|
private SysDeptDao sysDeptDao;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MonitorServiceV2 monitorServiceV2;
|
||||||
|
|
||||||
// @RequestMapping("/metrics/**")
|
// @RequestMapping("/metrics/**")
|
||||||
void forward(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
void forward(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||||
//类似nginx匹配前缀
|
//类似nginx匹配前缀
|
||||||
|
@ -326,4 +330,11 @@ public class MonitorController {
|
||||||
return Result.success(selectCount);
|
return Result.success(selectCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/gateway-monitor/getCallCount")
|
||||||
|
@ApiOperation("查询总api调用总量")
|
||||||
|
public Result getCallCount(){
|
||||||
|
Long callCount = monitorServiceV2.getCallCount();
|
||||||
|
return Result.success(callCount);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package io.renren.modules.gateway.entity;
|
package io.renren.modules.gateway.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import io.renren.common.entity.BaseEntity;
|
import io.renren.common.entity.BaseEntity;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,7 +25,11 @@ import java.io.Serializable;
|
||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
@TableName(value = "t_api_count_history", autoResultMap = true)
|
@TableName(value = "t_api_count_history", autoResultMap = true)
|
||||||
public class ApiCountHistoryEntity extends BaseEntity implements Serializable {
|
public class ApiCountHistoryEntity implements Serializable {
|
||||||
|
|
||||||
|
@TableId
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据版本递增
|
* 数据版本递增
|
||||||
|
@ -38,4 +45,9 @@ public class ApiCountHistoryEntity extends BaseEntity implements Serializable {
|
||||||
* 历史数据量
|
* 历史数据量
|
||||||
*/
|
*/
|
||||||
private Long historyCount;
|
private Long historyCount;
|
||||||
|
|
||||||
|
@TableField(fill = FieldFill.INSERT)
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
private Date updateTime;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,15 @@ import io.renren.modules.gateway.entity.ApiCountHistoryEntity;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MonitorServiceV2 {
|
public class MonitorServiceV2 {
|
||||||
|
@ -19,12 +24,28 @@ public class MonitorServiceV2 {
|
||||||
@Value("${hisense.gateway.url}")
|
@Value("${hisense.gateway.url}")
|
||||||
private String gatewayDomain;
|
private String gatewayDomain;
|
||||||
|
|
||||||
|
@Value("${hisense.gateway.sync-enabled}")
|
||||||
|
private Boolean enableSync;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApiCountHistoryDao apiCountHistoryDao;
|
private ApiCountHistoryDao apiCountHistoryDao;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init(){
|
||||||
|
if (enableSync) {
|
||||||
|
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
|
||||||
|
service.scheduleAtFixedRate(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
fetchCallCount();
|
||||||
|
}
|
||||||
|
}, 0, 60, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void fetchCallCount(){
|
public void fetchCallCount(){
|
||||||
Long currentTime = System.currentTimeMillis() / 1000;
|
Long currentTime = System.currentTimeMillis() / 1000;
|
||||||
String query = "sum(apigateway_http_status)";
|
String query = "sum(apigateway_http_status)";
|
||||||
|
@ -71,7 +92,7 @@ public class MonitorServiceV2 {
|
||||||
}else if (currentCount < apiCountHistoryEntity.getCurrentCount()){
|
}else if (currentCount < apiCountHistoryEntity.getCurrentCount()){
|
||||||
//保存旧值
|
//保存旧值
|
||||||
apiCountHistoryEntity.setHistoryCount(apiCountHistoryEntity.getCurrentCount());
|
apiCountHistoryEntity.setHistoryCount(apiCountHistoryEntity.getCurrentCount());
|
||||||
apiCountHistoryEntity.setCreateDate(new Date());
|
apiCountHistoryEntity.setUpdateTime(new Date());
|
||||||
apiCountHistoryDao.updateById(apiCountHistoryEntity);
|
apiCountHistoryDao.updateById(apiCountHistoryEntity);
|
||||||
//新增记录
|
//新增记录
|
||||||
ApiCountHistoryEntity newHistoryEntity = new ApiCountHistoryEntity();
|
ApiCountHistoryEntity newHistoryEntity = new ApiCountHistoryEntity();
|
||||||
|
|
|
@ -14,6 +14,7 @@ census:
|
||||||
# 海信网关
|
# 海信网关
|
||||||
hisense:
|
hisense:
|
||||||
gateway:
|
gateway:
|
||||||
|
sync-enabled: false
|
||||||
url: http://devtest-security-app.hismarttv.com:8080
|
url: http://devtest-security-app.hismarttv.com:8080
|
||||||
# Tomcat
|
# Tomcat
|
||||||
server:
|
server:
|
||||||
|
|
Loading…
Reference in New Issue