spring cloud中redis的訂閱與發布

2021-09-29 15:05:12 字數 4621 閱讀 9333

專案a作為發布者,專案b作為訂閱者

先在專案a中,註冊redistemplate,作為訊息佇列的發布者

package com.bbg.domainmanager.common.redis;

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.stringredistemplate;

/** * @classname publisherconfig 註冊redistemplate,作為訊息佇列的發布者

* @description todo

* @author wyc

* @date 2019/7/29 9:28

* @version 1.0.1

**/@configuration

public

class

publisherconfig

}

然後,在service中,新建發布的方法redisservice

package com.bbg.domainmanager.service;

/** * @classname redisserver

* @description todo

* @author wyc

* @date 2019/7/29 11:13

* @version 1.0.1

**/public

inte***ce

redisserver

和redisserverimpl

package com.bbg.domainmanager.service.impl;

import com.bbg.domainmanager.common.constantconfiguration;

import com.bbg.domainmanager.service.redisserver;

import org.apache.poi.ss.formula.functions.t;

import org.slf4j.logger;

import org.slf4j.logge***ctory;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.data.redis.core.stringredistemplate;

import org.springframework.stereotype.service;

/** * @classname redisserverimpl

* @description todo

* @author wyc

* @date 2019/7/29 11:14

* @version 1.0.1

**/@service

public

class

redisserverimpl

implements

redisserver

catch

(exception e)

}}

列舉,發布與訂閱的頻道

package com.bbg.domainmanager.common;

/** * @classname constantconfiguration

* @description todo

* @author wang

* @date 2019/7/26 16:30

* @version 1.0.1

**/public

class

constantconfiguration

在controller中,使用

//在更新快取之後,需要去通知

string str = redisserver.

sendmsg((

newjsonarray

(listdomain)).

tostring()

);

以上,發布者**就緒 下面是訂閱者b專案

訊息訂閱者配置類

package com.cloud.domain.common.redis;

/** * @title: $

* @package $

* @description: $

* @author xwq

* @date 2019/8/7 000715:30

*/import com.cloud.domain.common.constantconfiguration;

import org.springframework.boot.autoconfigure.autoconfigureafter;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.data.redis.connection.redisconnectionfactory;

import org.springframework.data.redis.listener.patterntopic;

import org.springframework.data.redis.listener.redismessagelistenercontainer;

import org.springframework.data.redis.listener.adapter.messagelisteneradapter;

/** * 訊息訂閱者配置類

* @author xwq

* @create 2019-08-07 15:30

**/@configuration

@autoconfigureafter()

public

class

subscriberconfig

/** * 建立訊息監聽容器

** @param redisconnectionfactory

* @param messagelisteneradapter

* @return

*/@bean

public redismessagelistenercontainer getredismessagelistenercontainer

(redisconnectionfactory redisconnectionfactory, messagelisteneradapter messagelisteneradapter)

}

constantconfiguration.topic_username就是列舉,和發布者要一樣的頻道,然後@autoconfigureafter()中的receiver.class就是訂閱者在收到訊息後的處理方法

receiver.class

package com.cloud.domain.common.redis;

/** * @title: $

* @package $

* @description: $

* @author xwq

* @date 2019/8/7 000715:28

*/import org.slf4j.logger;

import org.slf4j.logge***ctory;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.data.redis.connection.message;

import org.springframework.data.redis.connection.messagelistener;

import org.springframework.data.redis.core.stringredistemplate;

import org.springframework.data.redis.serializer.redisserializer;

import org.springframework.stereotype.component;

/** * reids接受 訂閱

* @author xwq

* @create 2019-08-07 15:28

**/@component

public

class

receiver

implements

messagelistener

}

這個只是個簡單的使用,更加複雜的還在研究中

SpringCloud中的gateWay 閘道器

1 api閘道器介紹 2 spring cloud gateway 3 spring cloud gateway核心概念 所有服務,都需要在nacos中註冊服務 包括閘道器 注意版本號 服務註冊nacos org.springframework.cloud groupid spring cloud ...

SpringCloud中eureka治理元件的原理

當新增了乙個eureka client元件到乙個springboot工程時,依賴,屬性,註解會啟動乙個eureka client程序,完成和服務端互動 1.1註冊 程式啟動時會攜帶自身的資訊,包括服務名稱,包括ip位址,包括埠號,等引數,以http協議請求訪問 defaultzone 可以通過 eu...

springcloud中的gateway閘道器

引入依賴 com.atguigu groupid common utils artifactid 0.0.1 snapshot version dependency org.springframework.cloud groupid spring cloud starter alibaba naco...