WSL2官网视频教程

Docker ON WSL2安装

安装WSL2

升级至WIN10最新版本

使用WINDOWS预览体验内部版本

  • 打开windows设置
  • 搜索"预览",选择[WINDOWS预览体验计划设置]
  • 获取
  • 登录并链接微软帐户
  • 选择"快"

启用扩展功能

  • 适用于 Linux 的 Windows 子系统
  • 虚拟机平台
  • 重启电脑

微软应用商店

  • 下载Ubuntu18.04 LTS

打开系统更新

  • 等待预览版下载完成(必须下载完毕,才能进行后面的操作,如果windows更新一直卡在检查更新,可以运行更新设置的疑难解答)

打开Ubuntu

  • 创建Ubuntu帐户
  • 创建sudo密码(sudo passwd root)

打开PowerShell

  • 输入wsl --set-version Ubuntu-18.04 2 (这条命令只有内部预览版才有效)
  • 输入 wsl -l -v 查看wsl版本
代码
PS C:\Users\L1yp> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-18.04           Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

安装Docker For WSL2

下载地址:https://liangyongpeng.com/html/download.html?last_id=0&pid=2

官方下载可能有点慢。

  • 注册一个docker的帐户,在Docker For Win登录。
  • 跳过DockerForWin的入门教程
  • 选择Resource→WSL INTEGRATION→启用Ubuntu18.04
  • 右下角Apply & Restart
  • 状态栏右键DockerForWin重启

进入Ubuntu设置

切换apt国内源

  • 备份源文件:输入: sudo cp /etc/apt/source.list /etc/apt/source.list.back
  • 编辑source.list,输入: vim /etc/apt/source.list
代码

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse

deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse

deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse

deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic stable

# deb-src [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic stable

deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

  • 输入sudo apt-get update
  • 输入sudo apt-get upgrade

安装docker-ce

  • 安装依赖
代码

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

  • 信任公钥
代码

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • 对于AMD64架构的计算机,添加软件仓库
代码

sudo add-apt-repository \

   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \

   $(lsb_release -cs\

   stable"

  • 安装
代码

sudo apt-get update

sudo apt-get install docker-ce

  • 安装完事,输入docker info,测试是否连接上docker daemon

安装ES

拉取ES镜像

https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

  • 输入 docker pull docker.elastic.co/elasticsearch/elasticsearch:7.6.2

创建ES容器

代码
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.6.2

安装IK分词器

  • 下载 IK分词器
  • 安装unzip,解压zip
  • mkdir ik
  • unzip xxx.zip -d ik/
  • docker cp ik [容器ID]:[ES插件目录]

查看ES容器的IP

docker inspect --format='{{.NetworkSettings.IPAddress}}' [ES容器ID]

拉取Kibana镜像

https://hub.docker.com/_/kibana

  • 输入 docker pull docker.elastic.co/kibana/kibana:7.6.2
  • docker run -d --name kibana -p 5601:5601 docker.elastic.co/kibana/kibana:7.6.2
  • docker exec -it kibana /bin/bash
  • cd config
  • vi kibana.yml
  • elasticsearch.hosts: [ "http://172.17.0.5:9200" ]

学习ES


创建Index


  • 创建Index
代码
PUT /article
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "long",
        "index": false
      },
      "title":{
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "preface":{
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "uri":{
        "type": "keyword",
        "index": false
      },
      "gmt_modified":{
        "type": "date",
        "index": true,
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
        "locale": "zh_CN",
        "doc_values": true
      }
    }
  }
}

############response############
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "article"
}

创建/更新 Document


https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html

以下两种,如果<_id>已存在,ES会更新文档并递增 _version 版本号

  • PUT /<index>/_doc/<_id>:指定文档ID

  • POST /<index>/_doc/:自动生成文档ID

