parent
7f84c9964f
commit
771c524ae6
@ -0,0 +1,27 @@
|
|||||||
|
package com.xjs.mall.search;
|
||||||
|
|
||||||
|
import com.ruoyi.common.security.annotation.EnableCustomConfig;
|
||||||
|
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
|
||||||
|
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商城Search启动器
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-03-25
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableCustomConfig
|
||||||
|
@EnableCustomSwagger2
|
||||||
|
@EnableRyFeignClients
|
||||||
|
public class MallSearchApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ConfigurableApplicationContext run = SpringApplication.run(MallSearchApp.class, args);
|
||||||
|
//String[] beanDefinitionNames = run.getBeanDefinitionNames();
|
||||||
|
//for (String beanDefinitionName : beanDefinitionNames) {
|
||||||
|
// System.out.println("beanDefinitionName:"+beanDefinitionName);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.xjs.mall.search.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.client.RestClient;
|
||||||
|
import org.elasticsearch.client.RestClientBuilder;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Elasticsearcn配置类
|
||||||
|
*
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-03-25
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "elasticsearch")
|
||||||
|
@Data
|
||||||
|
public class ElasticsearchConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es的ip
|
||||||
|
*/
|
||||||
|
private String ip;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 端口
|
||||||
|
*/
|
||||||
|
private Integer port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 协议
|
||||||
|
*/
|
||||||
|
private String scheme;
|
||||||
|
|
||||||
|
|
||||||
|
public static final RequestOptions COMMON_OPTIONS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
|
||||||
|
|
||||||
|
//配置项.......
|
||||||
|
|
||||||
|
COMMON_OPTIONS = builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestHighLevelClient restHighLevelClient() {
|
||||||
|
RestClientBuilder builder = RestClient.builder(new HttpHost(ip, port, scheme));
|
||||||
|
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
|
||||||
|
return restHighLevelClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.xjs.mall.search.controller;
|
||||||
|
|
||||||
|
import com.xjs.mall.other.R;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static com.xjs.mall.search.config.ElasticsearchConfig.COMMON_OPTIONS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-03-26
|
||||||
|
*/
|
||||||
|
@RequestMapping("test")
|
||||||
|
@Api(tags = "test")
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@ApiOperation("测试")
|
||||||
|
public R test() throws IOException {
|
||||||
|
IndexRequest indexRequest = new IndexRequest("users");
|
||||||
|
indexRequest.id("1");
|
||||||
|
indexRequest.source("userName", "zs", "age", 18);
|
||||||
|
|
||||||
|
IndexResponse index = restHighLevelClient.index(indexRequest, COMMON_OPTIONS);
|
||||||
|
|
||||||
|
System.out.println(index.toString());
|
||||||
|
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.xjs.mall.search;
|
||||||
|
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static com.xjs.mall.search.config.ElasticsearchConfig.COMMON_OPTIONS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-03-25
|
||||||
|
*/
|
||||||
|
@SpringBootTest(classes = MallSearchApp.class)
|
||||||
|
class ElasticsearchConfigTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
/**
|
||||||
|
* 测试存储数据到es
|
||||||
|
*/
|
||||||
|
public void indexData() throws IOException {
|
||||||
|
IndexRequest indexRequest = new IndexRequest("users");
|
||||||
|
indexRequest.id("1");
|
||||||
|
indexRequest.source("userName", "zs", "age", 18);
|
||||||
|
|
||||||
|
IndexResponse index = restHighLevelClient.index(indexRequest, COMMON_OPTIONS);
|
||||||
|
|
||||||
|
System.out.println(index.toString());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9980
|
||||||
|
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: xjs-mall-ware
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
#配置组
|
||||||
|
group: xjs
|
||||||
|
#命名空间
|
||||||
|
namespace: xjs-666
|
Loading…
Reference in new issue