日志-day2

SpringMVC 拦截器

创建一个类(使用组件注解 ),实现 handlerIntercepter、重写 prehandle方法
允许通过则返回 true,否则 false
@Component
public class MyIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = request.getHeader("authorization");
        System.out.println("进入拦截");
        return true;
    }
}
创建一个类,实现 WebMVCConfigure,重写 addIntercepter,声明 上面 处理拦截的对象的自动注入,并将该对象注册为拦截器处理
@Configuration
public class SpringMVCConfig implements WebMvcConfigurer {

    @Autowired
    private  MyIntercepter intercepter;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(intercepter);
    }
}

gateway网关服务

gateway 作为网关,也是一项微服务,是为其他服务模块“看门”的,内部的结构除了启动类,一般就是过滤器,用于放行符合规则的一些请求,过滤掉不合法请求,该服务重在配置文件的编写

application.yml

gateway:
      routes:
        - id: userservice   #全局唯一的,第一个服务。
          uri: lb://userservice  # lb:负载均衡,//userservice:路由的目标服务名
          predicates: # 路由配置
            - Path=/user/**,/address/**   # 允许访问的资源路径,多个,用逗号分隔
        - id: itemservice   # 第二个服务
        ......  #略
      globalcors:   # 跨域
        add-to-simple-url-handler-mapping: true    #根据字面意思:是否添加到简易路径映射处理
        cors-configurations:  # 跨域属性
          '[/**]':
            allowedOrigins: # 允许跨域的请求,
              - "http://localhost:10001"   
              - "http://......"
            allowedHeaders: "*" # 允许在请求中携带的头信息
            allowCredentials: true   # 是否允许携带 cookie
            maxAge: 360000 # 跨域检测的有效期
            allowedMethods:  # 允许的请求方式
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
      default-filters:                    #默认过滤
        - AddRequestHeader=authorization,2  # 添加请求头,所有通过的请求都会携带上

Elasticsearch 搜索

这个玩意儿是真的不好学,索引库的创建是个麻烦事,一不小心就写错了

// 创建名称为 items 的索引库,并设置了自定义索引分词器
PUT /items
{
  "settings": {
    "analysis": {
      "analyzer": {
        "自定义的分词器名称": {
          "tokenizer": "ik_max_word",    ------------不重复分词
          "filter": "py"  ----------------------------拼音分词器
        },
        "completion_analyzer": {    ------------------用于自动补全(自定义的名称)
          "tokenizer": "keyword",
          "filter": "py"            -----------------指定分词器
        }
      },
      "filter": {---过滤策略
        "py": {
          "type": "pinyin",
          "keep_full_pinyin": false,  ------全拼?
          "keep_joined_full_pinyin": true,  ----拼音连续?
          "keep_original": true,               -----保持原始
          "limit_first_letter_length": 16,    ---限制首字母长度
          "remove_duplicated_term": true,        ----删除重复的
          "none_chinese_pinyin_tokenize": false   ---非中文拼音
        }
      }
    }
  },
  "mappings": {            --------字段映射
    "properties": {        ------属性
      "id":{
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "自定义分词器的名称",  ----分词时采用的分词策略
        "search_analyzer": "ik_smart",    ----搜索时采用的分词策略
        "copy_to": "all"
      },
      "price":{
        "type": "integer"
      },
      "commentCount":{
        "type": "integer"
      },
      "sold":{
        "type": "integer"
      },
      "stock":{
        "type": "integer",
        "index": false        -----是否建立索引  (不写这个的话,默认是创建索引的)
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "category":{
        "type": "keyword",
        "copy_to": "all"
      },
      "spec":{
        "type": "keyword",
        "copy_to": "all"
      },
      "image":{
        "type": "keyword",
        "index": false
      },
      "all":{
        "type": "text",
        "analyzer": "自定义分词器的名称",
        "search_analyzer": "ik_smart"
      },
      "suggestion":{            ------自动补全用的字段
          "type": "completion",
          "analyzer": "completion_analyzer" ----自定义的分词器
      }
    }
  }
}
// text  参与分词
// keyword  关键词,不可分词

搜索相关业务

数据导入(文档导入)

编写一个和索引库对应的字段的实体类,来封装pojo实体类,这种类称为 文档类

创建文档的请求:IndexRequest

// 关键代码
// id 要提供字符串类型的
// 文档对象 要转为 JSON 字符串
// XContentType   是 内容类型的指定
IndexRequest request = new IndexRequest("items");
            request.id(doc.getId().toString())
                    .source(JSON.toJSONString(doc),XContentType.JSON);

     RestClient.index(request,RequestOptions.DEFAULT);

搜索文档的请求:SearchRequest

这个挺复杂的,简单来讲,就是 精准匹配 + 可过滤字段

// 构建 bool 查询,,,聚合查询
        BoolQueryBuilder bool = QueryBuilders.boolQuery();

        // key 是必须参与查询匹配的字段
        String key = params.getKey();
        if (key == null || "".equals(key)) {
            // 没有参数,则查询全部
            bool.must(QueryBuilders.matchAllQuery());
        } else {
            // 从字段 all 中查询
            bool.must(QueryBuilders.matchQuery("all", key));
        }
 // 品牌
        if (params.getBrand() != null && !"".equals(params.getBrand())) {
            bool.filter(QueryBuilders.termQuery("brand", params.getBrand()));
        }

修改文档的请求:UpdateRequest

全量修改,就是先删掉原来的,重新添加,常用

删除文档的请求:DeleteRequest

请求对象创建之后,设置好参数,都通过 RestHighLevelClient 的对象 执行,

// 带上 id 执行删除的就可以了 
DeleteRequest delete = new DeleteRequest("items",id.toString());
                RestClient.delete(delete,RequestOptions.DEFAULT);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值