代码
PUT /article/_doc/1
{
  "id": 1,
  "title": "了解如何使用常用线程池",
  "preface": "ThreadPoolExecutor ThreadPoolExecutor线程池状态线程池状态转换线程池参数线程池类型ThreadPoolExecutor线程池状态RUNNING:接受新任务并且处理阻塞队列里的任务SHUTDOWN:拒绝新任务但是处理阻塞队列里的任务STOP:拒绝新任务并抛弃阻塞队列里的任务,同时中断正在处理的任务TIDYING:所有任务都执行完(包含阻塞队列里的...",
  "uri": "/html/detail/threadpool.html",
  "gmt_midified": "2020-04-04 20:20:20"
}

############response############
{
  "_index" : "article", # 索引名称
  "_type" : "_doc", # ES现在只支持_doc文档类型
  "_id" : "1", # 已添加文档的唯一标识
  "_version" : 2, # 更新就递增
  "result" : "updated", # created/updated,创建成功/更新成功
  "_shards" : {
    "total" : 1, # 分片总数
    "successful" : 1, # 分片复制成功数量
    "failed" : 0 # 分片复制失败数量
  },
  "_seq_no" : 1, # 文档的操作序列化,防止乐观锁的ABA
  "_primary_term" : 1
}

以下两种,如果<_id>已存在,ES会抛出异常,相当于putIfAbsent

  • PUT /<index>/_create/<_id>
  • POST /<index>/_create/<_id>
代码
PUT /article/_create/1
{
  "id": 1,
  "title": "了解如何使用常用线程池",
  "preface": "ThreadPoolExecutor ThreadPoolExecutor线程池状态线程池状态转换线程池参数线程池类型ThreadPoolExecutor线程池状态RUNNING:接受新任务并且处理阻塞队列里的任务SHUTDOWN:拒绝新任务但是处理阻塞队列里的任务STOP:拒绝新任务并抛弃阻塞队列里的任务,同时中断正在处理的任务TIDYING:所有任务都执行完(包含阻塞队列里的...",
  "uri": "/html/detail/threadpool.html",
  "gmt_midified": "2020-04-04 20:20:20"
}

############response############
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[1]: version conflict, document already exists (current version [2])",
        "index_uuid" : "LTY0RwRcQQScWT6ay2ob4A",
        "shard" : "0",
        "index" : "article"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[1]: version conflict, document already exists (current version [2])",
    "index_uuid" : "LTY0RwRcQQScWT6ay2ob4A",
    "shard" : "0",
    "index" : "article"
  },
  "status" : 409
}

更新Document

通过Document ID更新Document

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

  • POST /<index>/_update/<_id>
代码
POST /article/_update/1
{
    "doc" : {
        "title": "了解如何使用常用线程池2"
    }
}

