java电商项目搭建-------商品详情页模块

努力好了,时间会给你答案。--------magic_guo

在一个电商项目中,访问频率最高的是商品详情页页面,而且商品详情页的变化评率不会太高(除非是搞活动的时候);那么访问频率高,然后再使用数据库来查询,频繁访问数据库,性能肯定达不到要求。因此另外一套解决方案就应用而生:
nginx+静态的商品详情页;

nginx:处理静态数据没秒钟可以到达10W次(官方数据);
在项目上线的时候,搭建一台nginx服务器,专门来处理静态页面的请求;

需要考虑的是,页面生成的时机,如果用户要访问商品的时候,再去生成页面,这样用户体验将会有一个极大的折扣,因此页面是在用户访问之前就已经生成了,那么到底在什么时候生成呢?答案是:在后台添加商品的时候;

流程图:
在这里插入图片描述
架构图:
在这里插入图片描述
还有一个问题,就是已经存入数据的商品,没有详情页,怎么去生成呢?
我们将数据库的所有商品数据查出来,然后直接调用item模块来生成,以达到一个同步的需求;

话不多说,看代码:
maven依赖:

 <dependencies>

<!--        <dependency>-->
<!--            <groupId>com.guo</groupId>-->
<!--            <artifactId>shop-feign</artifactId>-->
<!--        </dependency>-->

        <dependency>
            <groupId>com.guo</groupId>
            <artifactId>shop-common</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

配置:

spring:
  cloud:
    config:
      uri: http://localhost:9999
      profile: shop-item, log, mq, eureka-clien
      name: application
  rabbitmq:
    listener:
      simple:
        acknowledge-mode: manual  # 手动ACK

mq配置:

@Configuration
public class RabbitMqConfig {

    // 创建一个item队列
    @Bean
    public Queue itemQueue() {
        return new Queue(ShopConstants.ITEM_QUEUE, true, false, false);
    }

    // 创建一个交换机,或者直接导入商品模块的good_exchange
    @Bean
    public TopicExchange goodsExchange() {
        return new TopicExchange(ShopConstants.GOODS_EXCHANGE, true, false);
    }

    // 将队列绑定到交换机上
    public Binding itemQueueToGoodsExchange() {
        return BindingBuilder.bind(itemQueue()).to(goodsExchange()).with("goods.*");
    }
}

监听器配置:

@Configuration
@Slf4j
public class ItemQueueListener {

    @Autowired
    private freemarker.template.Configuration configuration;

    private ExecutorService executorService = Executors.newFixedThreadPool(5);

    @RabbitListener(queues = ShopConstants.ITEM_QUEUE)
    public void createItem(Goods goods, Channel channel, Message message) {
        executorService.submit(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                // 获取模板
                Template template = configuration.getTemplate("goodsItemTemplate.ftl");

                // 准备数据
                Map<String, Object> map = new HashMap<>();
                map.put("gname", goods.getGname());
                map.put("gorice", goods.getGprice());
                map.put("pngList", goods.getTempPng().split("\\|"));

                // 准备静态页面输出的位置
                String path = ItemQueueListener.class.getClassLoader().getResource("static").getPath();

                // 生成静态页面
                template.process(map, new FileWriter(path + File.separator + goods.getId() + ".html"));

                // 手动ACK
                try {
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

启动类:

@SpringBootApplication(scanBasePackages = "com.guo", exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
public class ShopItemApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShopItemApplication.class, args);
    }

}

完工!


本文章教学视频来自:https://www.bilibili.com/video/BV1tb4y1Q74E?p=3&t=125


静下心,慢慢来,会很快!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于SpringBoot+MyBatis实现。 后台管理系统包含商品管理、订单管理、会员管理、促销管理、运营管理、内容管理、统计报表、财务管理、权限管理、设置等模块。 #### 后端技术 技术 | 说明 | 官网 ----|----|---- Spring Boot | 容器+MVC框架 | [https://spring.io/projects/spring-boot](https://spring.io/projects/spring-boot) Spring Security | 认证和授权框架 | [https://spring.io/projects/spring-security](https://spring.io/projects/spring-security) MyBatis | ORM框架 | [http://www.mybatis.org/mybatis-3/zh/index.html](http://www.mybatis.org/mybatis-3/zh/index.html) MyBatisGenerator | 数据层代码生成 | [http://www.mybatis.org/generator/index.html](http://www.mybatis.org/generator/index.html) PageHelper | MyBatis物理分页插件 | [http://git.oschina.net/free/Mybatis_PageHelper](http://git.oschina.net/free/Mybatis_PageHelper) Swagger-UI | 文档生产工具 | [https://github.com/swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui) Hibernator-Validator | 验证框架 | [http://hibernate.org/validator/](http://hibernate.org/validator/) Elasticsearch | 搜索引擎 | [https://github.com/elastic/elasticsearch](https://github.com/elastic/elasticsearch) RabbitMq | 消息队列 | [https://www.rabbitmq.com/](https://www.rabbitmq.com/) Redis | 分布式缓存 | [https://redis.io/](https://redis.io/) MongoDb | NoSql数据库 | [https://www.mongodb.com/](https://www.mongodb.com/) Docker | 应用容器引擎 | [https://www.docker.com/](https://www.docker.com/) Druid | 数据库连接池 | [https://github.com/alibaba/druid](https://github.com/alibaba/druid) OSS | 对象存储 | [https://github.com/aliyun/aliyun-oss-java-sdk](https://github.com/aliyun/aliyun-oss-java-sdk) JWT | JWT登录支持 | [https://github.com/jwtk/jjwt](https://github.com/jwtk/jjwt) LogStash | 日志收集 | [https://github.com/logstash/logstash-logback-encoder](https://github.com/logstash/logstash-logback-encoder) Lombok | 简化对象封装工具 | [https://github.com/rzwitserloot/lombok](https://github.com/rzwitserloot/lombok) #### 前端技术 技术 | 说明 | 官网 ----|----|---- Vue | 前端框架 | [https://vuejs.org/](https://vuejs.org/) Vue-router | 路由框架 | [https://router.vuejs.org/](https://router.vuejs.org/) Vuex | 全局状态管理框架 | [https://vuex.vuejs.org/](https://vuex.vuejs.org/) Element | 前端UI框架 | [https://element.eleme.io/](https://element.eleme.io/) Axios | 前端HTTP框架 | [https://github.com/axios/axios](https://github.com/axios/axios) v-charts | 基于Echarts的图表框架 | [https://v-charts.js.org/](https://v-charts.js.org/) Js-cookie | cookie管理工具 | [https://github.com/js-cookie/js-cookie](https://github.com/js-cookie/js-cookie) nprogress | 进度条控件 | [https://github.com/rstacruz/nprogress](https://github.com/rstacruz/nprogress)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值