Commit 219a9283 authored by 赵啸非's avatar 赵啸非

修改网关日志

parent 73410c40
package com.mortals.xhx.module.skin.service.impl; package com.mortals.xhx.module.skin.service.impl;
import cn.hutool.core.exceptions.ExceptionUtil;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context; import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo; import com.mortals.framework.model.PageInfo;
...@@ -69,4 +70,8 @@ public class SkinTemplateServiceImpl extends AbstractCRUDServiceImpl<SkinTemplat ...@@ -69,4 +70,8 @@ public class SkinTemplateServiceImpl extends AbstractCRUDServiceImpl<SkinTemplat
skinFieldService.save(entity.getSkinFieldList(), context); skinFieldService.save(entity.getSkinFieldList(), context);
} }
} }
public static void main(String[] args) {
}
} }
\ No newline at end of file
...@@ -65,7 +65,8 @@ Accept: application/json ...@@ -65,7 +65,8 @@ Accept: application/json
###构建站点树 ###构建站点树
GET {{baseUrl}}/site/siteTree GET {{baseUrl}}/site/siteTree
Authorization: {{authToken}} #Authorization: {{authToken}}
Authorization: eyJhbGciOiJIUzI1NiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM1OmVjZTkyMWQ1MzY2NDRkMmRhZTU0YmU0ZjA4ODE0OWZkIn0.EXKwA8I8t5rK864aJqMEh51XZ8IPtjG3juV51mgJf80
Accept: application/json Accept: application/json
......
###皮肤属性字段配置列表 ###皮肤属性字段配置列表
POST {{baseUrl}}/skin/field/list POST {{baseUrl}}/skin/field/list
Authorization: {{authToken}}
Content-Type: application/json Content-Type: application/json
{ {
"page":1, "page":1,
"size":10 "size":10,
"skinId": 0
} }
......
...@@ -4,8 +4,8 @@ POST {{baseUrl}}/login/login ...@@ -4,8 +4,8 @@ POST {{baseUrl}}/login/login
Content-Type: application/json Content-Type: application/json
{ {
"loginName":"admin", "loginName":"xiaoyi",
"password":"admin", "password":"xiaoyi",
"securityCode":"8888" "securityCode":"8888"
} }
......
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
<profiles.kafka.brokers>192.168.0.251:9092</profiles.kafka.brokers> <profiles.kafka.brokers>192.168.0.251:9092</profiles.kafka.brokers>
<profiles.rabbitmq.host>192.168.0.98</profiles.rabbitmq.host> <profiles.rabbitmq.host>192.168.0.98</profiles.rabbitmq.host>
<profiles.rabbitmq.port>5672</profiles.rabbitmq.port> <profiles.rabbitmq.port>5672</profiles.rabbitmq.port>
<profiles.rabbitmq.username>taxi_mq</profiles.rabbitmq.username>
<profiles.rabbitmq.password>admin@2020</profiles.rabbitmq.password>
<profiles.rabbitmq.virtualhost>/test</profiles.rabbitmq.virtualhost>
<profiles.nacos.server-addr>192.168.0.252:8848</profiles.nacos.server-addr> <profiles.nacos.server-addr>192.168.0.252:8848</profiles.nacos.server-addr>
<profiles.nacos.group>DEFAULT_GROUP</profiles.nacos.group> <profiles.nacos.group>DEFAULT_GROUP</profiles.nacos.group>
<profiles.nacos.namespace>smart-gov</profiles.nacos.namespace> <profiles.nacos.namespace>smart-gov</profiles.nacos.namespace>
...@@ -89,10 +92,17 @@ ...@@ -89,10 +92,17 @@
<artifactId>spring-cloud-starter-loadbalancer</artifactId> <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency> </dependency>
<!-- <dependency> <!-- 实现对 RabbitMQ 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>--> </dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>-->
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
......
package com.mortals.xhx.base.framework.config;
import com.mortals.xhx.common.key.QueueKey;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.AsyncRabbitTemplate;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitConfig {
public Integer messageTtl = 86400000;
public Map<String, Object> args = new HashMap<>();
// 创建 Queue
@Bean
public Queue accessLogQueue() {
args.put("x-message-ttl", messageTtl);
return new Queue(QueueKey.ACCESS_LOG_QUEUE, // Queue 名字
true, // durable: 是否持久化
false, // exclusive: 是否排它
false,
args); // autoDelete: 是否自动删除
}
@Bean
public Queue bizLogQueue() {
args.put("x-message-ttl", messageTtl);
return new Queue(QueueKey.BIZ_LOG_QUEUE, // Queue 名字
true, // durable: 是否持久化
false, // exclusive: 是否排它
false, args); // autoDelete: 是否自动删除
}
@Bean
public Queue errorLogQueue() {
return new Queue(QueueKey.ERROR_LOG_QUEUE, // Queue 名字
true, // durable: 是否持久化
false, // exclusive: 是否排它
false, args); // autoDelete: 是否自动删除
}
@Bean
public Queue operationLogQueue() {
return new Queue(QueueKey.OPERATION_LOG_QUEUE, // Queue 名字
true, // durable: 是否持久化
false, // exclusive: 是否排它
false, args); // autoDelete: 是否自动删除
}
// 创建 Direct Exchange
@Bean
public DirectExchange exchange() {
return new DirectExchange(QueueKey.EXCHANGE,
true, // durable: 是否持久化
false); // exclusive: 是否排它
}
// 创建 Binding
@Bean
public Binding accessBinding() {
return BindingBuilder.bind(accessLogQueue()).to(exchange()).with(QueueKey.ACCESS_LOG_QUEUE);
}
@Bean
public Binding bizBinding() {
return BindingBuilder.bind(bizLogQueue()).to(exchange()).with(QueueKey.BIZ_LOG_QUEUE);
}
@Bean
public Binding errorBinding() {
return BindingBuilder.bind(errorLogQueue()).to(exchange()).with(QueueKey.ERROR_LOG_QUEUE);
}
@Bean
public Binding operBinding() {
return BindingBuilder.bind(operationLogQueue()).to(exchange()).with(QueueKey.OPERATION_LOG_QUEUE);
}
@Bean(name = "consumerBatchContainerFactory")
public SimpleRabbitListenerContainerFactory consumerBatchContainerFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
// 创建 SimpleRabbitListenerContainerFactory 对象
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
// 额外添加批量消费的属性
factory.setBatchListener(true);
factory.setBatchSize(10);
factory.setReceiveTimeout(30 * 1000L);
factory.setConsumerBatchEnabled(true);
return factory;
}
//修改系列和与反序列化转换器
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public AsyncRabbitTemplate asyncRabbitTemplate(RabbitTemplate rabbitTemplate) {
AsyncRabbitTemplate asyncRabbitTemplate = new AsyncRabbitTemplate(rabbitTemplate);
asyncRabbitTemplate.setReceiveTimeout(10000);
return asyncRabbitTemplate;
}
}
package com.mortals.xhx.base.framework.filter; package com.mortals.xhx.base.framework.filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
...@@ -15,11 +16,13 @@ import reactor.core.publisher.Mono; ...@@ -15,11 +16,13 @@ import reactor.core.publisher.Mono;
* @author: zxfei * @author: zxfei
* @date: 2022/6/20 16:59 * @date: 2022/6/20 16:59
*/ */
//@Component @Component
@Slf4j
public class GlobalCacheRequestFilter implements GlobalFilter, Ordered { public class GlobalCacheRequestFilter implements GlobalFilter, Ordered {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("GlobalCacheRequestFilter:{}",getOrder());
// GET DELETE 不过滤 // GET DELETE 不过滤
HttpMethod method = exchange.getRequest().getMethod(); HttpMethod method = exchange.getRequest().getMethod();
if (method == null || method == HttpMethod.GET || method == HttpMethod.DELETE) { if (method == null || method == HttpMethod.GET || method == HttpMethod.DELETE) {
......
package com.mortals.xhx.base.framework.filter; package com.mortals.xhx.base.framework.filter;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.mortals.xhx.base.framework.config.CustomGatewayProperties; import com.mortals.xhx.base.framework.config.CustomGatewayProperties;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -19,6 +20,7 @@ import org.springframework.web.util.UriComponentsBuilder; ...@@ -19,6 +20,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.sound.midi.Track;
import java.net.URI; import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
...@@ -32,19 +34,54 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.G ...@@ -32,19 +34,54 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.G
* @date: 2022/6/20 17:00 * @date: 2022/6/20 17:00
*/ */
@Slf4j @Slf4j
//@Component @Component
public class GlobalLogFilter implements GlobalFilter, Ordered { public class GlobalLogFilter implements GlobalFilter, Ordered {
@Autowired @Autowired
private CustomGatewayProperties customGatewayProperties; private CustomGatewayProperties customGatewayProperties;
private static final String START_TIME = "startTime"; private static final String START_TIME = "startTime";
private static final String TRACE_ID = "traceId";
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("GlobalLogFilter:{}",getOrder());
//保存请求链路,设置数据追踪id到header中
if (!customGatewayProperties.getRequestLog()) {
return chain.filter(exchange);
}
ServerHttpRequest request = exchange.getRequest();
String path = getOriginalRequestUrl(exchange);
String url = request.getMethod().name() + " " + path;
//判断请求类型
if (isJsonRequest(request)) {
//读取请求体后 内容需要重新设置进去
String jsonParam = resolveBodyFromRequest(request);
log.info("开始请求 => URL[{}],参数类型[json],参数:[{}]", url, jsonParam);
} else {
//非json类型,
MultiValueMap<String, String> parameterMap = request.getQueryParams();
if (MapUtil.isNotEmpty(parameterMap)) {
log.info("开始请求 => URL[{}],参数类型[param],参数:[{}]", url, JSON.toJSONString(parameterMap));
} else {
log.info("开始请求 => URL[{}],无参数", url);
}
}
exchange.getAttributes().put(START_TIME, System.currentTimeMillis());
exchange.getRequest().getHeaders().add(TRACE_ID, IdUtil.fastSimpleUUID());
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute(START_TIME);
if (startTime != null) {
long executeTime = (System.currentTimeMillis() - startTime);
log.info("结束请求 => URL[{}],耗时:[{}]毫秒", url, executeTime);
}
}));
return chain.filter(exchange); //return chain.filter(exchange);
/* if (!customGatewayProperties.getRequestLog()) { /* if (!customGatewayProperties.getRequestLog()) {
return chain.filter(exchange); return chain.filter(exchange);
......
package com.mortals.xhx.common.key;
/**
* rabbit 队列key定义
*/
public class QueueKey {
public static final String ACCESS_LOG_QUEUE = "ACCESS_LOG_QUEUE";
public static final String BIZ_LOG_QUEUE = "BIZ_LOG_QUEUE";
public static final String ERROR_LOG_QUEUE = "ERROR_LOG_QUEUE";
public static final String OPERATION_LOG_QUEUE = "OPERATION_LOG_QUEUE";
public static final String EXCHANGE = "LOG";
public static final String ROUTING_KEY = "LOG_ROUTING_KEY";
}
...@@ -13,6 +13,12 @@ spring: ...@@ -13,6 +13,12 @@ spring:
multipart: multipart:
maxFileSize: 100Mb maxFileSize: 100Mb
maxRequestSize: 1000Mb maxRequestSize: 1000Mb
rabbitmq:
host: @profiles.rabbitmq.host@
port: @profiles.rabbitmq.port@
username: @profiles.rabbitmq.username@
password: @profiles.rabbitmq.password@
virtualHost: @profiles.rabbitmq.virtualhost@
cloud: cloud:
# 网关配置 # 网关配置
gateway: gateway:
...@@ -56,6 +62,12 @@ spring: ...@@ -56,6 +62,12 @@ spring:
uri: lb://device-manager uri: lb://device-manager
predicates: predicates:
- Path=/m/** - Path=/m/**
# 日志服务
- id: log-manager
# uri: http://127.0.0.1:17211
uri: lb://log-manager
predicates:
- Path=/logservice/**
nacos: nacos:
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类 # Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
discovery: discovery:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment