CommonUtil/README.md

331 lines
9.1 KiB
Markdown
Raw Normal View History

2019-07-26 16:59:56 +08:00
[TOC]
2019-07-26 15:12:03 +08:00
# CommonUtil
2019-07-26 16:59:56 +08:00
* <b>Desc: A common utils jar for using redis elasticsearch Rest even and file.</b>
2019-11-21 16:10:44 +08:00
## Download
2019-11-22 16:39:00 +08:00
[CommonUtil.jar](https://github.com/carolcoral/CommonUtil/releases/download/1.0.1/CommonUtil-1.0.1.jar)
2019-11-21 16:24:32 +08:00
2019-11-22 16:39:00 +08:00
[CommonUtil-javadoc.jar](https://github.com/carolcoral/CommonUtil/releases/download/1.0.1/CommonUtil-1.0.1-javadoc.jar)
2019-11-21 16:24:32 +08:00
2019-11-22 16:39:00 +08:00
[CommonUtil-sources.jar](https://github.com/carolcoral/CommonUtil/releases/download/1.0.1/CommonUtil-1.0.1-sources.jar)
2019-11-21 16:10:44 +08:00
2019-07-26 16:59:56 +08:00
## How to use
### Import it in your pom
2019-11-21 16:10:44 +08:00
#### Import jar into your maven
```shell
mvn install:install-file -Dfile=<path-to-file> -DgroupId=site.cnkj -DartifactId=CommonUtil -Dversion=1.0 -Dpackaging=jar
```
2019-11-21 16:24:32 +08:00
> Note:<path-to-file> is your local jar of full path.
2019-11-21 16:10:44 +08:00
> For example:
```shell
mvn install:install-file -Dfile=/User/carol/Desktop/CommonUtil-1.0.jar -DgroupId=site.cnkj -DartifactId=CommonUtil -Dversion=1.0 -Dpackaging=jar
```
#### Import dependency into your pom.xml
```yaml
<dependency>
<groupId>site.cnkj</groupId>
<artifactId>CommonUtil</artifactId>
<version>1.0</version>
</dependency>
2019-07-26 16:59:56 +08:00
```
## Use in project
2019-11-25 16:58:36 +08:00
### Redis
#### 1.First import it in your application.
2019-07-26 16:59:56 +08:00
```java
@SpringBootApplication
@ComponentScan(basePackages = "site.cnkj.*",
basePackageClasses = {
RedisConfig.class
})
public class Application {
2019-11-25 16:58:36 +08:00
2019-07-26 16:59:56 +08:00
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
2019-11-25 16:58:36 +08:00
2019-07-26 16:59:56 +08:00
@Resource(name = "redisUtil")
public RedisUtil redisUtil;
}
```
2019-11-25 16:58:36 +08:00
#### 2.Then autowired in your class, for example to use like this.
2019-07-26 16:59:56 +08:00
```java
@Component
public class TestClass {
@Autowired
RedisUtil redisUtil;
public void test(){
redisUtils.scanAll();
}
}
```
#### Redis single
> Add these config info in your application.properties
2019-11-21 16:10:44 +08:00
```yaml
2019-07-26 16:59:56 +08:00
//set prefix name for all key
2019-11-25 16:58:36 +08:00
spring.redis.name=
2019-07-26 16:59:56 +08:00
//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
2019-11-21 16:10:44 +08:00
```yaml
2019-07-26 16:59:56 +08:00
//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
2019-11-21 16:10:44 +08:00
```yaml
2019-07-26 16:59:56 +08:00
//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
```
2019-11-25 16:58:36 +08:00
#### Redis SubDescription/Publish(订阅/发布)
2019-07-26 16:59:56 +08:00
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
2019-11-21 16:10:44 +08:00
```yaml
2019-11-25 16:58:36 +08:00
spring.redis.subDescription.channel= //SubDescription or Publish of name
2019-07-26 16:59:56 +08:00
```
### ElasticSearch
2019-11-25 16:58:36 +08:00
#### Import it in your application.
```java
@SpringBootApplication
@ComponentScan(basePackages = "site.cnkj.*",
basePackageClasses = {
ElasticsearchConfig.class
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
#### 1.Add configuration in application.properties
2019-11-21 16:10:44 +08:00
```yaml
2019-07-26 16:59:56 +08:00
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
```
2019-11-25 16:58:36 +08:00
#### 2.Autowired client in your class
2019-07-26 16:59:56 +08:00
```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
2019-11-25 16:58:36 +08:00
#### 1.Add config in your application.properties
2019-11-21 16:10:44 +08:00
```yaml
2019-07-26 16:59:56 +08:00
spring.async.pool.corePoolSize = 10
spring.async.pool.maxPoolSize = 100
spring.async.pool.keepAliveSeconds = 60
spring.async.pool.queueCapacity = 25
```
2019-11-25 16:58:36 +08:00
#### 2.Import it in your application
2019-07-26 16:59:56 +08:00
```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();
}
}
}
```
2019-11-25 16:58:36 +08:00
#### 3.Use in your class
2019-07-26 16:59:56 +08:00
```java
@Service
public class AsyncExecutePool {
@Async(value = "myTaskAsyncPool")
public void execute() {
//TODO
}
}
```
2019-11-25 16:58:36 +08:00
### HttpCommonUtil
#### 1.Import it in your application
```java
@SpringBootApplication
@ComponentScan(basePackages = "site.cnkj.*",
basePackageClasses = {
RestTemplateConfig.class
})
public class TestApplication {
public static void main(String[] args) {
try {
SpringApplication.run(TestApplication.class, args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
#### 2.Use in your class
```java
@Service
public class RestTemplateService {
@Autowired
RestTemplateUtil restTemplateUtil;
public void test(){
restTemplateUtil.getWithQ("http://127.0.0.1", Object.class, new HashMap<>().put("test", "1"));
}
}
```
### MongodbUtil
#### 1.Add config in your application.properties
```yaml
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database= # Database name.
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.
spring.data.mongodb.grid-fs-database= # GridFS database name.
spring.data.mongodb.host= # Mongo server host. Cannot be set with URI.
spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with URI.
spring.data.mongodb.port= # Mongo server port. Cannot be set with URI.
spring.data.mongodb.repositories.type=auto # Type of Mongo repositories to enable.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with URI.
```
#### 1.Import it in your application
```java
@SpringBootApplication
@ComponentScan(basePackages = "site.cnkj.*",
basePackageClasses = {
MongodbConfig.class
})
public class TestApplication {
public static void main(String[] args) {
try {
SpringApplication.run(TestApplication.class, args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
#### 2.Use in your class
```java
@Service
public class MongodbService {
@Autowired
MongodbUtil mongodbUtil;
public void test(){
mongodbUtil.dropCollection("collectionName");
}
}
```