Skip to content

Commit

Permalink
tensquare-mq
Browse files Browse the repository at this point in the history
  • Loading branch information
szlsay committed Oct 22, 2021
1 parent b54df25 commit a53471e
Show file tree
Hide file tree
Showing 40 changed files with 760 additions and 113 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
<!-- <version>${mybatisplus-spring-boot-starter.version}</version>-->
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
Expand All @@ -57,6 +56,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.tensquare.article;

import com.tensquare.util.IdWorker;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.IdWorker;

@SpringBootApplication
//配置Mapper包扫描
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tensquare.article.client;

import com.tensquare.article.pojo.Notice;
import com.tensquare.entity.Result;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.Key;
import java.util.List;
import java.util.Map;

Expand All @@ -29,7 +30,7 @@ public class ArticleController {
@RequestMapping(value = "thumbup/{articleId}", method = RequestMethod.PUT)
public Result thumbup(@PathVariable String articleId) {
//TODO 使用JWT鉴权的方式获取当前用户的id
String userId = "4";
String userId = "1";

//查询用户对文章的点赞信息,根据用户id和文章id
String key = "thumbup_article_" + userId + "_" + articleId;
Expand Down Expand Up @@ -88,7 +89,7 @@ public Result findByPage(@PathVariable Integer page,

//封装分页返回对象
PageResult<Article> pageResult = new PageResult<>(
(int)pageData.getTotal(), pageData.getRecords()
pageData.getTotal(), pageData.getRecords()
);

//返回数据
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tensquare.article.controller;

import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import entity.Result;
import entity.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.pojo.Notice;
import com.tensquare.util.IdWorker;
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.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +36,9 @@ public class ArticleService {
@Autowired
private RedisTemplate redisTemplate;

@Autowired
private RabbitTemplate rabbitTemplate;

public List<Article> findAll() {
Article article = articleDao.selectById(1);
return articleDao.selectList(null);
Expand Down Expand Up @@ -85,6 +94,11 @@ public void save(Article article) {
noticeClient.save(notice);
}

//发消息给RabbitMQ,就是新消息的通知
//第一个参数是交换机名,使用之前完成的订阅功能的交换机
//第二个参数是路由键,使用时文章作者的id作为路由键
//第三个参数是消息内容,这里只完成新消息提醒,内容是文章id
rabbitTemplate.convertAndSend("article_subscribe", userId, id);

}

Expand Down Expand Up @@ -135,6 +149,22 @@ public Boolean subscribe(String articleId, String userId) {
//根据文章id查询文章作者id
String authorId = articleDao.selectById(articleId).getUserid();

//1 创建Rabbit管理器
RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate.getConnectionFactory());

//2 声明Direct类型交换机,处理新增文章消息
DirectExchange exchange = new DirectExchange("article_subscribe");
rabbitAdmin.declareExchange(exchange);

//3 创建队列,每个用户都有自己的队列,通过用户id进行区分
Queue queue = new Queue("article_subscribe_" + userId, true);

//4 声明交换机和队列的绑定关系,需要确保队列只收到对应作者的新增文章消息
// 通过路由键进行绑定作者,队列只收到绑定作者的文章消息。
//第一个是队列,第二个是交换机,第三个是路由键作者id
Binding binding = BindingBuilder.bind(queue).to(exchange).with(authorId);


//存放用户订阅信息的集合key,里面存放作者id
String userKey = "article_subscribe_" + userId;
//存放作者订阅者信息的集合key,里面存放订阅者id
Expand All @@ -150,6 +180,9 @@ public Boolean subscribe(String articleId, String userId) {
//作者订阅者信息的集合中,删除订阅者
redisTemplate.boundSetOps(authorKey).remove(userId);

//如果取消订阅,删除队列绑定关系
rabbitAdmin.removeBinding(binding);

//返回false
return false;

Expand All @@ -160,6 +193,10 @@ public Boolean subscribe(String articleId, String userId) {
//在作者订阅者信息中,添加订阅者
redisTemplate.boundSetOps(authorKey).add(userId);

// 如果订阅,声明要绑定的队列
rabbitAdmin.declareQueue(queue);
// 添加绑定关系
rabbitAdmin.declareBinding(binding);
//返回true
return true;
}
Expand Down Expand Up @@ -190,5 +227,17 @@ public void thumpup(String articleId, String userId) {

//保存消息
noticeClient.save(notice);

//1 创建Rabbit管理器
RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate.getConnectionFactory());

//2 创建队列,每个作者都有自己的队列,通过作者id进行区分
Queue queue = new Queue("article_thumbup_" + article.getUserid(), true);
rabbitAdmin.declareQueue(queue);

//3 发消息到队里中
rabbitTemplate.convertAndSend("article_thumbup_" + article.getUserid(), articleId);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.tensquare.util.IdWorker;
import util.IdWorker;

import java.util.Date;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ spring:
database: commentdb
redis:
host: localhost
rabbitmq:
host: localhost
# Mybatis-Plus 配置
mybatis-plus:
# mapper-locations: classpath:/mapper/*Mapper.xml
# mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.tensquare.article.pojo
global-config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
<orderEntry type="library" name="Maven: io.github.openfeign:feign-hystrix:10.1.0" level="project" />
<orderEntry type="library" name="Maven: com.netflix.hystrix:hystrix-core:1.5.18" level="project" />
<orderEntry type="library" name="Maven: org.hdrhistogram:HdrHistogram:2.1.9" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-amqp:2.1.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-messaging:5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.amqp:spring-rabbit:2.1.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.amqp:spring-amqp:2.1.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.retry:spring-retry:1.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.rabbitmq:amqp-client:5.4.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.1.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.8" level="project" />
Expand Down

This file was deleted.

Loading

0 comments on commit a53471e

Please sign in to comment.