首页 文章 精选 留言 我的

精选列表

搜索[自动化研究],共10001篇文章
优秀的个人博客,低调大师

Appium滑动问题研究

一、Appium中,经常会遇到会遇到滑动操作,但往往用各种手势操作后还是滑动不了,今天主要讲下如何正确使用appium的手势操作。系统环境为最新的iOS 7.1+ Xcode 5.1 首先讲下滑动操作的几个基本方法。 1.swipe操作,主要用于缓慢拖动,代码示例 JavascriptExecutor js = (JavascriptExecutor) driver; HashMap<String, Double> swipeObject = new HashMap<String, Double>(); swipeObject.put("startX", startX); swipeObject.put("startY", startY); swipeObject.put("endX", endX); swipebject.put("endY", endY); swipeObject.put("duration", duration); swipeObject.put("element", Double.valueOf(((RemoteWebElement) element).getId())); js.executeScript("mobile: swipe", swipeObject); ①X,Y可为coordinator,也可以是percent,duration单位为秒 ②可以指定的element,也可以不指定 ③appium mac端有swipe的按钮可以试下 2.flick操作,类似swipe,但没有duration,用于快速滑动,如ViewController的切换,代码示例 JavascriptExecutor js = (JavascriptExecutor) driver; HashMap<String, Double> flickObject = new HashMap<String, Double>(); flickObject.put("startX", 0.8); flickObject.put("startY", 0.5); flickObject.put("endX", 0.2); flickObject.put("endY", 0.5); flickObject.put("element", Double.valueOf(((RemoteWebElement) element).getId())); js.executeScript("mobile: flick", flickObject);); 3.scroll操作,专为iOS 7.x而生,官方的解释如下 An unfortunate bug exists in the iOS 7.x Simulator where ScrollViews don't recognize gestures initiated by UIAutomation (which Appium uses under the hood for iOS). To work around this, we have provided access to a different function, scroll, which in many cases allows you to do what you wanted to do with a ScrollView, namely, scroll it! 简而言之,iOS 7的系统ScrollView无法识别手势操作,使用scroll方法可完美替代,代码见例子 二、接下来以三个不同app的引导图为例,分别为看游戏,云阅读和云音乐,演示下不同方法实现的滑动操作 1.看游戏,引导图以ScrollView引导,只需要使用srcoll方法即可 JavascriptExecutor js = (JavascriptExecutor) driver; HashMap<String, String> scrollObject = new HashMap<String, String>(); scrollObject.put("direction", "right"); js.executeScript("mobile: scroll", scrollObject 2.云音乐,引导图以ScrollView引导,分别为4张image Inspector中显示如下: 如上所示,如果使用swipe或flick方法是不可以滑动引导图的,而用Scroll的方向模式也不行,这里采用如下方法 JavascriptExecutor js = (JavascriptExecutor) driver; WebElement element = driver.findElementByXPath("4张image的xpath路径"); HashMap<String, String> scrollObject = new HashMap<String, String>(); scrollObject.put("element", ((RemoteWebElement) element).getId()); js.executeScript("mobile: scroll", scrollObject); 3.云阅读,云阅读的引导图并不是存在于ScrollView中,而是专门有一个UIAElement存放,那就只需要用swipe拖动这个UIAElement就好了,如图所示。 代码见swipe方法。 最新内容请见作者的GitHub页:http://qaseven.github.io/

优秀的个人博客,低调大师

elasticsearch 拼音检索能力研究

