|
|
package com.inspect.tcpserver.cofiguration;
|
|
|
|
|
|
import com.inspect.tcpserver.constant.Constant;
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
|
|
import org.springframework.amqp.core.*;
|
|
|
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
@Configuration
|
|
|
public class WebConfig {
|
|
|
|
|
|
@Bean
|
|
|
public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory factory) {
|
|
|
RedisTemplate<String, T> template = new RedisTemplate<>();
|
|
|
template.setConnectionFactory(factory);
|
|
|
|
|
|
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
|
|
serializer.setObjectMapper(mapper);
|
|
|
|
|
|
// 使用StringRedisSerializer来序列化和反序列化redis的key值
|
|
|
template.setKeySerializer(new StringRedisSerializer());
|
|
|
template.setValueSerializer(serializer);
|
|
|
|
|
|
// Hash的key也采用StringRedisSerializer的序列化方式
|
|
|
template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
template.setHashValueSerializer(serializer);
|
|
|
|
|
|
template.afterPropertiesSet();
|
|
|
|
|
|
return template;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
|
|
|
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
|
|
|
|
|
|
return rabbitTemplate;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 申明交换机
|
|
|
*/
|
|
|
@Bean(Constant.EX_CHANGE_NAME)
|
|
|
public Exchange directExchange() {
|
|
|
// 申明路由交换机,durable:在rabbitmq重启后,交换机还在
|
|
|
return ExchangeBuilder.directExchange(Constant.EX_CHANGE_NAME).durable(true).build();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 申明Blog队列
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
@Bean(Constant.QUEUE_NAME)
|
|
|
public Queue deviceQueue() {
|
|
|
return new Queue(Constant.QUEUE_NAME, true);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 绑定交换机-路由键
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
@Bean
|
|
|
public Binding bindRouting(Queue deviceQueue, Exchange directExchange) {
|
|
|
return BindingBuilder.bind(deviceQueue).to(directExchange).with(Constant.ROUTING_KEY_NAME).noargs();
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public RestTemplate restTemplate() {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
return restTemplate;
|
|
|
}
|
|
|
}
|