欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

学成在线 第6天 讲义-页面发布 课程管理三

程序员文章站 2022-03-03 18:32:36
...

1.3 页面发布生产方 
1.3.1 需求分析
管理员通过 cms系统发布
页面发布的消费,cms系统作为页面发布的生产方。
需求如下: 
1、管理员进入管理界面点击页面发布,前端请求cms页面发布接口。
2cms页面发布接口执行页面静态化,并将静态化页面存储至GridFS中。 
3、静态化成功后,向消息队列发送页面发布的消息。 
1) 获取页面的信息及页面所属站点ID。 
2) 设置消息内容为页面ID。(采用json格式,方便日后扩展) 
3) 发送消息给ex_cms_postpage交换机,并将站点ID作为routingKey
1.3.2 RabbitMQ配置 
1、配置Rabbitmq的连接参数 
application.yml添加如下配置:

spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtualHost: /

2、在pom.xml添加依赖

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

3RabbitMQConfifig配置 
由于cms作为页面发布方要面对很多不同站点的服务器,面对很多页面发布队列,所以这里不再配置队列,只需要配置交换机即可。
cms工程只配置交换机名称即可。

package com.xuecheng
.manage_cms.config;
import org
.springframework.amqp
.core.*
;
import org
.springframework.beans.factory
.annotation.
Qualifier;
import org
.springframework.beans.factory
.annotation.Value;
import org
.springframework.context.annotation.Bean;
import org
.springframework.context.annotation.Configuration;
package com.xuecheng
.manage_cms_client.config;
import org
.springframework.amqp
.core.*
;
import org
.springframework.beans.factory
.annotation.
Qualifier;
import org
.springframework.beans.factory
.annotation.Value;
import org
.springframework.context.annotation.Bean;
import org
.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
//交换机的名称
public static final String EX_ROUTING_CMS_POSTPAGE=
"
ex_routing_cms_postpage
"
;
/
**
*
交换机配置使用direct类型
*
@return the exchange
*
/
@Bean(EX_ROUTING_CMS_POSTPAGE)
public Exchange EXCHANGE_TOPICS_INFORM() {
return ExchangeBuilder.directExchange(EX_ROUTING_CMS_POSTPAGE)
.durable(true)
.build();
}
}

1.3.3 Api接口 
api工程定义页面发布接口:

@ApiOperation(
"
发布页面
"
)
public ResponseResult post(String pageId);

1.3.4 PageService 
PageService中定义页面发布方法,代码如下:

//页面发布
public ResponseResult postPage(String pageId){
//执行静态化
String pageHtml =
this.
getPageHtml(pageId);
if(StringUtils.isEmpty(pageHtml)){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
}
//保存静态化文件
CmsPage cmsPage
=
saveHtml(pageId, pageHtml);
//发送消息
sendPostPage(pageId);
return new ResponseResult(CommonCode.SUCCESS);
}
//发送页面发布消息
private void sendPostPage(String pageId){
CmsPage cmsPage
=
this.
getById(pageId);
if(cmsPage
== null){
ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
}
Map<String,String> msgMap
= new HashMap<>();
msgMap
.
put(
"
pageId
"
,pageId);
//消息内容
String msg
=
JSON.toJSONString(msgMap);
//获取站点id作为routingKey
String siteId
=
cmsPage.
getSiteId();
//发布消息
this.rabbitTemplate.convertAndSend(RabbitmqConfig
.EX_ROUTING_CMS_POSTPAGE,siteId, msg);
}
//保存静态页面内容
private CmsPage saveHtml(String pageId,String content){
//查询页面
Optional<CmsPage> optional =
cmsPageRepository
.findById(pageId);
if(!optional.isPresent()){
ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
}
CmsPage cmsPage
=
optional.
get();
//存储之前先删除
String htmlFileId
=
cmsPage.
getHtmlFileId();
if(StringUtils.isNotEmpty(htmlFileId)){
gridFsTemplate.delete(Query
.
query(Criteria.where(
"
_id
"
)
.is(htmlFileId)));
}
//保存html文件到GridFS
InputStream inputStream = IOUtils.toInputStream(content);
ObjectId objectId
=
gridFsTemplate.store(inputStream, cmsPage.
getPageName());
//文件id
String fileId
=
objectId.toString();
//将文件id存储到cmspage中
cmsPage.setHtmlFileId(fileId);
cmsPageRepository
.save(cmsPage);
return cmsPage;
}

1.3.5 CmsPageController 
编写Controller实现api接口,接收页面请求,调用service执行页面发布。

@Override
@PostMapping(
"
/postPage/{pageId}
"
)
public ResponseResult post(@PathVariable(
"
pageId
"
) String pageId) {
return pageService.
postPage(pageId);
}

 

相关标签: 技术文章