############response############
{
  "_index" : "article",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

通过脚本更新

代码
POST /article/_update/1
{
    "script" : {
        "source": "ctx._source.title += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    }
}

############response############
{
  "_index" : "article",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

upsert操作:

  • 若文档不存在,则插入[upsert]结构。
  • 若文档存在,则执行更新脚本。
代码
POST /article/_update/2
{
    "script" : {
        "source": "ctx._source.title += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
      "id": 2,
      "title": "了解如何使用常用线程池2",
      "preface": "ThreadPoolExecutor ThreadPoolExecutor线程池状态线程池状态转换线程池参数线程池类型ThreadPoolExecutor线程池状态RUNNING:接受新任务并且处理阻塞队列里的任务SHUTDOWN:拒绝新任务但是处理阻塞队列里的任务STOP:拒绝新任务并抛弃阻塞队列里的任务,同时中断正在处理的任务TIDYING:所有任务都执行完(包含阻塞队列里的2...",
      "uri": "/html/detail/threadpool.html",
      "gmt_midified": "2020-04-04 20:21:20"
    }

}

############response############
{
  "_index" : "article",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

############response2############
{
  "_index" : "article",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}

通过查询结果更新Document

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html

代码
POST /article/_update_by_query?conflicts=proceed
{
  "script": {
      "source": "ctx._source.title += params.count",
      "lang": "painless",
      "params" : {
        "count" : 4
      }
  },
  "query": {
      "match": {
        "title" : "线程池"
      }
  }
}

############response2############
# https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html#docs-update-by-query-response-body
{
  "took" : 17, # 操作耗时
  "timed_out" : false, 
  "total" : 2, # 成功处理的Document数量
  "updated" : 2, # 成功更新的Document数量
  "deleted" : 0, # 成功删除的Document数量
  "batches" : 1, # 通过查询更新拉回的滚动响应数
  "version_conflicts" : 0, # 按查询进行更新的版本冲突数
  "noops" : 0, # 由于用于查询更新的脚本返回 的值而忽略的文档数
  "retries" : {
    "bulk" : 0, # 
    "search" : 0
  },
  "throttled_millis" : 0, # 请求睡到符合 的毫秒数
  "requests_per_second" : -1.0, # 查询更新期间每秒有效执行的请求数
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

删除Document

通过Document ID删除

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html

  • DELETE /<index>/_doc/<_id>
代码
DELETE /article/_doc/2

{
  "_index" : "article",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 1
}

通过查询结果删除文档

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

代码
POST /article/_delete_by_query
{
  "query": {
    "match": {
      "title": "线程"
    }
  }
}

############response############
{
  "took" : 24,
  "timed_out" : false,
  "total" : 4,
  "deleted" : 4,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

查询

查看Index的全部Document

代码
GET /article/_search
{
    "query":{
        "match_all":{}
    }
}

Multi Get(mget)

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html

req

代码
GET /_mget
{
    "docs" : [
        {
            "_index" : "article",
            "_id" : "1"
        },
        {
            "_index" : "article",
            "_id" : "2"
        }
    ]
}
代码
GET /article/_mget
{
    "ids":["1", "2"]
}
代码
GET /article/_doc/_mget
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}
代码
GET /_mget
{
    "docs" : [
        {
            "_index" : "article",
            "_type" : "_doc",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "article",
            "_type" : "_doc",
            "_id" : "2",
            "_source" : ["title", "preface"]
        },
        {
            "_index" : "article",
            "_type" : "_doc",
            "_id" : "3",
            "_source" : {
                "include": ["title"]
            }
        }
    ]
}

resp

代码
{
  "docs" : [
    {
      "_index" : "article",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 3,
      "_seq_no" : 26,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "id" : 1,
        "title" : "了解如何使用常用线程池",
        "preface" : "ThreadPoolExecutor ThreadPoolExecutor线程池状态线程池状态转换线程池参数线程池类型ThreadPoolExecutor线程池状态RUNNING:接受新任务并且处理阻塞队列里的任务SHUTDOWN:拒绝新任务但是处理阻塞队列里的任务STOP:拒绝新任务并抛弃阻塞队列里的任务,同时中断正在处理的任务TIDYING:所有任务都执行完(包含阻塞队列里的...",
        "uri" : "/html/detail/threadpool.html",
        "gmt_midified" : "2020-04-04 20:20:20"
      }
    },
    {
      "_index" : "article",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 3,
      "_seq_no" : 27,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "id" : 2,
        "title" : "了解JVM垃圾回收的细节",
        "preface" : "JVM垃圾回收 垃圾收集理论对象已死吗?引用计数算法 给对象添加一个引用计数器,每当有一个地方引用它时,计数器加1,引用失效计数器减1,任何时刻计数器为0的对象就是不可能再被使用的。应用场景:微软的COM(Component Object Model)技术ActionScript3的FlashPlayer缺点:无法解决循环引用。可达性分析算法 通过一系列GC Roots的对象...",
        "uri" : "/html/detail/jvm-gc.html",
        "gmt_midified" : "2020-04-04 20:20:20"
      }
    }
  ]
}

查询和过滤


  • filter用来过滤结果集,不会影响匹配分数
代码
GET /article/_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "了解"        }}
      ],
      "filter": [ 
        { "range": { "gmt_modified": { "gte": "2020-03-04 20:20:20" }}}
      ]
    }
  }
}