gitchennan/elasticsearch-analysis-lc-pinyin 配置参数少,功能满足需求。 对应版本 elasticsearch2.3.2 对应 elasticsearch-analysis-lc-pinyin 分支 2.4.2.1 或者 tag 2.2.2.1 创建一个类型 elasticsearch-analysis-lc-pinyin 的 README 是根据 elasticsearch5.0 编写的,给出的创建一个类型的语法如下 curl -XPOST http://localhost:9200/index/_mapping/brand -d' { "brand": { "properties": { "name": { "type": "text", "analyzer": "lc_index", "search_analyzer": "lc_search", "term_vector": "with_positions_offsets" } } } }' type=text 是 elasticsearch5.0 之后的类型,所以无法创建成功,稍作修改 type=text,使用如下语法创建一个类型 curl -XPOST http://localhost:9200/index/_mapping/brand -d' { "brand": { "properties": { "name": { "type": "string", "analyzer": "lc_index", "search_analyzer": "lc_search", "term_vector": "with_positions_offsets" } } } }' index 索引结构如下 { "index": { "aliases": {}, "mappings": { "brand": { "properties": { "name": { "type": "string", "term_vector": "with_positions_offsets", "analyzer": "lc_index", "search_analyzer": "lc_search" } } } }, "settings": { "index": { "creation_date": "1490152096129", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "Lp1sSHGhQZyZ57LKO5KwRQ", "version": { "created": "2030299" } } }, "warmers": {} } } 存入几条数据 curl -XPOST http://localhost:9200/index/brand/1 -d'{"name":"百度"}' curl -XPOST http://localhost:9200/index/brand/8 -d'{"name":"百度糯米"}' curl -XPOST http://localhost:9200/index/brand/2 -d'{"name":"阿里巴巴"}' curl -XPOST http://localhost:9200/index/brand/3 -d'{"name":"腾讯科技"}' curl -XPOST http://localhost:9200/index/brand/4 -d'{"name":"网易游戏"}' curl -XPOST http://localhost:9200/index/brand/9 -d'{"name":"大众点评"}' curl -XPOST http://localhost:9200/index/brand/10 -d'{"name":"携程旅行网"}' 查出目前的所有数据 http://localhost:9200/index/_search { "took": 70, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 7, "max_score": 1, "hits": [ { "_index": "index", "_type": "brand", "_id": "8", "_score": 1, "_source": { "name": "百度糯米" } }, { "_index": "index", "_type": "brand", "_id": "9", "_score": 1, "_source": { "name": "大众点评" } }, { "_index": "index", "_type": "brand", "_id": "10", "_score": 1, "_source": { "name": "携程旅行网" } }, { "_index": "index", "_type": "brand", "_id": "2", "_score": 1, "_source": { "name": "阿里巴巴" } }, { "_index": "index", "_type": "brand", "_id": "4", "_score": 1, "_source": { "name": "网易游戏" } }, { "_index": "index", "_type": "brand", "_id": "1", "_score": 1, "_source": { "name": "百度" } }, { "_index": "index", "_type": "brand", "_id": "3", "_score": 1, "_source": { "name": "腾讯科技" } } ] } } 插件自带分词器 lc_index 原文:lc_index : 该分词器用于索引数据时指定,将中文转换为全拼和首字,同时保留中文 分词器分词效果 curl -X POST -d '{ "analyzer" : "lc_index", "text" : ["刘德华"] }' "http://localhost:9200/lc/_analyze" { "tokens": [ { "token": "刘", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "l", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "德", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "d", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "华", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "h", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 } ] } 插件自带分词器 lc_search 原文:lc_search: 该分词器用于拼音搜索时指定,按最小拼音分词个数拆分拼音,优先拆分全拼 curl -X POST -d '{ "analyzer" : "lc_search", "text" : ["刘德华"] }' "http://localhost:9200/index/_analyze" { "tokens": [ { "token": "刘", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "德", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "华", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 } ] } 拼音全拼 搜索 baidu,结果正确 curl -X POST -d '{ "query": { "match": { "name": { "query": "baidu", "analyzer": "lc_search", "type": "phrase" } } }, "highlight" : { "pre_tags" : ["<tag1>"], "post_tags" : ["</tag1>"], "fields" : { "name" : {} } } }' "http://localhost:9200/index/brand/_search" { "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1.4054651, "hits": [ { "_index": "index", "_type": "brand", "_id": "8", "_score": 1.4054651, "_source": { "name": "百度糯米" }, "highlight": { "name": [ "<tag1>百度</tag1>糯米" ] } }, { "_index": "index", "_type": "brand", "_id": "1", "_score": 0.38356602, "_source": { "name": "百度" }, "highlight": { "name": [ "<tag1>百度</tag1>" ] } } ] } } 单字拼音全拼与中文混合 搜索 xie程lu行,结果正确 { "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 2.459564, "hits": [ { "_index": "index", "_type": "brand", "_id": "10", "_score": 2.459564, "_source": { "name": "携程旅行网" }, "highlight": { "name": [ "<tag1>携程旅行</tag1>网" ] } } ] } } 单字拼音首字母与中文混合 搜索 携cl行,结果正确 curl -X POST -d '{ "query": { "match": { "name": { "query": "携cl行", "analyzer": "lc_search", "type": "phrase" } } }, "highlight" : { "pre_tags" : ["<tag1>"], "post_tags" : ["</tag1>"], "fields" : { "name" : {} } } }' "http://localhost:9200/index/brand/_search" { "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 2.459564, "hits": [ { "_index": "index", "_type": "brand", "_id": "10", "_score": 2.459564, "_source": { "name": "携程旅行网" }, "highlight": { "name": [ "<tag1>携程旅行</tag1>网" ] } } ] } } 拼音首字母 搜索 albb,结果正确 curl -X POST -d '{ "query": { "match": { "name": { "query": "albb", "analyzer": "lc_search", "type": "phrase" } } }, "highlight" : { "pre_tags" : ["<tag1>"], "post_tags" : ["</tag1>"], "fields" : { "name" : {} } } }' "http://localhost:9200/index/brand/_search" { "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 2.828427, "hits": [ { "_index": "index", "_type": "brand", "_id": "2", "_score": 2.828427, "_source": { "name": "阿里巴巴" }, "highlight": { "name": [ "<tag1>阿里巴巴</tag1>" ] } } ] } } 结论 elasticsearch-analysis-lc-pinyin 按照全拼、首字母,拼音中文混合搜索 elasticsearch-analysis-pinyin v1.7.2 github 项目 elasticsearch-analysis-pinyin v1.7.2 完全是为 elasticsearch 2.3.2 服务 first_letter 改变 first_letter=prefix padding_char=" " curl -X POST -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "prefix", "padding_char" : " " } } } } } }' "http://localhost:9200/medcl" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl/_analyze" { "tokens": [ { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 }, { "token": "liu", "start_offset": 0, "end_offset": 3, "type": "word", "position": 1 }, { "token": "de", "start_offset": 0, "end_offset": 3, "type": "word", "position": 2 }, { "token": "hua", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 } ] } first_letter=append padding_char=" " curl -X POST -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "append", "padding_char" : " " } } } } } }' "http://localhost:9200/medcl2" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl2/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 }, { "token": "de", "start_offset": 0, "end_offset": 3, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 0, "end_offset": 3, "type": "word", "position": 2 }, { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 } ] } first_letter=only padding_char=" " curl -X POST -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "only", "padding_char" : " " } } } } } }' "http://localhost:9200/medcl3" 拼音效果如下 curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: 67015c0d-cd07-961b-4c46-da90f7d558d8" -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl3/_analyze" { "tokens": [ { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 } ] } first_letter=none padding_char=" " curl -X POST -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "none", "padding_char" : " " } } } } } }' "http://localhost:9200/medcl4" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl4/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 }, { "token": "de", "start_offset": 0, "end_offset": 3, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 0, "end_offset": 3, "type": "word", "position": 2 } ] } padding_char 改变 first_letter=prefix padding_char="" curl -X POST -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "prefix", "padding_char" : "" } } } } } }' "http://localhost:9200/medcl5" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl5/_analyze" { "tokens": [ { "token": "ldhliudehua", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 } ] } first_letter=append padding_char="" curl -X PUT -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "append", "padding_char" : "" } } } } } }' "http://localhost:9200/medcl7" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl7/_analyze" { "tokens": [ { "token": "liudehualdh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 } ] } first_letter=only padding_char="" curl -X PUT -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "only", "padding_char" : "" } } } } } }' "http://localhost:9200/medcl8" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl8/_analyze" { "tokens": [ { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 } ] } first_letter=none padding_char="" curl -X PUT -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "none", "padding_char" : "" } } } } } }' "http://localhost:9200/medcl9" 拼音效果如下 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl9/_analyze" { "tokens": [ { "token": "liudehua", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 } ] } 结论 elasticsearch 2.3.2 对应 elasticsearch-analysis-pinyin 1.7.2,pinyin 1.7.2 可配置参数有:first_letter 和 padding_char。 padding_char 的作用是将字符串按照什么字符分隔,比如 padding_char = " ",那么 刘德华 将先被分隔为 刘,德,华;如果 padding_char = "",那么 刘德华 将不会被分隔 first_letter 取值:prefix,append,only,none。 padding_char 与 first_letter 的组合会影响拼音输出的结果 elasticsearch-analysis-pinyin 2.x 分支 github 项目 elasticsearch-analysis-pinyin 2.x 分支 是为 elasticsearch 2.x 服务,经过测试 elasticsearch 2.3.2 也可以使用该插件。 官方文档中的说明 remove_duplicated_term when this option enabled, duplicated term will be removed to save index, eg: de的>de, default: false, NOTE: position related query maybe influenced keep_first_letter when this option enabled, eg: 刘德华>ldh, default: true keep_separate_first_letter when this option enabled, will keep first letters separately, eg: 刘德华>l,d,h, default: false, NOTE: query result maybe too fuzziness due to term too frequency limit_first_letter_length set max length of the first_letter result, default: 16 keep_full_pinyin when this option enabled, eg: 刘德华> [liu,de,hua], default: true keep_joined_full_pinyin when this option enabled, eg: 刘德华> [liudehua], default: false keep_none_chinese keep non chinese letter or number in result, default: true keep_none_chinese_together keep non chinese letter together, default: true, eg: DJ音乐家 -> DJ,yin,yue,jia, when set to false, eg: DJ音乐家 -> D,J,yin,yue,jia, NOTE: keep_none_chinese should be enabled first keep_none_chinese_in_first_letter keep non Chinese letters in first letter, eg: 刘德华AT2016->ldhat2016, default: true none_chinese_pinyin_tokenize break non chinese letters into separate pinyin term if they are pinyin, default: true, eg: liudehuaalibaba13zhuanghan -> liu,de,hua,a,li,ba,ba,13,zhuang,han, NOTE: keep_none_chinese and keep_none_chinese_together should be enabled first keep_original when this option enabled, will keep original input as well, default: false lowercase lowercase non Chinese letters, default: true trim_whitespace default: true 基准配置 基准配置参数 "keep_joined_full_pinyin": "false", "lowercase": "true", "keep_original": "false", "keep_none_chinese_together": "true", "remove_duplicated_term": "false", "keep_first_letter": "true", "keep_separate_first_letter": "false", "trim_whitespace": "true", "keep_none_chinese": "true", "limit_first_letter_length": "16", "keep_full_pinyin": "true" 创建索引与分词器 curl -X POST -d '{ "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin" } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "remove_duplicated_term" : false, "keep_joined_full_pinyin" : false, "keep_separate_first_letter" : false, "keep_first_letter" : true, "limit_first_letter_length" : 16, "keep_full_pinyin" : true, "keep_original" : true, "keep_none_chinese" : true, "keep_none_chinese_together" : true, "lowercase" : true, "trim_whitespace" : true } } } } } }' "http://localhost:9200/medcl20" 生成索引结构 curl -X GET "http://localhost:9200/medcl20" { "medcl20": { "aliases": {}, "mappings": { "folk": { "properties": { "text": { "type": "string", "analyzer": "pinyin_analyzer" } } } }, "settings": { "index": { "creation_date": "1490170676090", "analysis": { "analyzer": { "pinyin_analyzer": { "tokenizer": "my_pinyin" } }, "tokenizer": { "my_pinyin": { "keep_joined_full_pinyin": "false", "lowercase": "true", "keep_original": "true", "keep_none_chinese_together": "true", "remove_duplicated_term": "false", "keep_first_letter": "true", "keep_separate_first_letter": "false", "trim_whitespace": "true", "type": "pinyin", "keep_none_chinese": "true", "limit_first_letter_length": "16", "keep_full_pinyin": "true" } } }, "number_of_shards": "5", "number_of_replicas": "1", "uuid": "31Y9PizQQ2KQn_Fl6bpPNw", "version": { "created": "2030299" } } }, "warmers": {} } } 分词器分词效果 curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl20/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "刘德华", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 }, { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 4 } ] } keep_original keep_original = true curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl20/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "刘德华", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 }, { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 4 } ] } keep_original = false curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl20/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 } ] } keep_original 功能 keep_original=true 将保留原字符串,比如存入索引的数据为 刘德华 那么 刘德华 将也会被保存到索引中。keep_original=false 则不保存原字符串到索引 trim_whitespace trim_whitespace=true curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : [" 最爱 刘德华 的帅气帅气的 "] }' "http://localhost:9200/medcl20/_analyze" { "tokens": [ { "token": "zui", "start_offset": 3, "end_offset": 4, "type": "word", "position": 0 }, { "token": "ai", "start_offset": 4, "end_offset": 5, "type": "word", "position": 1 }, { "token": "liu", "start_offset": 8, "end_offset": 9, "type": "word", "position": 2 }, { "token": "de", "start_offset": 9, "end_offset": 10, "type": "word", "position": 3 }, { "token": "hua", "start_offset": 10, "end_offset": 11, "type": "word", "position": 4 }, { "token": "de", "start_offset": 14, "end_offset": 15, "type": "word", "position": 5 }, { "token": "shuai", "start_offset": 15, "end_offset": 16, "type": "word", "position": 6 }, { "token": "qi", "start_offset": 16, "end_offset": 17, "type": "word", "position": 7 }, { "token": "shuai", "start_offset": 17, "end_offset": 18, "type": "word", "position": 8 }, { "token": "qi", "start_offset": 18, "end_offset": 19, "type": "word", "position": 9 }, { "token": "de", "start_offset": 19, "end_offset": 20, "type": "word", "position": 10 }, { "token": "最爱 刘德华 的帅气帅气的", "start_offset": 0, "end_offset": 23, "type": "word", "position": 11 }, { "token": "zaldhdsqsqd", "start_offset": 0, "end_offset": 11, "type": "word", "position": 12 } ] } trim_whitespace=false curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : [" 最爱 刘德华 的帅气帅气的 "] }' "http://localhost:9200/medcl20/_analyze" { "tokens": [ { "token": "zui", "start_offset": 3, "end_offset": 4, "type": "word", "position": 0 }, { "token": "ai", "start_offset": 4, "end_offset": 5, "type": "word", "position": 1 }, { "token": "liu", "start_offset": 8, "end_offset": 9, "type": "word", "position": 2 }, { "token": "de", "start_offset": 9, "end_offset": 10, "type": "word", "position": 3 }, { "token": "hua", "start_offset": 10, "end_offset": 11, "type": "word", "position": 4 }, { "token": "de", "start_offset": 14, "end_offset": 15, "type": "word", "position": 5 }, { "token": "shuai", "start_offset": 15, "end_offset": 16, "type": "word", "position": 6 }, { "token": "qi", "start_offset": 16, "end_offset": 17, "type": "word", "position": 7 }, { "token": "shuai", "start_offset": 17, "end_offset": 18, "type": "word", "position": 8 }, { "token": "qi", "start_offset": 18, "end_offset": 19, "type": "word", "position": 9 }, { "token": "de", "start_offset": 19, "end_offset": 20, "type": "word", "position": 10 }, { "token": " 最爱 刘德华 的帅气帅气的 ", "start_offset": 0, "end_offset": 23, "type": "word", "position": 11 }, { "token": "zaldhdsqsqd", "start_offset": 0, "end_offset": 11, "type": "word", "position": 12 } ] } trim_whitespace 功能 去除字符串首尾空格字符,不去除字符串中间的空格。这个参数只有当 keep_original=true 时才能够看到效果。 例如当字符串为:最爱 刘德华 的帅气帅气的,trim_whitespace=true 则原字符串将被保存为 最爱 刘德华 的帅气帅气的,如果 trim_whitespace=false 则原字符串将被保存为 最爱 刘德华 的帅气帅气的。如果 keep_original=false,那么原字符串没有被保存,也将看不到效果。 keep_joined_full_pinyin keep_joined_full_pinyin = false curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl21/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "刘德华", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 }, { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 4 } ] } keep_joined_full_pinyin = true curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华"] }' "http://localhost:9200/medcl22/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "刘德华", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 }, { "token": "liudehua", "start_offset": 0, "end_offset": 8, "type": "word", "position": 4 }, { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 5 } ] } keep_joined_full_pinyin 功能 keep_joined_full_pinyin=true 将保存字符串拼音全拼,false 则不保存。例如,当 kepp_joined_full_pinyin=true 时,文本 刘德华 的拼音全拼 liudehua 将会被保留;当 keep_joined_full_pinyin=false 则 全拼liudehua remove_duplicated_term remove_duplicated_term = false curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华刘德华帅帅帅,帅帅帅"] }' "http://localhost:9200/medcl20/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "liu", "start_offset": 3, "end_offset": 4, "type": "word", "position": 3 }, { "token": "de", "start_offset": 4, "end_offset": 5, "type": "word", "position": 4 }, { "token": "hua", "start_offset": 5, "end_offset": 6, "type": "word", "position": 5 }, { "token": "shuai", "start_offset": 6, "end_offset": 7, "type": "word", "position": 6 }, { "token": "shuai", "start_offset": 7, "end_offset": 8, "type": "word", "position": 7 }, { "token": "shuai", "start_offset": 8, "end_offset": 9, "type": "word", "position": 8 }, { "token": "shuai", "start_offset": 10, "end_offset": 11, "type": "word", "position": 9 }, { "token": "shuai", "start_offset": 11, "end_offset": 12, "type": "word", "position": 10 }, { "token": "shuai", "start_offset": 12, "end_offset": 13, "type": "word", "position": 11 }, { "token": "刘德华刘德华帅帅帅,帅帅帅", "start_offset": 0, "end_offset": 13, "type": "word", "position": 12 }, { "token": "ldhldhssssss", "start_offset": 0, "end_offset": 12, "type": "word", "position": 13 } ] } remove_duplicated_term = true curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华刘德华帅帅帅,帅帅帅"] }' "http://localhost:9200/medcl26/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "shuai", "start_offset": 6, "end_offset": 7, "type": "word", "position": 3 }, { "token": "刘德华刘德华帅帅帅,帅帅帅", "start_offset": 0, "end_offset": 13, "type": "word", "position": 4 }, { "token": "ldhldhssssss", "start_offset": 0, "end_offset": 12, "type": "word", "position": 5 } ] } remove_duplicated_term 功能 remove_duplicated_term=true 则会将文本中相同的拼音只保存一份,比如 刘德华刘德华 只会保留一份拼音 liu,de,hua;相对的 remove_duplicated_term=false 则会保留两份 liu,de,hua。注意:remove_duplicated_term 并不会影响文本首字母的文本,刘德华刘德华 生成的首字母拼音始终都为 ldhldh remove_duplicated_term = true 并且 keep_joined_full_pinyin = true curl -X POST -d '{ "analyzer" : "pinyin_analyzer", "text" : ["刘德华刘德华帅帅帅,帅帅帅"] }' "http://localhost:9200/medcl27/_analyze" { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "shuai", "start_offset": 6, "end_offset": 7, "type": "word", "position": 3 }, { "token": "刘德华刘德华帅帅帅,帅帅帅", "start_offset": 0, "end_offset": 13, "type": "word", "position": 4 }, { "token": "liudehualiudehuashuaishuaishuaishuaishuaishuai", "start_offset": 0, "end_offset": 46, "type": "word", "position": 5 }, { "token": "ldhldhssssss", "start_offset": 0, "end_offset": 12, "type": "word", "position": 6 } ] } remove_duplicated_term 功能 remove_duplicated_term = true 会过滤相同的拼音,但是不影响全拼,刘德华刘德华 生成的字符串全拼为 liudehualiudehua keep_none_chinese keep_none_chinese = true POST /medcl20/_analyze HTTP/1.1 Host: localhost:9200 { "analyzer" : "pinyin_analyzer", "text" : ["刘*20*德b华DJ"] } { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "20", "start_offset": 3, "end_offset": 5, "type": "word", "position": 1 }, { "token": "de", "start_offset": 5, "end_offset": 6, "type": "word", "position": 2 }, { "token": "b", "start_offset": 6, "end_offset": 7, "type": "word", "position": 3 }, { "token": "hua", "start_offset": 7, "end_offset": 8, "type": "word", "position": 4 }, { "token": "d", "start_offset": 7, "end_offset": 9, "type": "word", "position": 5 }, { "token": "j", "start_offset": 7, "end_offset": 9, "type": "word", "position": 6 }, { "token": "刘*20*德b华dj", "start_offset": 0, "end_offset": 10, "type": "word", "position": 7 }, { "token": "l20dbhdj", "start_offset": 0, "end_offset": 8, "type": "word", "position": 8 } ] } keep_none_chinese = false POST /medcl28/_analyze HTTP/1.1 Host: localhost:9200 { "analyzer" : "pinyin_analyzer", "text" : ["刘*20*德b华DJ"] } { "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "de", "start_offset": 5, "end_offset": 6, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 7, "end_offset": 8, "type": "word", "position": 2 }, { "token": "刘*20*德b华dj", "start_offset": 0, "end_offset": 10, "type": "word", "position": 3 }, { "token": "l20dbhdj", "start_offset": 0, "end_offset": 8, "type": "word", "position": 4 } ] } keep_none_chinese 功能 keep_none_chinese = true 则非中文字母以及数字将会被保留,但是要确定所有的特别字符都是无法被保留下来的。例如,文本 刘*20*德b华dj 中的数字 20,字母 b 与 dj 将会被保留,而特殊字符 * 是不会保留的;当 keep_none_chinese=false 则非中文字母以及数字将不会被保留,上述文本中的数字 20,字母 b 与 dj 将不会被保留。注意:参数 keep_none_chinese 是不会影响首字母以及所有字符组成全拼的拼音,上述文本生成的首字母拼音为 l20dbhdj,所有字符组成的全拼为:liu20debhuadj,特别字符始终是被过滤去除的。 欢迎转载,请注明本文链接,谢谢你。 2017.4.12 20:44

