Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client优化 #19

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.xiaoju.uemc.tinyid</groupId>
<artifactId>tinyid</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>tinyid</name>
Expand All @@ -17,7 +17,18 @@
<maven.compiler.source>1.7</maven.compiler.source>
<junit.version>4.12</junit.version>
</properties>

<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://repo.hualala.com/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://repo.hualala.com/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -36,6 +47,7 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
4 changes: 2 additions & 2 deletions tinyid-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.xiaoju.uemc.tinyid</groupId>
<artifactId>tinyid</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>tinyid-base</artifactId>

<version>0.2.0-SNAPSHOT</version>
<dependencies>
</dependencies>
</project>
41 changes: 0 additions & 41 deletions tinyid-client/dependency-reduced-pom.xml

This file was deleted.

89 changes: 74 additions & 15 deletions tinyid-client/pom.xml
Original file line number Diff line number Diff line change
@@ -1,43 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xiaoju.uemc.tinyid</groupId>
<artifactId>tinyid</artifactId>
<version>0.1.0-SNAPSHOT</version>
<groupId>com.xiaoju.uemc.tinyid</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>tinyid-client</artifactId>
<version>0.2.0-SNAPSHOT</version>

<name>tinyid-client</name>

<properties>
<spring.version>2.3.0.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>com.xiaoju.uemc.tinyid</groupId>
<artifactId>tinyid-base</artifactId>
<version>0.2.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.xiaoju.uemc.tinyid.client.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;

@Configuration
@EnableConfigurationProperties(TinyIdClientConfigProperties.class)
public class TinyIdAutoConfig {
@Autowired
private TinyIdClientConfigProperties properties;
private static final int MIN_TIME_OUT = 1 * 1000;
private static final String cacheServerUrl = "{0}/tinyid/id/nextSegmentIdSimple";
private static final String noCacheServerUrl = "{0}/tinyid/id/nextIdSimple";

@Bean
public TinyIdClientConfig config() {
String cacheServer = MessageFormat.format(cacheServerUrl, properties.getServer());
String noCacheServer = MessageFormat.format(noCacheServerUrl, properties.getServer());
return new TinyIdClientConfig(cacheServer, noCacheServer, properties.getToken(), properties.isLocalCache());
}

@Bean
public RestTemplate restTemplate() {

if (properties.getReadTimeout() < MIN_TIME_OUT) {
properties.setReadTimeout(MIN_TIME_OUT);
}

if (properties.getConnectTimeout() < MIN_TIME_OUT) {
properties.setConnectTimeout(MIN_TIME_OUT);
}

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(properties.getConnectTimeout());
requestFactory.setReadTimeout(properties.getReadTimeout());

return new RestTemplate(requestFactory);
}

}
86 changes: 23 additions & 63 deletions tinyid-client/src/main/java/com/xiaoju/uemc/tinyid/client/config/TinyIdClientConfig.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,73 +1,33 @@
package com.xiaoju.uemc.tinyid.client.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.List;

/**
* @author du_imba
*/
@Data
public class TinyIdClientConfig {

private String tinyIdToken;
private String tinyIdServer;
private List<String> serverList;
private Integer readTimeout;
private Integer connectTimeout;

private volatile static TinyIdClientConfig tinyIdClientConfig;

private TinyIdClientConfig() {
}

public static TinyIdClientConfig getInstance() {
if (tinyIdClientConfig != null) {
return tinyIdClientConfig;
}
synchronized (TinyIdClientConfig.class) {
if (tinyIdClientConfig != null) {
return tinyIdClientConfig;
}
tinyIdClientConfig = new TinyIdClientConfig();
}
return tinyIdClientConfig;
}

public String getTinyIdToken() {
return tinyIdToken;
}

public void setTinyIdToken(String tinyIdToken) {
this.tinyIdToken = tinyIdToken;
}

public String getTinyIdServer() {
return tinyIdServer;
}

public void setTinyIdServer(String tinyIdServer) {
this.tinyIdServer = tinyIdServer;
}

public List<String> getServerList() {
return serverList;
}

public void setServerList(List<String> serverList) {
this.serverList = serverList;
}

public Integer getReadTimeout() {
return readTimeout;
}

public void setReadTimeout(Integer readTimeout) {
this.readTimeout = readTimeout;
}

public Integer getConnectTimeout() {
return connectTimeout;
}

public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
private String cacheServerUrl;
private String noCacheServerUrl;
private String token;
private boolean localCache;

public TinyIdClientConfig(String cacheServerUrl, String noCacheServerUrl, String token, boolean localCache) {
this.cacheServerUrl = cacheServerUrl;
this.noCacheServerUrl = noCacheServerUrl;
this.token = token;
this.localCache = localCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.xiaoju.uemc.tinyid.client.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "tinyid")
public class TinyIdClientConfigProperties {
/**
* 本地缓存开启时,会预读取两段号段到本项目中,然后从本地内存获得ID
* 缓存关闭时,http直接访问服务端获得ID
*/
private boolean localCache = false;
/**
* 服务端地址
*/
private String server;
/**
* 本地token
*/
private String token;

private int readTimeout = 5000;

private int connectTimeout = 5000;
}
Loading