• 黑马头条知识点总结


    黑马头条知识点总结



    前言

    本人跟着黑马的视频做了近一个月,过程有些仓促,所以来复盘一下重要知识点。


    一、使用的所有技术栈

    主要就是java的spring cloud项目。

    • Spring-Cloud-Gateway : 微服务之前架设的网关服务,实现服务注册中的API请求路由,以及控制流速控制和熔断处理都是常用的架构手段,而这些功能Gateway天然支持。

    cloud的服务网关还有zuul,Zuul是基于Servlet的实现,属于阻塞式编程。而SpringCloudGateway则是基于Spring5中提供的WebFlux,属于响应式编程的实现,具备更好的性能。

    • 运用Spring Cloud Alibaba Nacos作为项目中的注册中心和配置中心

    还能选择使用Eureka作为服务中心,nacos多个配置中心的功能,配置中心还有个Spring Cloud Config,不过我没接触过。

    • Nacos与eureka的共同点
      • 都支持服务注册和服务拉取
      • 都支持服务提供者心跳方式做健康检测
    • Nacos与Eureka的区别
      • Nacos支持服务端主动检测提供者状态:临时实例采用心跳模式,非临时实例采用主动检测模式
      • 临时实例心跳不正常会被剔除,非临时实例则不会被剔除
      • Nacos支持服务列表变更的消息推送模式,服务列表更新更及时
      • Nacos集群默认采用AP方式,当集群中存在非临时实例时,采用CP模式;Eureka采用AP方式

    看不懂?那看这个

    • 接口方式:Nacos与Eureka都对外暴露了Rest风格的API接口,用来实现服务注册、发现等功能

    • 实例类型:Nacos的实例有永久和临时实例之分;而Eureka只支持临时实例

    • 健康检测:Nacos对临时实例采用心跳模式检测,对永久实例采用主动请求来检测;Eureka只支持心跳模式

    • 服务发现:Nacos支持定时拉取和订阅推送两种模式;Eureka只支持定时拉取模式

    • 运用Spring Boot快速开发框架,构建项目工程;并结合Spring Cloud全家桶技术,实现后端个人中心、自媒体、管理中心等微服务。
    • 运用mybatis-plus作为持久层提升开发效率

    springboot和mybatisplus是老搭档了,这次做项目的时候我发现分页插件的配置类是可以直接写到启动类下面,用@Bean声明就能生效了,不需要再写个配置类,还挺方便的。 实体类也可以用代码生成器直接生成。
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
    }

    • 运用Kafka完成内部系统消息通知;与客户端系统消息通知;以及实时数据计算

    几种常见MQ的对比:头条项目需要的吞吐量很大,所以选择kafka

    RabbitMQActiveMQRocketMQKafka
    公司/社区RabbitApache阿里Apache
    开发语言ErlangJavaJavaScala&Java
    协议支持AMQP,XMPP,SMTP,STOMPOpenWire,STOMP,REST,XMPP,AMQP自定义协议自定义协议
    可用性一般
    单机吞吐量一般非常高
    消息延迟微秒级毫秒级毫秒级毫秒以内
    消息可靠性一般一般

    追求可用性:Kafka、 RocketMQ 、RabbitMQ

    追求可靠性:RabbitMQ、RocketMQ

    追求吞吐能力:RocketMQ、Kafka

    追求消息低延迟:RabbitMQ、Kafka

    • 运用Redis缓存技术,实现热数据的计算,提升系统性能指标

    • 使用Mysql存储用户数据,以保证上层数据查询的高性能

    • 使用Mongo存储用户热数据,以保证用户热数据高扩展和高性能指标

    • 使用freemarker模板引擎生成文章html

    • 使用minio存储图片和文章

    • 运用ES搜索技术,对冷数据、文章数据建立索引,以保证冷数据、文章查询性能

    • 接口工具postman、swagger、knife4j

    二、初始化项目

    2.1加密盐登录

    涉及身份验证的系统都需要存储用户的认证信息,常用的用户认证方式主要为用户名和密码的方式,为了安全起见,用户输入的密码需要保存为密文形式,可采用已公开的不可逆的hash加密算法,比如SHA256, SHA512, SHA3等,对于同一密码,同一加密算法会产生相同的hash值,这样,当用户进行身份验证时,也可对用户输入的明文密码应用相同的hash加密算法,得出一个hash值,然后使用该hash值和之前存储好的密文值进行对照,如果两个值相同,则密码认证成功,否则密码认证失败。

    由于密码是由用户设定的,在实际应用中,用户设置的密码复杂度可能不够高,同时不同的用户极有可能会使用相同的密码,那么这些用户对应的密文也会相同,这样,当存储用户密码的数据库泄露后,攻击者会很容易便能找到相同密码的用户,从而也降低了破解密码的难度,因此,在对用户密码进行加密时,需要考虑对密码进行掩饰,即使是相同的密码,也应该要保存为不同的密文,即使用户输入的是弱密码,也需要考虑进行增强,从而增加密码被攻破的难度,而使用带盐的加密hash值便能满足该需求。

    配置文件:
    bootstrap.yml

    server:
      port: 51801
    spring:
      application:
        name: leadnews-user
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.200.130:8848
          config:
            server-addr: 192.168.200.130:8848
            file-extension: yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在nacos中创建配置yaml文件

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/leadnews_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
        username: root
        password: root
    # 设置Mapper接口所对应的XML文件位置,如果你在Mapper接口中有自定义方法,需要进行该配置
    mybatis-plus:
      mapper-locations: classpath*:mapper/*.xml
      # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
      type-aliases-package: com.heima.model.user.pojos
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    logback.xml

    
    
    <configuration>
        
        <property name="LOG_HOME" value="e:/logs"/>
    
        
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
                <charset>utf8charset>
            encoder>
        appender>
    
        
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                
                <fileNamePattern>${LOG_HOME}/leadnews.%d{yyyy-MM-dd}.logfileNamePattern>
            rollingPolicy>
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
            encoder>
        appender>
    
        
        <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
            
            <discardingThreshold>0discardingThreshold>
            
            <queueSize>512queueSize>
            
            <appender-ref ref="FILE"/>
        appender>
    
    
        <logger name="org.apache.ibatis.cache.decorators.LoggingCache" level="DEBUG" additivity="false">
            <appender-ref ref="CONSOLE"/>
        logger>
        <logger name="org.springframework.boot" level="debug"/>
        <root level="info">
            
            <appender-ref ref="FILE"/>
            <appender-ref ref="CONSOLE"/>
        root>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    业务逻辑

    package com.heima.user.service.impl;
    
    import com.baomidou.mybatisplus.core.toolkit.Wrappers;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.heima.model.common.dtos.ResponseResult;
    import com.heima.model.common.enums.AppHttpCodeEnum;
    import com.heima.model.user.dtos.LoginDto;
    import com.heima.model.user.pojos.ApUser;
    import com.heima.user.mapper.ApUserMapper;
    import com.heima.user.service.ApUserService;
    import com.heima.utils.common.AppJwtUtil;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.util.DigestUtils;
    
    import java.util.HashMap;
    import java.util.Map;
    
    
    @Service
    public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    
        @Override
        public ResponseResult login(LoginDto dto) {
    
            //1.正常登录(手机号+密码登录)
            if (!StringUtils.isBlank(dto.getPhone()) && !StringUtils.isBlank(dto.getPassword())) {
                //1.1查询用户
                ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()));
                if (apUser == null) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
                }
    
                //1.2 比对密码
                String salt = apUser.getSalt();
                String pswd = dto.getPassword();
                pswd = DigestUtils.md5DigestAsHex((pswd + salt).getBytes());
                if (!pswd.equals(apUser.getPassword())) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
                }
                //1.3 返回数据  jwt
                Map<String, Object> map = new HashMap<>();
                map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
                apUser.setSalt("");
                apUser.setPassword("");
                map.put("user", apUser);
                return ResponseResult.okResult(map);
            } else {
                //2.游客  同样返回token  id = 0
                Map<String, Object> map = new HashMap<>();
                map.put("token", AppJwtUtil.getToken(0l));
                return ResponseResult.okResult(map);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    2.2网关

    (1)在heima-leadnews-gateway导入以下依赖

    pom文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
         <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
            dependency>
        <dependency>
            <groupId>io.jsonwebtokengroupId>
            <artifactId>jjwtartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (2)在heima-leadnews-gateway下创建heima-leadnews-app-gateway微服务

    引导类:

    package com.heima.app.gateway;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient  //开启注册中心
    public class AppGatewayApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AppGatewayApplication.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    bootstrap.yml

    server:
      port: 51601
    spring:
      application:
        name: leadnews-app-gateway
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.200.130:8848
          config:
            server-addr: 192.168.200.130:8848
            file-extension: yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在nacos的配置中心创建dataid为leadnews-app-gateway的yml配置

    spring:
      cloud:
        gateway:
          globalcors:
            add-to-simple-url-handler-mapping: true
            corsConfigurations:
              '[/**]':
                allowedHeaders: "*"
                allowedOrigins: "*"
                allowedMethods:
                  - GET
                  - POST
                  - DELETE
                  - PUT
                  - OPTION
          routes:
            # 平台管理
            - id: user
              uri: lb://leadnews-user
              predicates:
                - Path=/user/**
              filters:
                - StripPrefix= 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在网关微服务中新建全局过滤器:

    package com.heima.app.gateway.filter;
    
    
    import com.heima.app.gateway.util.AppJwtUtil;
    import io.jsonwebtoken.Claims;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang.StringUtils;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.core.Ordered;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.server.reactive.ServerHttpRequest;
    import org.springframework.http.server.reactive.ServerHttpResponse;
    import org.springframework.stereotype.Component;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
    
    @Component
    @Slf4j
    public class AuthorizeFilter implements Ordered, GlobalFilter {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            //1.获取request和response对象
            ServerHttpRequest request = exchange.getRequest();
            ServerHttpResponse response = exchange.getResponse();
    
            //2.判断是否是登录
            if(request.getURI().getPath().contains("/login")){
                //放行
                return chain.filter(exchange);
            }
    
    
            //3.获取token
            String token = request.getHeaders().getFirst("token");
    
            //4.判断token是否存在
            if(StringUtils.isBlank(token)){
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
    
            //5.判断token是否有效
            try {
                Claims claimsBody = AppJwtUtil.getClaimsBody(token);
                //是否是过期
                int result = AppJwtUtil.verifyToken(claimsBody);
                if(result == 1 || result  == 2){
                    response.setStatusCode(HttpStatus.UNAUTHORIZED);
                    return response.setComplete();
                }
            }catch (Exception e){
                e.printStackTrace();
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
    
            //6.放行
            return chain.filter(exchange);
        }
    
        /**
         * 优先级设置  值越小  优先级越高
         * @return
         */
        @Override
        public int getOrder() {
            return 0;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    2.3配置nginx

    ①:解压资料文件夹中的压缩包nginx-1.18.0.zip

    ②:解压资料文件夹中的前端项目app-web.zip

    ③:配置nginx.conf文件

    在nginx安装的conf目录下新建一个文件夹leadnews.conf,在当前文件夹中新建heima-leadnews-app.conf文件

    heima-leadnews-app.conf配置如下:

    upstream  heima-app-gateway{
        server localhost:51601;
    }
    
    server {
    	listen 8801;
    	location / {
    		root D:/workspace/app-web/;
    		index index.html;
    	}
    	
    	location ~/app/(.*) {
    		proxy_pass http://heima-app-gateway/$1;
    		proxy_set_header HOST $host;  # 不改变源请求头的值
    		proxy_pass_request_body on;  #开启获取请求体
    		proxy_pass_request_headers on;  #开启获取请求头
    		proxy_set_header X-Real-IP $remote_addr;   # 记录真实发出请求的客户端IP
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  #记录代理信息
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    nginx.conf 把里面注释的内容和静态资源配置相关删除,引入heima-leadnews-app.conf文件加载

    #user  nobody;
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    	# 引入自定义配置文件
    	include leadnews.conf/*.conf;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ④ :启动nginx

    ​ 在nginx安装包中使用命令提示符打开,输入命令nginx启动项目

    ​ 可查看进程,检查nginx是否启动

    ​ 重新加载配置文件:nginx -s reload

    三。文章通过freemarker生成html文件存入minio中

    freemarkerminio的使用
    依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        <dependency>
            <groupId>com.heimagroupId>
            <artifactId>heima-file-starterartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    测试代码,仅供参考,里面的模板文件还有一些实体类对象都是没有写的:

    package com.heima.article.test;
    
    
    import com.alibaba.fastjson.JSONArray;
    import com.baomidou.mybatisplus.core.toolkit.Wrappers;
    import com.heima.article.ArticleApplication;
    import com.heima.article.mapper.ApArticleContentMapper;
    import com.heima.article.mapper.ApArticleMapper;
    import com.heima.file.service.FileStorageService;
    import com.heima.model.article.pojos.ApArticle;
    import com.heima.model.article.pojos.ApArticleContent;
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import org.apache.commons.lang3.StringUtils;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import java.io.StringWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    @SpringBootTest(classes = ArticleApplication.class)
    @RunWith(SpringRunner.class)
    public class ArticleFreemarkerTest {
    
        @Autowired
        private Configuration configuration;
    
        @Autowired
        private FileStorageService fileStorageService;
    
    
        @Autowired
        private ApArticleMapper apArticleMapper;
    
        @Autowired
        private ApArticleContentMapper apArticleContentMapper;
    
        @Test
        public void createStaticUrlTest() throws Exception {
            //1.获取文章内容
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, 1390536764510310401L));
            if(apArticleContent != null && StringUtils.isNotBlank(apArticleContent.getContent())){
                //2.文章内容通过freemarker生成html文件
                StringWriter out = new StringWriter();
                Template template = configuration.getTemplate("article.ftl");
    
                Map<String, Object> params = new HashMap<>();
                params.put("content", JSONArray.parseArray(apArticleContent.getContent()));
    
                template.process(params, out);
                InputStream is = new ByteArrayInputStream(out.toString().getBytes());
    
                //3.把html文件上传到minio中
                String path = fileStorageService.uploadHtmlFile("", apArticleContent.getArticleId() + ".html", is);
    
                //4.修改ap_article表,保存static_url字段
                ApArticle article = new ApArticle();
                article.setId(apArticleContent.getArticleId());
                article.setStaticUrl(path);
                apArticleMapper.updateById(article);
    
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    四。内容安全阿里云接口

    在这里插入图片描述
    测试类:

    package com.heima.wemedia.test;
    
    import com.heima.common.aliyun.GreenImageScan;
    import com.heima.common.aliyun.GreenTextScan;
    import com.heima.file.service.FileStorageService;
    import com.heima.wemedia.WemediaApplication;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.Arrays;
    import java.util.Map;
    
    @SpringBootTest(classes = WemediaApplication.class)
    @RunWith(SpringRunner.class)
    public class AliyunTest {
    
        @Autowired
        private GreenTextScan greenTextScan;
    
        @Autowired
        private GreenImageScan greenImageScan;
    
        @Autowired
        private FileStorageService fileStorageService;
    
        @Test
        public void testScanText() throws Exception {
            Map map = greenTextScan.greeTextScan("我是一个好人,冰毒 ");
            System.out.println(map);
        }
    
        @Test
        public void testScanImage() throws Exception {
            byte[] bytes = fileStorageService.downLoadFile("http://101.42.235.20:9090/leadnews/2022/10/14/a9ecadb4ff6147e0b484b3ebafa4da02.png");
            Map map = greenImageScan.imageScan(Arrays.asList(bytes));
            System.out.println(map);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    5.使用延迟任务发布审核文章

    具体的思路标题链接中有,这里主要说一下几个问题。

    1. 什么是延迟队列?
    • 定时任务:有固定周期的,有明确的触发时间
    • 延迟队列:没有固定的开始时间,它常常是由一个事件触发的,而在这个事件触发之后的一段时间内触发另一个事件,任务可以立即执行,也可以延迟
    1. 为什么使用redis实现?
      主要是要考虑到数据需要持久化,防止因为意外宕机导致队列内未处理的数据丢失。比如使用jdk自带的DelayQueue 。结合Redis的有序集合(sorted set)和定时任务来实现延迟任务队列。
    2. 要在启动类下面配置redis乐观锁,防止不同线程的竞争问题,用在了任务日志表上,因为它需要频繁进行更新。
    /**
         * mybatis-plus乐观锁支持
         * @return
         */
    @Bean
    public MybatisPlusInterceptor optimisticLockerInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    4.9.3)redis分布式锁

    sexnx (SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。

    在这里插入图片描述

    这种加锁的思路是,如果 key 不存在则为 key 设置 value,如果 key 已存在则 SETNX 命令不做任何操作

    • 客户端A请求服务器设置key的值,如果设置成功就表示加锁成功
    • 客户端B也去请求服务器设置key的值,如果返回失败,那么就代表加锁失败
    • 客户端A执行代码完成,删除锁
    • 客户端B在等待一段时间后再去请求设置key的值,设置成功
    • 客户端B执行代码完成,删除锁
    在工具类CacheService中添加方法
    /**
     * 加锁
     *
     * @param name
     * @param expire
     * @return
     */
    public String tryLock(String name, long expire) {
        name = name + "_lock";
        String token = UUID.randomUUID().toString();
        RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
        RedisConnection conn = factory.getConnection();
        try {
    
            //参考redis命令:
            //set key value [EX seconds] [PX milliseconds] [NX|XX]
            Boolean result = conn.set(
                    name.getBytes(),
                    token.getBytes(),
                    Expiration.from(expire, TimeUnit.MILLISECONDS),
                    RedisStringCommands.SetOption.SET_IF_ABSENT //NX
            );
            if (result != null && result)
                return token;
        } finally {
            RedisConnectionUtils.releaseConnection(conn, factory,false);
        }
        return null;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    修改未来数据定时刷新的方法,如下:

    /**
     * 未来数据定时刷新
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void refresh(){
    
        String token = cacheService.tryLock("FUTURE_TASK_SYNC", 1000 * 30);
        if(StringUtils.isNotBlank(token)){
            log.info("未来数据定时刷新---定时任务");
    
            //获取所有未来数据的集合key
            Set<String> futureKeys = cacheService.scan(ScheduleConstants.FUTURE + "*");
            for (String futureKey : futureKeys) {//future_100_50
    
                //获取当前数据的key  topic
                String topicKey = ScheduleConstants.TOPIC+futureKey.split(ScheduleConstants.FUTURE)[1];
    
                //按照key和分值查询符合条件的数据
                Set<String> tasks = cacheService.zRangeByScore(futureKey, 0, System.currentTimeMillis());
    
                //同步数据
                if(!tasks.isEmpty()){
                    cacheService.refreshWithPipeline(futureKey,topicKey,tasks);
                    log.info("成功的将"+futureKey+"刷新到了"+topicKey);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    4.10)数据库同步到redis
    1. redis连接池
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- redis依赖commons-pool 这个依赖一定要添加 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    文章搜索

    使用ElasticSearch搜索,使用Mongodb保存搜索历史

    总结

  • 相关阅读:
    POJ 1328 简单贪心算法
    修改element-ui源码给el-dialog添加全屏功能
    第八章 泛型
    opencv入门笔记(二)
    如何实现一个 Paxos
    澳大利亚量子计算制造商SQC获5000万美元投资用于硅量子计算
    Linux 追踪技术 ftrace 简介(一)
    PAT 1032 Sharing
    数据库管理-第220期 Oracle的高可用-03(20240715)
    网络安全(黑客)自学
  • 原文地址:https://blog.csdn.net/dsafghljk/article/details/127732126