本文已参与「新人创作礼」活动,一起开启掘金创作之路。
微服务调用实例:
两个服务,一个商品服务(Product) 一个库存服务(Ware)
场景:商品服务查询库存服务使用有库存。
1.库存服务(ware)做好支持查询业务
/**
* 查询是否有库存
* @param skuIds
* @return
*/
@PostMapping("/hasstock")
public R getSkusHasStock(@RequestBody List<Long> skuIds){
//sku_id stock
List<SkuHasStockVo> vos = wareSkuService.getSkusHasStock(skuIds);
return R.ok().put("data",vos);
}
2.商品服务pom导入openfeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.建立专属feign远程业务的包(文件夹)
位置:个人习惯 这里与controller目录平级
config
controller
dao
entity
exception
roductApplication
service
vo
增加一个 --> feign 包
4.写远程调用接口
1.建立类文件 WareFeignService
2.接口注解FeignClient客户端 调用远程服务的服务名
@FeignClient("ware")
public interface WareFeignService {
}
3.在feign类方法编写远程接口方法。 直接去被调用方法复制签名
@PostMapping("/hasstock")
R getSkusHasStock(@RequestBody List<Long> skuIds)
4.把这个方法,补齐被调用方法的访问路径
@PostMapping("/ware服务路径/远程getSkusHasStock所在控制器路径/hasstock")
R getSkusHasStock(@RequestBody List<Long> skuIds)
5.在product的需要远程调用库存的地方 引入这个远程调用接口 当作service试用
@Autowired
WareFeignService wareFeignService;
...
如:
skusHasStock = wareFeignService.getSkusHasStock(skuIdList);
至此,调用完成。
elasticSearch的属性的index doc_values和nested嵌入式使用
需要ik中文检索:
"skuTitle": {
"type": "text",
"analyzer": "ik_smart"
}
使用ik_smart 分词类型,需要安装ik分词器插件哦
节省资源的设置:
"skuImg": {
"type": "keyword",
"index": false,
"doc_values": false
}
index: 默认 true,如果为 false,表示该字段不会被索引,但是检索结果里面有,但字段本身不能 当做检索条件。
doc_values: 默认 true,设置为 false,表示不可以做排序、聚合以及脚本操作,这样更节省磁盘空间。 还可以通过设定 doc_values 为 true,index 为 false 来让字段不能被搜索但可以用于排序、聚 合以及脚本操作:
对数组属性nested嵌入式设置
"attrs":
{
"type": "nested",
"properties": {
"attrId": {
"type": "long"
},
"attrName": {
"type": "keyword",
"index": false,
"doc_values": false
},
"attrValue": {
"type": "keyword"
}
}
}
对于数组型属性不设置嵌入式结构,存储时会被扁平化处理:
想要保存两个人:
做一个索引,有个user数组属性,把它的存储人的名字
John Smith
Alice White
PUT my-index-000001/_doc/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
默认会被扁平化存储:
扁平化存储后,它的文档将在内部转换为更像以下内容的文档:
存储对象的同属性被合并到了一起
{
"group" : "fans",
"user.first" : [ "Alice", "john" ],
"user.last" : [ "smith", "white" ]
}
测试一下它的的特性: 我们找一个不存在的人:
我们找:Alice Smith
GET my-index-000001/_search
{
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "Smith" }}
]
}
}
}
我们却能找到结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.105360515,
"hits" : [
{
"_index" : "my-index-000001",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.105360515,
"_source" : {
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
}
]
}
}
查看他的mapping:
GET my-index-000001/_mapping
得到:
{
"my-index-000001" : {
"mappings" : {
"properties" : {
"group" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"user" : {
"properties" : {
"first" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"last" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
竟然有结果,不符合我们预期 接下来删除这个索引
DELETE my-index-000001
重新定义它,给它加上嵌入式的类型nested:
PUT my-index-000001
{
"mappings": {
"properties": {
"user": {
"type": "nested"
}
}
}
}
重新存储:
PUT my-index-000001/_doc/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
查找那个不存在的人:
GET my-index-000001/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "Smith" }}
]
}
}
}
}
}
这货不该出现,就不出现了:
{
"took" : 65,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}