优秀的个人博客,低调大师

Apache Beam研究报告

概述 本文不是一篇Beam的入门文档,不会介绍Beam的基本概念;而会主要探讨Beam的表达力,Beam的性能,以及Beam目前在业内的使用情况。面向的读者是那些想使用Beam作为自己公司操作大数据的统一API,但是还有所顾虑的人们。 表达力 离线 Beam里面有两个核心原语: ParDo: 来处理通用的基于单条数据的计算: 每条需要处理的数据会被喂给用户提供的指定的一个函数(Beam里面的@ProcessElement), 然后输出0个或者多个输出。 我们平常熟悉的Filter, AppendColumn等等都可以通过ParDo来实现。 ParDo的语义涵盖了Hadoop中Map-Shuffle-Reduce的Map,Reduce GroupByKey: 用来做Grouping的操作。 我们平常熟悉的Count, Sum, Min等等都可以通过G

优秀的个人博客,低调大师

Java技术专题-JVM研究系列(12)字符串常量池的研究分析

字符串冗余问题 从平均情况来看,应用程序中的String对象会消耗大量的内存。这里面有一部分是冗余的——同样的字符串会存在多个不同的实例(a != b, 但a.equals(b))。在实践中,有许多字符串会出于不同的原因造成冗余。 最初JDK提供了一个String.intern() 方法来解决字符串冗余的问题。这个方法的缺点在于你必须得去找出哪些字符串需要进行驻留(interned)。通常都需要具备冗余字符串查找功能的堆分析的工具才行,比如Youkit profiler。如果使用得当的话,字符串驻留会是一个非常有效的节省内存的工具——它让你可以重用整个字符串对象(每个字符串对象在底层char[]的基础上会增加24字节的额外开销)。 Java 7 update 6开始,每个String对象都有一个自己专属的私有char[] 。这样JVM才可以自动进行优化——既然底层的char[]没有暴露给外部的客户端的话,那么JVM就能去判断两个字符串的内容是否是一致的,进而将一个字符串底层的char[]替换成另一个字符串的底层char[]数组。 字符串去重这个特性就是用来做这个的,它在Java 8 update 20中被引入。下面是它的工作原理: 1.你得使用G1垃圾回收器并启用这一特性: -XX:+UseG1GC -XX:+UseStringDeduplication这一特性作为G1垃圾回收器的一个可选的步骤来实现的,如果你用的是别的回收器是无法使用这一特性的。 2.这个特性会在G1回收器的minor GC阶段中执行。根据我的观察来看,它是否会执行取决于有多少空闲的CPU周期。因此,你不要指望它会在一个处理本地数据的数据分析器中会被执行,也就是说,WEB服务器中倒是很可能会执行这个优化。 3.字符串去重会去查找那些未被处理的字符串,计算它们的hash值(如果它没在应用的代码中被计算过的话),然后再看是否有别的字符串的hash值和底层的char[]都是一样的。如果找到的话——它会用一个新字符串的char[]来替换掉现有的这个char[]。 4.字符串去重只会去处理那些历经数次GC仍然存活的那些字符串。这样能确保大多数的那些短生命周期的字符串不会被处理。字符串的这个最小的存活年龄可以通过 -XX:StringDeduplicationAgeThreshold=3 的JVM参数来指定(3是这个参数的默认值)。 下面是这个实现的一些重要的结论: 没错,如果你想享受字符串去重特性的这份免费午餐的话,你得使用G1回收器。 使用parellel GC的话是无法使用它的,而对那些对吞吐量要求比延迟时期高的应用而言,parellel GC应该是个更好的选择。 字符串去重是无法在一个已加载完的系统中运行的。要想知道它是否被执行了,可以通过 -XX:+PrintStringDeduplicationStatistics参数来运行JVM,并查看控制台的输出。 如果你希望节省内存的话,你可以在应用程序中将字符串进行驻留(interned)——那么放手去做吧,不要依赖于字符串去重的功能。 你需要时刻注意的是字符串去重是要处理你所有的字符串的(至少是大部分吧)——也就是说尽管你知道某个指定的字符串的内容是唯一的(比如说GUID),但JVM并不知道这些,它还是会尝试将这个字符串和其它的字符串进行匹配。这样的结果就是,字符串去重所产生的CPU开销既取决于堆中字符串的数量(将新的字符串和别的字符串进行比较),也取决于你在字符串去重的间隔中所创建的字符串的数量(这些字符串会和堆中的字符串进行比较)。 在一个拥有好几个G的堆的JVM上,可以通过- XX:+PrintStringDeduplicationStatistics选项来看下这个特性所产生的影响究竟有多大。 另一方面,它基本是以一种非阻塞的方式来完成的,如果你的服务器有足够多的空闲CPU的话,那为什么不用呢? 最后,请记住,String.intern可以让你只针对你的应用程序中指定的某一部分已知会产生冗余的字符串。通常来说,它只需要比较一个较小的驻留字符串的池就可以了,也就是说你可以更高效地使用你的CPU。不仅如此,你还可以将整个字符串对象进行驻留,这样每个字符串你还多节省了24个字节。 这里是我用来试验这一特性的一个测试类。这三个测试都会一直运行到JVM抛出OOM为止,因此你得分别去单独地运行它们。 第一个测试会创建内容一样的字符串,如果你想知道当堆中字符串很多的时候,字符串去重会花掉多少时间的话,这个测试就变得非常有用了。尽量给第一个测试分配尽可能多的内存——它创建的字符串越多,优化的效果就越好。 第二三个测试会比较去重(第二个测试)及驻留(interning, 第三个测试)间的差别。你得用一个相同的Xmx设置来运行它们。在程序中我把这个常量设置成了Xmx256M,但是当然了,你可以分配得多点。然而,你会发现,和interning测试相比,去重测试会更早地挂掉。这是为什么?因为我们在这组测试中只有100个不同的字符串,因此对它们进行驻留就意味着你用到的内存就只是存储这些字符串所需要的空间。而字符串去重的话,会产生不同的字符串对象,它仅会共享底层的char[]数组。 /** * String deduplication vs interning test */ public class StringDedupTest { private static final int MAX_EXPECTED_ITERS = 300; private static final int FULL_ITER_SIZE = 100 * 1000; //30M entries = 120M RAM (for 300 iters) private static List<String> LIST = new ArrayList<>( MAX_EXPECTED_ITERS * FULL_ITER_SIZE ); public static void main(String[] args) throws InterruptedException { //24+24 bytes per String (24 String shallow, 24 char[]) //136M left for Strings //Unique, dedup //136M / 2.9M strings = 48 bytes (exactly String size) //Non unique, dedup //4.9M Strings, 100 char[] //136M / 4.9M strings = 27.75 bytes (close to 24 bytes per String + small overhead //Non unique, intern //We use 120M (+small overhead for 100 strings) until very late, but can't extend ArrayList 3 times - we don't have 360M /* Run it with: -XX:+UseG1GC -XX:+UseStringDeduplication -XX:+PrintStringDeduplicationStatistics Give as much Xmx as you can on your box. This test will show you how long does it take to run a single deduplication and if it is run at all. To test when deduplication is run, try changing a parameter of Thread.sleep or comment it out. You may want to print garbage collection information using -XX:+PrintGCDetails -XX:+PrintGCTimestamps */ //Xmx256M - 29 iterations fillUnique(); /* This couple of tests compare string deduplication (first test) with string interning. Both tests should be run with the identical Xmx setting. I have tuned the constants in the program for Xmx256M, but any higher value is also good enough. The point of this tests is to show that string deduplication still leaves you with distinct String objects, each of those requiring 24 bytes. Interning, on the other hand, return you existing String objects, so the only memory you spend is for the LIST object. */ //Xmx256M - 49 iterations (100 unique strings) //fillNonUnique( false ); //Xmx256M - 299 iterations (100 unique strings) //fillNonUnique( true ); } private static void fillUnique() throws InterruptedException { int iters = 0; final UniqueStringGenerator gen = new UniqueStringGenerator(); while ( true ) { for ( int i = 0; i < FULL_ITER_SIZE; ++i ) LIST.add( gen.nextUnique() ); Thread.sleep( 300 ); System.out.println( "Iteration " + (iters++) + " finished" ); } } private static void fillNonUnique( final boolean intern ) throws InterruptedException { int iters = 0; final UniqueStringGenerator gen = new UniqueStringGenerator(); while ( true ) { for ( int i = 0; i < FULL_ITER_SIZE; ++i ) LIST.add( intern ? gen.nextNonUnique().intern() : gen.nextNonUnique() ); Thread.sleep( 300 ); System.out.println( "Iteration " + (iters++) + " finished" ); } } private static class UniqueStringGenerator { private char upper = 0; private char lower = 0; public String nextUnique() { final String res = String.valueOf( upper ) + lower; if ( lower < Character.MAX_VALUE ) lower++; else { upper++; lower = 0; } return res; } public String nextNonUnique() { final String res = "a" + lower; if ( lower < 100 ) lower++; else lower = 0; return res; } } } Java 8 update 20中添加了字符串去重的特性。它是G1垃圾回收器的一部分,因此你必须使用G1回收器才能启用它:-XX:+UseG1GC -XX:+UseStringDeduplication 字符串去重是G1的一个可选的阶段。它取决于当前的系统负载。 字符串去重会查询内容相同那些字符串,并将它们底层存储字符的char[]数组进行统一。使用这一特性你不需要写任何代码,不过这意味着最后你得到的是不同的字符串对象,每个对象会占用24个字节。有的时候显式地调用String.intern进行驻留还是有必要的。 字符串去重不会对年轻的字符串进行处理。字符串处理的最小年龄是通过-XX:StringDeduplicationAgeThreshold=3的JVM参数来进行管理的(3是这个参数的默认值)

优秀的个人博客,低调大师

Docker与自动化部署

Docker安装与基本使用 虚拟机系统centOS7 mini Docker的应用场景 1.简化配置,同一Docker的配置可以在不同 环境中使用,降低了硬件要求和应用环境之间的耦合度.2.代码的流水线管理.代码从开发者的假期到最终在生产环境上的部署,需要经过很多的中间环境.而每一个中间环境都有自己微小的蛤贝,Docker给应用提供一个从开发到上线均一致的环境,让代码的流水线变得简单.3.提高开发效率4.隔离应用,使应用松耦合5.快速部署docker [CMD] [options]基本命令与释义attach进入到正在运行的容器build由Dockerfile构建镜像commit由容器的改变创建一个新的镜像cp在容器中复制文件或文件夹到本地文件或文件夹中.logs获取容器日志network管理Docker网络node管理Docker集群节点pause暂停一个或多个容器内的所有进程port列表端口映射或用于容器的特定的映射ps列出容器pull从镜像仓库中拉出镜像push上传镜像rename重命名镜像restart重启一个容器rm 删除容器rmi删除镜像run在容器中运行命令search在Docker Hub中查找镜像service管理Docker服务start启动停止的容器stats显示容器的实时流资源使用统计信息stop停止正在运行的容器swarm管理Docker集群tag将镜像标记到存储库中top显示容器的正在运行的进程volume管理Docker卷 docker run [options]常用参数与释义(主要介绍docker run) -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;-d: 后台运行容器,并返回容器ID;-i: 以交互模式运行容器,通常与 -t 同时使用;-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;–name=”nginx-lb”: 为容器指定一个名称;–dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;–dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;-h “mars”: 指定容器的hostname;-e username=”ritchie”: 设置环境变量;–env-file=[]: 从指定文件读入环境变量;–cpuset=”0-2” or –cpuset=”0,1,2”: 绑定容器到指定CPU运行;-m :设置容器使用内存最大值;–net=”bridge”: 指定容器的网络连接类型,支持 bridge/host/none/Container: 四种类型;–link=[]: 添加链接到另一个容器;–expose=[]: 开放一个端口或一组端口; 1.安装Docker2.加入开机启动3.启动Docker4.下载镜像5.查看本地镜像6.启动容器7.查看容器 搭建本地镜像仓库 仓库IP 192.168.247.142 1.拉取registry镜像 2.启动镜像仓库容器并且映射虚拟机端口5000和容器端口5000 3.将本地目录/opt/data/registry挂载到容器内目录/tmp/registry中 4.用busybox做试验 5.在需要拉取镜像的虚拟机上创建/etc/docker/daemon.json(这一步很关键,解决的http和https服务冲突问题) 6.向私有仓库中上传镜像 docker的网络配置 1.查看docker的所有网络 2.指定查看bridge网络的配置 3.启动两个busybox做实验,分别是container1和container2 4.再次查看bridge,这时可以看到container1和container2已经被加入bridge中了 5.也可以进入到container1中,用ifconfig查看网络配置6.ping7.8.自建桥接模式的网络isolated_nw9.查看网络能看到已经建立的isolated_nw10.启动container3时指定网络isolated_nw,则可以加入自建网络中 Tomcat与Mysql 这部分我把网上的教程基本都做了一遍,但是由于我对javaweb项目的结构了解不透彻,web应用部署也不熟悉,所以没能达到预期的要求。以下我分别列出Tocmcat和Mysql容器的启动和使用。还有部分参数解释。 docker run -it –rm -p 8888:8080 tomcat:8.0 虚拟机端口:容器端口 tomcat镜像指定 docker run –name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag指定容器名,mysql密码,镜像 将mysql与web应用容器相连的命令是:docker run –name some-app –link some-mysql:mysql -d application-that-uses-mysql启动web应用容器,用–link连接mysql容器 官方文档指出,–link几乎已经废弃 单容器应用部署实践 说明:部署一个单页面的应用参考网址:https://github.com/docker/labs/blob/master/beginner/chapters/votingapp.md 1.启动容器会自动下载dockersanples/static-site镜像 2.启动容器 3.查看端口 4.查看容器运行情况 5.在浏览器中输入地址和端口号,就可以看到网页 6.创建文件夹flask-app 7.flask-app下有四个文件 8.构建镜像 9.启动容器,将虚拟机8888端口映射到容器的5000端口 10.在浏览器中输入网址,查看运行效果 多容器应用部署实践 说明:用docker compose和docker deploy stack 部署参考网址:https://github.com/docker/labs/blob/master/beginner/chapters/votingapp.md应用说明:该应用基于以下镜像搭建PythonNode.jsredis.netpostgress 1.下载工程 2.创建Docker Swarm 3.docker stack deploy是在Docker Swarm模式下docker-compose的替代 4.查看启动的服务 5.查看容器的运行情况 6.浏览器中输入虚拟机地址,这是5000端口,是投票服务 7.浏览器中输入虚拟机地址,这是5001端口,投票统计服务 8.镜像更新,只需要修改虚拟机中的配置文件9. 10.重启app 11.在浏览器中查看运行情况 12.13.14. Kubernetes安装和配置 1.创建多台centOS7 mini 系统的虚拟机备用2.修改主机名3.设置hosts4.关闭防火墙5.安装etcd6.修改etc配置文件/etc/etcd/etcd.conf7.安装Docker8.修改配置/etc/sysconfig/docker9.将Docker设为开机启动项,启动Docker10.安装Kubernetes11.修改/etc/kubernetes/apiserver12.修改/etc/kubernetes/config13.启动服务并设置开机项自启动14.修改/etc/kubernetes/config15.修改/etc/kubernetes/kubelet16.启动服务并设置开机自启动17.在master上查看节点和各节点状态18.安装flannel19.修改配置/etc/sysconfig/flanneld 用kubeadm安装kubernetes并且部署微服务 因为GFW问题导致遇到很多下载失败问题 1.安装Docker 2.安装k8s工具包 3.kubeadm 初始化 Kuberbernetes基础概念 Pod Pod是Kubernetes的最小操作单元,一个Pod可以由一个或多个容器组成;同一个Pod只能运行在同一主机上,共享相同的volumes,network,namespace ReplicationController(RC) RC用来管理Pod,一个RC可以由一个或多个Pod组成,在RC被创建后,系统会根据定义好的副本数来创建Pod数量.在运行过程中,如果Pod数量小于定义的,就会重启停止的或重新分配Pod,反之则kill多余的.当然,也可以动态伸缩运行的Pods规模或熟悉. Service Service定义了一个Pod逻辑集合的抽象资源,Pod集合中的容器提供相同的共而过.集合根据定义的Label和Selector完成,当创建一个Service后,会分配一个ClusterIP,这个IP与定义的端口提供这个集合一个统一的访问接口,并且实现负载均衡. Label Label是用于区分Pod,Service,RC的key/value键值对;Pod,Service,RC可以有多个label,但是每个label的key只能对应一个;主要是将Service的请求通过label转发给后端提供服务的Pod集合. kubectl 客户端命令行工具,将接受的命令格式化后发送给kube-apiserver,作为整个系统的操作入口. kube-apiserver 作为整个系统的控制入口,以REST API服务提供接口. kube-controller-manager 用来执行整个系统中的后台任务,包括节点状态状况,Pod个数,Pods和Service的关联等. kube-scheduler 负责节点资源管理,接受来自kube-apiserver创建Pods任务,并分配到某个节点. etcd 负责节点间的服务发现和配置共享. kube-proxy 运行在每个计算节点上,负责Pod网络代理.定时从etcd获取到service信息来做相应的策略. kubelet 运行在每个计算节点上,作为agent,节后分配该节点的Pods任务及管理容器,周期性获取容器状态,反馈给kube-apiserver. DNS 一个可选的DNS服务,用于为每个Service对象创建DNS记录,这样所有的Pod就都可以通过DNS访问服务了.

优秀的个人博客,低调大师

Spring自动化装配bean

阅读目录 1. 场景 2. 架构图示 3. 代码 回到顶部 1. 场景 用CD(Compact disc)和CD播放器(CDPlayer)阐述DI(依赖注入)。 如果不将CD插入(注入)到CDPlayer中,那么CDPlayer其实没有太大的用处,所以,可以这样说:CDPlayer依赖于CD才能完成它的使命。 回到顶部 2. 架构图示 回到顶部 3. 代码 接口: CompactDisc.java package soundsystem; public interface CompactDisc { void play(); } 接口: MediaPlayer.java package soundsystem; public interface MediaPlayer { void play(); } SgtPeppers.java package soundsystem; import org.springframework.stereotype.Component; @Component public class SgtPeppers implements CompactDisc { private String title = "Sgt. Pepper's Lonely Hearts Club Band"; private String artist = "The Beatles"; @Override public void play() { System.out.println("Playing " + title + " by " + artist); } } 注:SgtPeppers类上使用了@Component注解。这个简单的注解表明该类会作为组件类,并告知Spring要为这个类创建bean CDPlayer.java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CDPlayer implements MediaPlayer { private CompactDisc cd; @Autowired public CDPlayer(CompactDisc cd) { this.cd = cd; } @Override public void play() { cd.play(); } } 不过,组件扫描默认是不启用的,我们还需显式配置一下Spring,从而命令它去寻找带有@Component注解的类,并未其创建bean。下例中使用了@ComponentScan注解,这个注解能够在Spring中启用组件扫描。如没有其他配置,@ComponentScan默认会扫描与配置类相同的包:soundsystem CDPlayerConfig.java package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan public class CDPlayerConfig { } 测试CDPlayerTest.java package soundsystem; import static org.junit.Assert.*; import org.junit.Test; import org.junit.contrib.java.lang.system.StandardOutputStreamLog; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=CDPlayerConfig.class) public class CDPlayerTest { @Autowired private MediaPlayer player; @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); } @Test public void play() { player.play(); } } 自动装配就是让Spring自动满足bean依赖的一种方法,在满足依赖的过程中,会在Spring应用的上下文中寻找匹配某个bean需求的其他bean。为了声明要进行自动装配,我们借助Spring的@Autowired注解。 上述代码中,在构造器中添加了@Autowised注解,这表明当Spring创建CDPlayer bean的时候,会通过这个构造器来进行实例化,并传入一个可设置为CompactDisc类的bean,在上面的代码中,SgtPeppers被声明为组件,并实现了CompactDisc接口。因此,在实际运行中会把SgtPeppers作为实例化类。 本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/6942296.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册