merge
This commit is contained in:
@@ -8,5 +8,6 @@ package com.xwbing.demo;
|
||||
*/
|
||||
public class Ademo {
|
||||
public static void main(String[] args) {
|
||||
String[] s={"a","b"};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,8 +72,6 @@ public class DruidDataSourceConfig {
|
||||
return registration;
|
||||
}
|
||||
|
||||
/*servlet*****************************************************filter*/
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean webStatFilter() {
|
||||
logger.info("注册druid监控信息采集webStatFilter ======================= ");
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.xwbing.rabbit;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import lombok.Data;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
@@ -15,24 +19,19 @@ import org.springframework.context.annotation.Scope;
|
||||
* 作者: xiangwb
|
||||
* 说明: 配置类
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "spring.rabbitmq")
|
||||
public class RabbitConfig {
|
||||
@Value("${spring.rabbitmq.host}")
|
||||
private String host;
|
||||
@Value("${spring.rabbitmq.port}")
|
||||
private String port;
|
||||
@Value("${spring.rabbitmq.username}")
|
||||
private String username;
|
||||
@Value("${spring.rabbitmq.password}")
|
||||
private String password;
|
||||
@Value("${spring.rabbitmq.virtual-host}")
|
||||
private String virtualHost;
|
||||
@Value("${spring.rabbitmq.connection-timeout}")
|
||||
private int timeout;
|
||||
@Value("${spring.rabbitmq.publisher-confirms}")
|
||||
private int connectionTimeout;
|
||||
private boolean publisherConfirms;
|
||||
@Value("${spring.rabbitmq.publisher-returns}")
|
||||
private boolean publisherReturns;
|
||||
private final Logger logger = LoggerFactory.getLogger(RabbitConfig.class);
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
@@ -41,7 +40,7 @@ public class RabbitConfig {
|
||||
connectionFactory.setUsername(username);
|
||||
connectionFactory.setPassword(password);
|
||||
connectionFactory.setVirtualHost(virtualHost);
|
||||
connectionFactory.setConnectionTimeout(timeout);
|
||||
connectionFactory.setConnectionTimeout(connectionTimeout);
|
||||
//如果要进行消息回调,则这里必须要设置为true
|
||||
connectionFactory.setPublisherConfirms(publisherConfirms);
|
||||
connectionFactory.setPublisherReturns(publisherReturns);
|
||||
@@ -50,12 +49,30 @@ public class RabbitConfig {
|
||||
|
||||
/**
|
||||
* 因为要设置回调类,所以应是prototype类型,如果是singleton类型,则回调类为最后一次设置
|
||||
* ConfirmCallback接口用于实现消息发送到RabbitMQ交换器后接收ack回调 即消息发送到exchange ack
|
||||
* ReturnCallback接口用于实现消息发送到RabbitMQ 交换器,但无相应队列与交换器绑定时的回调 即消息发送不到任何一个队列中 ack
|
||||
*/
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
|
||||
// rabbitTemplate.setEncoding("UTF-8");
|
||||
rabbitTemplate.setMandatory(true);
|
||||
//相应交换机接收后异步回调
|
||||
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
|
||||
if (ack) {
|
||||
logger.info("交换机接收信息成功,id:{}", correlationData.getId());
|
||||
} else {
|
||||
logger.error("交换机接收信息失败:{}", cause);
|
||||
}
|
||||
});
|
||||
//无相应队列与交换机绑定异步回调
|
||||
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
|
||||
byte[] body = message.getBody();
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
String msg = new String(message.getBody());
|
||||
logger.error("消息:{} 发送失败, 应答码:{} 原因:{} 交换机:{} 路由键:{}", msg, replyCode, replyText, exchange, routingKey);
|
||||
});
|
||||
return rabbitTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,10 @@ package com.xwbing.rabbit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.rabbit.support.CorrelationData;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
@@ -18,39 +16,16 @@ import java.util.UUID;
|
||||
* 作者: xiangwb
|
||||
* 说明: 生产者
|
||||
*/
|
||||
|
||||
//public class Sender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
|
||||
@Component
|
||||
public class Sender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
|
||||
public class Sender {
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
private final Logger logger = LoggerFactory.getLogger(Sender.class);
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
//用于实现消息发送到RabbitMQ交换机后接收ack回调。
|
||||
rabbitTemplate.setConfirmCallback(this);
|
||||
//用于实现消息发送到RabbitMQ交换机,但无相应队列与交换器绑定时的回调。
|
||||
rabbitTemplate.setReturnCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 相应交换机接收后异步回调
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (ack) {
|
||||
logger.info("交换机接收信息成功:{}", correlationData);
|
||||
} else {
|
||||
logger.error("交换机接收信息失败:{}", cause);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 无相应队列与交换机绑定异步回调
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
|
||||
String msg = new String(message.getBody());
|
||||
logger.error("field 无相应队列接收信息:{}", msg);
|
||||
public void sendM(String[] msg) {
|
||||
send(msg, RabbitConstant.CONTROL_EXCHANGE, "aa");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,8 +49,8 @@ public class TestControl {
|
||||
private final Logger logger = LoggerFactory.getLogger(TestControl.class);
|
||||
|
||||
@GetMapping("send")
|
||||
public void send(@RequestParam String[] msg) {
|
||||
sender.sendMessage(msg);
|
||||
public void sendM(@RequestParam String[] msg) {
|
||||
sender.sendM(msg);
|
||||
}
|
||||
|
||||
@LogInfo("redis功能测试")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.xwbing.handler;
|
||||
|
||||
import com.xwbing.annotation.LogInfo;
|
||||
import com.xwbing.util.IpUtil;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
@@ -10,8 +11,16 @@ import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 说明: web层日志记录切面
|
||||
@@ -41,10 +50,24 @@ public class WebLogAspect {
|
||||
@Before(value = "pointCutWithMsg(logInfo)", argNames = "joinPoint,logInfo")
|
||||
public void before(JoinPoint joinPoint, LogInfo logInfo) {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
//接收到请求,记录请求内容
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
//获取ip
|
||||
String ip = IpUtil.getIpAddr(request);
|
||||
//注解信息
|
||||
String info = logInfo.value();
|
||||
//获取相应类名
|
||||
String targetName = joinPoint.getTarget().getClass().getName();
|
||||
//获取方法名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
logger.info("{}/{}:{} started", targetName, methodName, info);
|
||||
//获取参数
|
||||
Object[] args = joinPoint.getArgs();
|
||||
List<Object> params = new ArrayList<>();
|
||||
if (args != null && args.length > 0) {
|
||||
params = Arrays.stream(args).filter(o -> !(o instanceof HttpServletRequest || o instanceof HttpServletResponse)).collect(Collectors.toList());
|
||||
}
|
||||
logger.info("{}/{}:{} started 参数:{}", targetName, methodName, info, params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,8 +54,11 @@ spring:
|
||||
password: guest
|
||||
virtual-host: /
|
||||
connection-timeout: 60000 #毫秒
|
||||
publisher-confirms: true
|
||||
publisher-returns: true
|
||||
publisher-confirms: true #支持发布确认
|
||||
publisher-returns: true #支持发布返回
|
||||
cache:
|
||||
channel:
|
||||
size: 1
|
||||
#log
|
||||
logging:
|
||||
config: classpath:log4j2.xml
|
||||
|
||||
Reference in New Issue
Block a user