springcloud 之 redis消息队列

(1) redis 作为消息队列,
(2) eureka client 作为服务发现组件
(3) netty作为长连接中心

  1. application.yml

    server:
    port: 8099
    # Redis数据库索引(默认为0)
    spring:
    redis:
    #数据库索引
    database: 0
    host: 10.1.3.49
    port: 6379
    password: 123456
    #连接超时时间
    timeout: 10000
    application:
    name: tdgisnetty
    eureka:
    instance:
    hostname: eureka-server
    prefer-ip-address: true
    leaseRenewalIntervalInSeconds: 10
    client:
    service-url:
    defaultZone: http://localhost:8761/eureka/

2.CachingConfigurerSupport

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    /**
     * 选择redis作为默认缓存工具
     * @param redisTemplate
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        return rcm;
    }

    /**
     * retemplate相关配置
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        // 值采用json序列化
        template.setValueSerializer(jacksonSeial);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();

        return template;
    }

    /**
     * 对hash类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    /**
     * 对redis字符串类型数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * 对链表类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    /**
     * 对无序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 对有序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }
    
public class RedisKeyUtil {
        /**
         * redis的key
         * 形式为:
         * 表名:主键名:主键值:列名
         *
         * @param tableName 表名
         * @param majorKey 主键名
         * @param majorKeyValue 主键值
         * @param column 列名
         * @return
         */
        public static String getKeyWithColumn(String tableName,String majorKey,String majorKeyValue,String column){
            StringBuffer buffer = new StringBuffer();
            buffer.append(tableName).append(":");
            buffer.append(majorKey).append(":");
            buffer.append(majorKeyValue).append(":");
            buffer.append(column);
            return buffer.toString();
        }
        /**
         * redis的key
         * 形式为:
         * 表名:主键名:主键值
         *
         * @param tableName 表名
         * @param majorKey 主键名
         * @param majorKeyValue 主键值
         * @return
         */
        public static String getKey(String tableName,String majorKey,String majorKeyValue){
            StringBuffer buffer = new StringBuffer();
            buffer.append(tableName).append(":");
            buffer.append(majorKey).append(":");
            buffer.append(majorKeyValue).append(":");
            return buffer.toString();
        }
    }
  1. Controller Controllerone
    @RestController
    @CrossOrigin
    @RequestMapping(“controllerone/”)
    @Api(value = “controllerone”, description = “测试”)
    public class controllerone {

     @Autowired
     private RedisTemplate redisTemplate;
     @Resource
     private RedisService redisService;
     @Autowired
     private ListOperations<String, Object> listOperations;
     public void runNettyClient()
     {
         try{
         final NettyClient c = NettyClient.getInstance();
         ChannelFuture cf = c.getChannelFuture();
             cf.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));
             cf.channel().writeAndFlush(Unpooled.copiedBuffer("666".getBytes()));
             cf.channel().writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
         cf.channel().closeFuture().sync();
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }
     @ApiOperation("用户登录")
     @GetMapping("/login")
     public Object login() {
         try {
             JSONObject ret = new JSONObject();
             runNettyClient();
             ret.put("hh","");
             return ret;
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new JSONObject();
     }
     @ApiOperation("提交任务")
     @GetMapping("/postTask")
     public Object postTask() {
         try {
             TaskModel taskmodel = new TaskModel();
             taskmodel.setTaskname("lige");
             taskmodel.setTaskPaths("D://lige");
             taskmodel.setTaskState(1);
             taskmodel.setTaskflags(1);
             listOperations.leftPush("list:task",taskmodel);
    
            // common.redisQueue.pushFromHead(taskmodel);
             JSONObject ret = new JSONObject();
             ret.put("info","");
             return ret;
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new JSONObject();
     }
     @ApiOperation("消费任务")
     @GetMapping("/popTask")
     public Object popTask() {
         try {
             TaskModel taskmodel = new TaskModel();
             listOperations.leftPop("lige");
             taskmodel = (TaskModel)listOperations.leftPop("list:task");
             JSONObject ret = new JSONObject();
             ret.put("obj",taskmodel);
             return ret;
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new JSONObject();
     }
    

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值