resp

代码
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.7046783,
    "hits" : [
      {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.7046783,
        "_source" : {
          "id" : 1,
          "title" : "了解如何使用常用线程池",
          "preface" : "ThreadPoolExecutor ThreadPoolExecutor线程池状态线程池状态转换线程池参数线程池类型ThreadPoolExecutor线程池状态RUNNING:接受新任务并且处理阻塞队列里的任务SHUTDOWN:拒绝新任务但是处理阻塞队列里的任务STOP:拒绝新任务并抛弃阻塞队列里的任务,同时中断正在处理的任务TIDYING:所有任务都执行完(包含阻塞队列里的...",
          "uri" : "/html/detail/threadpool.html",
          "gmt_modified" : "2020-04-04 20:20:20"
        }
      }
    ]
  }
}

筛选聚合


代码
POST /article/_search?size=0
{
    "aggs" : {
        "id" : {
            "filter" : { "match": { "title": "了解" } },
            "aggs" : {
                "avg_price" : { "avg" : { "field" : "id" } }
            }
        }
    }
}
代码
PUT /article
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "long",
        "index": false
      },
      "title":{
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "preface":{
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "uri":{
        "type": "keyword",
        "index": false
      },
      "gmt_modified":{
        "type": "date",
        "index": true,
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
        "locale": "zh_CN",
        "doc_values": true
      }
    }
  }
}


PUT /article/_doc/1
{
  "id" : 1,
  "title" : "了解如何使用常用线程池",
  "preface" : "ThreadPoolExecutor ThreadPoolExecutor线程池状态线程池状态转换线程池参数线程池类型ThreadPoolExecutor线程池状态RUNNING:接受新任务并且处理阻塞队列里的任务SHUTDOWN:拒绝新任务但是处理阻塞队列里的任务STOP:拒绝新任务并抛弃阻塞队列里的任务,同时中断正在处理的任务TIDYING:所有任务都执行完(包含阻塞队列里的...",
  "uri" : "/html/detail/threadpool.html",
  "gmt_modified" : "2020-04-04 20:20:20"
}



PUT /article/_doc/2
{
  "id" : 2,
  "title" : "了解JVM垃圾回收的细节",
  "preface" : "JVM垃圾回收 垃圾收集理论对象已死吗?引用计数算法 给对象添加一个引用计数器,每当有一个地方引用它时,计数器加1,引用失效计数器减1,任何时刻计数器为0的对象就是不可能再被使用的。应用场景:微软的COM(Component Object Model)技术ActionScript3的FlashPlayer缺点:无法解决循环引用。可达性分析算法 通过一系列GC Roots的对象...",
  "uri" : "/html/detail/jvm-gc.html",
  "gmt_modified" : "2020-02-04 20:20:20"
}



PUT /article/_doc/3
{
  "id" : 3,
  "title" : "Concurrenthashmap高级应用",
  "preface" : "ConcurrentHashMap高级应用 ConcurrentHashMap高级应用computeIfAbsentcomputeIfPresentcomputemergeConcurrentHashMap高级应用 一直以来想做的一个功能是,对用户调用API的次数做限制,由于调用很频繁,读写次数基本上相等,MySQL这种数据库肯定不能解决。 接触JAVA...",
  "uri" : "/html/detail/ConcurrentHashMap-Advanced.html",
  "gmt_modified" : "2020-03-04 20:20:20"
}


PUT /article/_doc/4
{
  "id" : 4,
  "title" : "破解最强文本编辑器Sublime3",
  "preface" : "sublime-3207 sublime 3207下载sublime3,高速下载修改hosts文件C:WindowsSystem32driversetc127.0.0.1 www.sublimetext.com127.0.0.1 sublimetext.com127.0.0.1 sublimehq.com127.0.0.1 telemetry.sub...",
  "uri" : "/html/detail/sublime-crack.html",
  "gmt_modified" : "2020-04-04 20:20:20"
}


未完待续....