ReadMe
This commit is contained in:
parent
4b6f3a7c5a
commit
195700c6fe
9
.idea/sonarlint/issuestore/index.pb
Normal file
9
.idea/sonarlint/issuestore/index.pb
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
7
|
||||
pom.xml,4\4\442292b8a7efeabbe4cc176709b833b1792140ec
|
||||
e
|
||||
5src/main/java/site/cnkj/utils/config/RedisConfig.java,1\c\1c975fc549020abdaa84eed8958c340de82c46ba
|
||||
9
|
||||
README.md,8\e\8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d
|
||||
\
|
||||
,src/main/java/site/cnkj/utils/RedisUtil.java,7\5\752ec767523fc44d876b82408ca379fab24b78f4
|
@ -2,9 +2,8 @@
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="eb22885f-8347-4349-9d11-17e6e18e8973" name="Default" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
|
||||
</list>
|
||||
<ignored path="$PROJECT_DIR$/out/" />
|
||||
<ignored path="$PROJECT_DIR$/target/" />
|
||||
@ -17,11 +16,39 @@
|
||||
</component>
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
|
||||
<file leaf-file-name="pom.xml" pinned="false" current-in-tab="true">
|
||||
<file leaf-file-name="pom.xml" pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/pom.xml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="248">
|
||||
<caret line="183" column="34" lean-forward="true" selection-start-line="183" selection-start-column="34" selection-end-line="183" selection-end-column="34" />
|
||||
<state relative-caret-position="408">
|
||||
<caret line="191" column="103" selection-start-line="191" selection-start-column="103" selection-end-line="191" selection-end-column="103" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file leaf-file-name="README.md" pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/README.md">
|
||||
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
|
||||
<state split_layout="SPLIT">
|
||||
<first_editor relative-caret-position="500">
|
||||
<caret line="25" column="31" selection-end-line="59" />
|
||||
<folding>
|
||||
<element signature="e#321#322#0" expanded="true" />
|
||||
<element signature="e#383#384#0" expanded="true" />
|
||||
</folding>
|
||||
</first_editor>
|
||||
<second_editor />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file leaf-file-name="RedisUtil.java" pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/RedisUtil.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="-18554">
|
||||
<caret line="24" column="20" selection-start-line="24" selection-start-column="20" selection-end-line="24" selection-end-column="20" />
|
||||
<folding>
|
||||
<element signature="imports" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -59,6 +86,7 @@
|
||||
<option value="$PROJECT_DIR$/src/main/java/site/cnkj/utils/LoggerUtil.java" />
|
||||
<option value="$PROJECT_DIR$/src/main/java/site/cnkj/utils/RestTemplateUtil.java" />
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/README.md" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -135,6 +163,15 @@
|
||||
<item name="java" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="utils" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="CommonUtil" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="CommonUtil" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="main" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="java" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="utils" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="config" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
</expand>
|
||||
<select />
|
||||
</subPane>
|
||||
@ -228,15 +265,16 @@
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1564120653653</updated>
|
||||
<workItem from="1564120658771" duration="3389000" />
|
||||
<workItem from="1564124978277" duration="1265000" />
|
||||
<workItem from="1564124978277" duration="3804000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TimeTrackingManager">
|
||||
<option name="totallyTimeSpent" value="4654000" />
|
||||
<option name="totallyTimeSpent" value="7193000" />
|
||||
</component>
|
||||
<component name="ToolWindowManager">
|
||||
<frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
|
||||
<editor active="true" />
|
||||
<layout>
|
||||
<window_info anchor="right" id="Palette" order="3" />
|
||||
<window_info anchor="bottom" id="TODO" order="6" />
|
||||
@ -252,7 +290,7 @@
|
||||
<window_info anchor="bottom" id="Terminal" order="7" />
|
||||
<window_info id="Capture Tool" order="2" />
|
||||
<window_info id="Designer" order="2" />
|
||||
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25266525" />
|
||||
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.16257995" />
|
||||
<window_info anchor="right" id="Bean Validation" order="3" />
|
||||
<window_info anchor="bottom" id="SonarLint" order="7" />
|
||||
<window_info anchor="right" id="Database" order="3" />
|
||||
@ -293,13 +331,6 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/config/RedisConfig.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="420">
|
||||
<caret line="21" column="26" lean-forward="true" selection-start-line="21" selection-start-column="26" selection-end-line="21" selection-end-column="26" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/config/AsyncThreadPoolConfig.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="160">
|
||||
@ -352,6 +383,9 @@
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="540">
|
||||
<caret line="27" column="17" selection-start-line="27" selection-start-column="17" selection-end-line="27" selection-end-column="17" />
|
||||
<folding>
|
||||
<element signature="imports" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -384,17 +418,13 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/RedisUtil.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="-960">
|
||||
<caret line="24" column="20" lean-forward="true" selection-start-line="24" selection-start-column="20" selection-end-line="24" selection-end-column="20" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/RestTemplateUtil.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="-760">
|
||||
<caret line="18" column="38" lean-forward="true" selection-start-line="18" selection-start-column="38" selection-end-line="18" selection-end-column="38" />
|
||||
<folding>
|
||||
<element signature="imports" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -409,8 +439,42 @@
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/pom.xml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="248">
|
||||
<caret line="183" column="34" lean-forward="true" selection-start-line="183" selection-start-column="34" selection-end-line="183" selection-end-column="34" />
|
||||
<state relative-caret-position="408">
|
||||
<caret line="191" column="103" selection-start-line="191" selection-start-column="103" selection-end-line="191" selection-end-column="103" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/config/RedisConfig.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="-447">
|
||||
<caret line="26" column="37" selection-start-line="26" selection-start-column="14" selection-end-line="26" selection-end-column="37" />
|
||||
<folding>
|
||||
<element signature="imports" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/README.md">
|
||||
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
|
||||
<state split_layout="SPLIT">
|
||||
<first_editor relative-caret-position="500">
|
||||
<caret line="25" column="31" selection-end-line="59" />
|
||||
<folding>
|
||||
<element signature="e#321#322#0" expanded="true" />
|
||||
<element signature="e#383#384#0" expanded="true" />
|
||||
</folding>
|
||||
</first_editor>
|
||||
<second_editor />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/java/site/cnkj/utils/RedisUtil.java">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="-18554">
|
||||
<caret line="24" column="20" selection-start-line="24" selection-start-column="20" selection-end-line="24" selection-end-column="20" />
|
||||
<folding>
|
||||
<element signature="imports" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
213
README.md
213
README.md
@ -1 +1,214 @@
|
||||
[TOC]
|
||||
|
||||
# CommonUtil
|
||||
* <b>Desc: A common utils jar for using redis elasticsearch Rest even and file.</b>
|
||||
|
||||
## How to use
|
||||
### Import it in your pom
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
## Use in project
|
||||
1.First import it in your application
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "site.cnkj.*",
|
||||
basePackageClasses = {
|
||||
RestTemplateConfig.class,
|
||||
RedisConfig.class
|
||||
})
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
@Resource(name = "restTemplateUtil")
|
||||
public RestTemplateUtil restTemplateUtil;
|
||||
@Resource(name = "redisUtil")
|
||||
public RedisUtil redisUtil;
|
||||
}
|
||||
```
|
||||
|
||||
2.Then autowired in your class
|
||||
```java
|
||||
@Component
|
||||
public class TestClass {
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@Autowired
|
||||
private RestTemplateUtil restTemplateUtil;
|
||||
|
||||
public void test(){
|
||||
redisUtils.scanAll();
|
||||
restTemplateUtil.getWithQ("http://127.0.0.1", Object.class, new HashMap<>().put("test", "1"));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Redis
|
||||
#### Redis single
|
||||
> Add these config info in your application.properties
|
||||
```java
|
||||
//set prefix name for all key
|
||||
spring.redis.prefixName=
|
||||
//set database which you want to use
|
||||
spring.redis.database=0
|
||||
//set redis connection host
|
||||
spring.redis.host=127.0.0.1
|
||||
//set redis connection port
|
||||
spring.redis.port=6379
|
||||
//set redis connection failed callback time
|
||||
spring.redis.timeout=10s
|
||||
//set redis subdescripton channel when you wanto to use it
|
||||
spring.redis.subDescription.channel= ${spring.application.name}:flush
|
||||
```
|
||||
|
||||
#### Redis sentinel
|
||||
> Add these config info in your application.properties
|
||||
```java
|
||||
//set prefix name for all key
|
||||
spring.redis.prefixName=
|
||||
//set database which you want to use
|
||||
spring.redis.database=0
|
||||
//set redis connection failed callback time
|
||||
spring.redis.timeout=10s
|
||||
spring.redis.sentinel.nodes = 127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383
|
||||
spring.redis.sentinel.master = sentinel-127.0.0.1-6379
|
||||
```
|
||||
|
||||
#### Redis cluster
|
||||
> Add these config info in your application.properties
|
||||
```java
|
||||
//set prefix name for all key
|
||||
spring.redis.prefixName=
|
||||
//set database which you want to use
|
||||
spring.redis.database=0
|
||||
//set redis connection failed callback time
|
||||
spring.redis.timeout=10s
|
||||
spring.redis.cluster.nodes = 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383
|
||||
```
|
||||
|
||||
#### Redis subdescription/publish
|
||||
1.Implements Receiver
|
||||
```java
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SubDescription implements Receiver {
|
||||
/**
|
||||
* @param message redis published message
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Object receiver(String message) {
|
||||
//TODO what you want to do by use received message
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
2.Add this configuration in your application.properties after redis config
|
||||
```java
|
||||
spring.redis.subDescription.channel=
|
||||
```
|
||||
|
||||
### ElasticSearch
|
||||
1.Add configuration in application.properties
|
||||
```java
|
||||
common.elasticsearch.cluster.name = site_cnkj_test
|
||||
common.elasticsearch.clusterNodes=127.0.0.1:8000,127.0.0.1:8001
|
||||
common.elasticsearch.username = elastic
|
||||
common.elasticsearch.password = changeme
|
||||
common.elasticsearch.pool = 100
|
||||
common.elasticsearch.snifferinterval = 180000
|
||||
common.elasticsearch.socketTimeout = 180000
|
||||
common.elasticsearch.maxRetryTimeoutMillis = 180000
|
||||
common.elasticsearch.connectTimeout = 180000
|
||||
```
|
||||
|
||||
2.Autowried client in your class
|
||||
```java
|
||||
@Service
|
||||
public class ElasticSearchService{
|
||||
//@Resource(name = "HighLevelClient")
|
||||
//RestHighLevelClient client;
|
||||
|
||||
@Resource(name = "HighLevelSniffClient")
|
||||
RestHighLevelClient client;
|
||||
|
||||
public void search(String startTime, String endTime, String filterKey, String filterValue){
|
||||
//Timeout (seconds)
|
||||
int elasticsearchTimeout = 1000 * 60;
|
||||
//Single request quantity
|
||||
int elasticsearchSize = 10000;
|
||||
try {
|
||||
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(10L));
|
||||
SearchRequest searchRequest = new SearchRequest(index);
|
||||
searchRequest.types(type);
|
||||
searchRequest.scroll(scroll);
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
//Aggregate statement
|
||||
searchSourceBuilder.query(QueryBuilders.boolQuery()
|
||||
.must(QueryBuilders.matchPhraseQuery(filterKey, filterValue))
|
||||
.must(QueryBuilders.rangeQuery("@timestamp").gte(startTime).lte(endTime))
|
||||
)
|
||||
.timeout(new TimeValue(elasticsearchTimeout,TimeUnit.SECONDS))
|
||||
.size(elasticsearchSize);
|
||||
searchRequest.source(searchSourceBuilder);
|
||||
// Print the executed DSL statement, which can be used directly in Kibana
|
||||
// LOGGER.info("\n"+searchSourceBuilder.toString());
|
||||
SearchResponse searchResponse = client.search(searchRequest);
|
||||
for (SearchHit hit : searchResponse.getHits().getHits()) {
|
||||
// TODO
|
||||
String res = hit.getSourceAsString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Thread Pool
|
||||
1.Add config in your application.properties
|
||||
```java
|
||||
spring.async.pool.corePoolSize = 10
|
||||
spring.async.pool.maxPoolSize = 100
|
||||
spring.async.pool.keepAliveSeconds = 60
|
||||
spring.async.pool.queueCapacity = 25
|
||||
```
|
||||
|
||||
2.Import it in your application
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "site.cnkj.*",
|
||||
basePackageClasses = {
|
||||
AsyncThreadPoolConfig.class,
|
||||
AsyncExecutePool.class
|
||||
})
|
||||
public class TestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3.Use in your class
|
||||
```java
|
||||
@Service
|
||||
public class AsyncExecutePool {
|
||||
|
||||
@Async(value = "myTaskAsyncPool")
|
||||
public void execute() {
|
||||
//TODO
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user