logback.xml文件配置
{"source":"%c-%L","createtime":"%d{yyyy-MM-dd HH:mm:ss}","thread":"%thread","level":"%-5level", "logTrackId": "%X{logTrackId}", "requestUri":"%X{requestUri}", "msg":"%msg"}
${rabbitmqHost}
${rabbitmqPort}
${rabbitmqUsername}
${rabbitmqPassword}
${rabbitmqvirtualHost}
true
direct
log.exchange.direct
logDirectQueue
true
UTF-8
false
false
NON_PERSISTENT
yml文件配置
# rabbitmq
rabbitmq:
host: 132.33.228.149
username: admin
password: admin@2023$
port: 5673
#虚拟host 可以不设置,使用server默认host
virtual-host: /
connection-timeout: 10000
listener:
simple:
prefetch: 1 # 每次只能获取一条,处理完成才能获取下一条
acknowledge-mode: manual #一定改为要手动确认模式
代码配置rabbitmq信息
package com.xx.weixin.rabbitmq;
import java.util.HashMap;
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.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 作者 srp
* @version 创建时间服务器托管网:2023-12-1 16:42:41
*
*/
@Configuration
public class RabbitMqLogConfig {
@Autowired
private RabbitAdmin rabbitAdmin;
//强制用此方法生成自己希望的队列
@Bean
public Queue logDirectQueue() {
//设置过期时间,以毫秒为单位
HashMap map = new HashMap();
map.put("x-message-ttl", 60000);
//true,false跟logback.xml对应
Queue queue = new Queue( "logDirectQueue", true,false,false,map);
rabbitAdmin.declareQueue(queue);
return queue;
}
@Bean
public DirectExchange logDirectExchange() {
//true,false跟logback.xml对应
return new DirectExchange("log.exchange.direct", false, false);
}
/**
* 根据路由键绑定队列到交换器上
*
* @return
*/
@Bean
public Binding logDirectBinding() {
return BindingBuilder.bind(logDirectQueue()).to(logDire服务器托管网ctExchange()).with("logDirectQueue");
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory defaultConnectionFactory){
return new RabbitAdmin(defaultConnectionFactory);
}
}
测试客户端
package com.xx.pyt.confg;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import com.rabbitmq.client.Channel;
///**
//* @author 作者 srp
//* @version 创建时间:2023-12-1 16:45:44
//*
//*/
@Component
public class DirectConsumer {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "logDirectQueue", durable = "false"),
exchange = @Exchange(name = "log.exchange.direct", durable = "false", type = "direct"),key ="logDirectQueue"))
@RabbitHandler
public void handleMessage(String msg, Channel channel, Message message) throws IOException {
try {
// 处理消息逻辑
processMessage(msg);
// 手动ack确认
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
// 手动nack拒绝,并要求重新投递
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
log.error(e.getMessage(), e);
}
}
private void processMessage(String message) {
// 模拟处理消息过程
log.info("Processing message: " + message);
}
}
此外,如果要对日志进行链路标记,可以是用MDC
package com.xxm.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.MDC;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import com.ylkh.admin.entity.MyUser;
import com.ylkh.security.dto.JwtUserDto;
import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;
/**
* @author 作者 srp
* @version 创建时间:2022年1月4日 下午12:52:43
*
*/
@Slf4j
@Component
@WebFilter(urlPatterns = "/**",filterName = "tlFilter")
public class TraceLogLocalFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.debug("链路过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
if(SecurityContextHolder.getContext().getAuthentication()!=null&&!"anonymousUser".equals(SecurityContextHolder.getContext().getAuthentication().getPrincipal())) {
//log.info(""+SecurityContextHolder.getContext().getAuthentication().getPrincipal());
JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
MyUser myUser = jwtUserDto.getMyUser();
MDC.put("user", myUser.getUserName());
}
String logtrackId = UUID.randomUUID(true).toString();
if(!request.getRequestURI().contains(".css")&&!request.getRequestURI().contains(".js")&&!request.getRequestURI().contains(".html"))
//添加MDC日志
{
MDC.put("logTrackId",logtrackId);
MDC.put("requestUri", request.getRequestURI());
}
try {
filterChain.doFilter(servletRequest, servletResponse);
}finally{
//移除MDC日志
MDC.remove(logtrackId);
}
// log.info("过滤器执行完成");
}
@Override
public void destroy() {
log.warn("过滤器销毁");
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
当我们用RAG构建一个知识库问答应用的时候,总是希望知识库里面灌的数据越多,问答的效果越好,事实真是如此吗?这篇文章给大家答案。 引言 在人工智能问答系统的发展中,RAG(Retrieval-Augmented Generation)技术以其独特的检索增强生成…