Skip to content

Commit

Permalink
split archetype-rpc module,improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
kelin.tan authored and Kelin Tan committed Jun 20, 2022
1 parent b13edeb commit a848de3
Show file tree
Hide file tree
Showing 78 changed files with 180 additions and 192 deletions.
14 changes: 2 additions & 12 deletions archetype-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ description = 'archetype-common'
dependencies {
compile project(':archetype-beans')

api(group: 'org.springframework.boot', name: 'spring-boot-starter-web') {
exclude module: "spring-boot-starter-tomcat"
}
api 'org.apache.commons:commons-lang3'
api 'org.apache.commons:commons-collections4'
api 'org.apache.httpcomponents:httpclient'
api 'org.asynchttpclient:async-http-client'
api 'commons-io:commons-io'
api 'com.google.guava:guava'
api 'org.springframework.data:spring-data-commons'
api 'org.assertj:assertj-core'
api 'org.mybatis.spring.boot:mybatis-spring-boot-starter'
api 'org.springframework.boot:spring-boot-starter-undertow'
api 'org.springframework.boot:spring-boot-autoconfigure'
api 'org.springframework.boot:spring-boot-starter-jdbc'
api 'org.springframework.boot:spring-boot-starter-log4j2'
api 'org.springframework.boot:spring-boot-starter-webflux'
api "org.junit.jupiter:junit-jupiter"
api 'io.opentracing.contrib:opentracing-spring-jaeger-web-starter'
api 'io.opentracing:opentracing-mock'

testCompile project(':archetype-systest')
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class TracingConstants {

public static final String MDC_TRACING = "X-TRACE";
public static final String ERROR_META_TRACE_ID = "traceId";
public static final String CUSTOM_TRACING_NAME_HEADER = "X-CUSTOM-TRACE-NAME";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

package com.kelin.archetype.common.exception;

import com.google.common.base.Preconditions;
import com.kelin.archetype.beans.exception.RestException;
import org.springframework.http.HttpStatus;
import org.apache.http.HttpStatus;

/**
* @author Kelin Tan
Expand All @@ -14,89 +13,75 @@ private RestExceptionFactory() {
}

public static RestException toBadRequestException(int errorCode, String message) {
return toException(HttpStatus.BAD_REQUEST, errorCode, message);
return toException(HttpStatus.SC_BAD_REQUEST, errorCode, message);
}

public static RestException toBadRequestException(int errorCode) {
return toException(errorCode, HttpStatus.BAD_REQUEST);
return toException(errorCode, HttpStatus.SC_BAD_REQUEST, "");
}

public static RestException toSystemException(int errorCode, String message) {
return toException(HttpStatus.INTERNAL_SERVER_ERROR, errorCode, message);
return toException(HttpStatus.SC_INTERNAL_SERVER_ERROR, errorCode, message);
}

public static RestException toSystemException(String message) {
return toException(HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
return toException(HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_INTERNAL_SERVER_ERROR, message);
}

public static RestException toSystemException(int errorCode) {
return toException(errorCode, HttpStatus.INTERNAL_SERVER_ERROR);
return toException(errorCode, HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
}

public static RestException toSystemException() {
return toException(HttpStatus.INTERNAL_SERVER_ERROR);
return toSystemException(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}

public static RestException toNotFoundException(int errorCode, String message) {
return toException(HttpStatus.NOT_FOUND, errorCode, message);
return toException(HttpStatus.SC_NOT_FOUND, errorCode, message);
}

public static RestException toNotFoundException(int errorCode) {
return toException(errorCode, HttpStatus.NOT_FOUND);
return toException(errorCode, HttpStatus.SC_NOT_FOUND, "");
}

public static RestException toForbiddenException(int errorCode, String message) {
return toException(HttpStatus.FORBIDDEN, errorCode, message);
return toException(HttpStatus.SC_FORBIDDEN, errorCode, message);
}

public static RestException toForbiddenException(int errorCode) {
return toException(errorCode, HttpStatus.FORBIDDEN);
return toException(errorCode, HttpStatus.SC_FORBIDDEN, "");
}

public static RestException toUnAuthorizedException(int errorCode, String message) {
return toException(HttpStatus.UNAUTHORIZED, errorCode, message);
return toException(HttpStatus.SC_UNAUTHORIZED, errorCode, message);
}

public static RestException toUnAuthorizedException(int errorCode) {
return toException(errorCode, HttpStatus.UNAUTHORIZED);
return toException(errorCode, HttpStatus.SC_UNAUTHORIZED, "");
}

public static RestException toMethodNotAllowedException(int errorCode, String message) {
return toException(HttpStatus.METHOD_NOT_ALLOWED, errorCode, message);
return toException(HttpStatus.SC_METHOD_NOT_ALLOWED, errorCode, message);
}

public static RestException toMethodNotAllowedException(int errorCode) {
return toException(errorCode, HttpStatus.METHOD_NOT_ALLOWED);
return toException(errorCode, HttpStatus.SC_METHOD_NOT_ALLOWED, "");
}

public static RestException toConflictException(int errorCode, String message) {
return toException(HttpStatus.CONFLICT, errorCode, message);
return toException(HttpStatus.SC_CONFLICT, errorCode, message);
}

public static RestException toConflictException(int errorCode) {
return toException(errorCode, HttpStatus.CONFLICT);
return toException(errorCode, HttpStatus.SC_CONFLICT, "");
}

public static RestException toRpcException() {
return toException(HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.SERVICE_UNAVAILABLE.value(),
return toException(HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_SERVICE_UNAVAILABLE,
"Rpc Request Error");
}

public static RestException toException(int errorCode, HttpStatus httpStatus) {
Preconditions.checkNotNull(httpStatus);

return new RestException(httpStatus.value(), httpStatus.getReasonPhrase(), errorCode);
}

public static RestException toException(HttpStatus httpStatus) {
Preconditions.checkNotNull(httpStatus);

return new RestException(httpStatus.value(), httpStatus.getReasonPhrase());
}

public static RestException toException(HttpStatus status, int errorCode, String message) {
Preconditions.checkNotNull(status);

return new RestException(status.value(), message, errorCode);
public static RestException toException(int httpStatus, int errorCode, String errorMessage) {
return new RestException(httpStatus, errorMessage, errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
package com.kelin.archetype.common.http;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Preconditions;
import com.kelin.archetype.common.json.JsonConverter;
import com.kelin.archetype.common.utils.HttpUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.protocol.HTTP;
import org.assertj.core.util.Preconditions;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Dsl;
import org.asynchttpclient.ListenableFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.kelin.archetype.common.http;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Preconditions;
import com.kelin.archetype.common.json.JsonConverter;
import com.kelin.archetype.common.utils.HttpUtils;
import lombok.Getter;
Expand All @@ -22,7 +23,6 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.assertj.core.util.Preconditions;

import java.net.URI;
import java.util.LinkedHashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing;
package com.kelin.archetype.common.tracing;

import com.kelin.archetype.common.constants.Profile;
import io.opentracing.Tracer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http;
package com.kelin.archetype.common.tracing.http;

import io.opentracing.Span;
import io.opentracing.log.Fields;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http;
package com.kelin.archetype.common.tracing.http;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http;
package com.kelin.archetype.common.tracing.http;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http;
package com.kelin.archetype.common.tracing.http;

import com.kelin.archetype.core.tracing.http.asynchttpclient.TracingAsyncHttpClient;
import com.kelin.archetype.core.tracing.http.httpclient.TracingHttpClientBuilder;
import com.kelin.archetype.common.tracing.http.asynchttpclient.TracingAsyncHttpClient;
import com.kelin.archetype.common.tracing.http.httpclient.TracingHttpClientBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.asynchttpclient.AsyncHttpClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http.asynchttpclient;
package com.kelin.archetype.common.tracing.http.asynchttpclient;

import io.netty.handler.codec.http.HttpHeaders;
import io.opentracing.propagation.TextMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http.asynchttpclient;
package com.kelin.archetype.common.tracing.http.asynchttpclient;

import com.kelin.archetype.core.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.core.tracing.http.HttpResponseTracing;
import com.kelin.archetype.common.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.common.tracing.http.HttpResponseTracing;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpHeaders;
import io.opentracing.Scope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http.asynchttpclient;
package com.kelin.archetype.common.tracing.http.asynchttpclient;

import static com.kelin.archetype.common.constants.TracingConstants.ASYNC_COMPONENT_NAME;
import static com.kelin.archetype.common.constants.TracingConstants.CUSTOM_TRACING_NAME_HEADER;

import com.kelin.archetype.core.rpc.RpcConstants;
import com.kelin.archetype.core.tracing.CurrentTracer;
import com.kelin.archetype.core.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.core.tracing.http.HttpRequestTracing;
import com.kelin.archetype.common.tracing.CurrentTracer;
import com.kelin.archetype.common.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.common.tracing.http.HttpRequestTracing;
import io.netty.handler.codec.http.HttpHeaders;
import io.opentracing.Span;
import io.opentracing.Tracer;
Expand Down Expand Up @@ -77,12 +77,12 @@ public <T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> h
}

/**
* use rpc-header name when {@see RpcConstants.RPC_NAME_HEADER}
* use custom-header name
*/
private String getSpanName(Request request) {
HttpHeaders headers = request.getHeaders();
if (headers.contains(RpcConstants.RPC_NAME_HEADER)) {
return headers.get(RpcConstants.RPC_NAME_HEADER);
if (headers.contains(CUSTOM_TRACING_NAME_HEADER)) {
return headers.get(CUSTOM_TRACING_NAME_HEADER);
} else {
return request.getMethod();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http.httpclient;
package com.kelin.archetype.common.tracing.http.httpclient;

import io.opentracing.propagation.TextMap;
import org.apache.http.HttpRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http.httpclient;
package com.kelin.archetype.common.tracing.http.httpclient;

import static com.kelin.archetype.common.constants.TracingConstants.APACHE_COMPONENT_NAME;
import static com.kelin.archetype.common.constants.TracingConstants.CUSTOM_TRACING_NAME_HEADER;
import static com.kelin.archetype.common.constants.TracingConstants.PARENT_CONTEXT;

import com.kelin.archetype.common.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.common.tracing.http.HttpRequestTracing;
import com.kelin.archetype.common.tracing.http.HttpResponseTracing;
import com.kelin.archetype.common.utils.HttpUtils;
import com.kelin.archetype.core.rpc.RpcConstants;
import com.kelin.archetype.core.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.core.tracing.http.HttpRequestTracing;
import com.kelin.archetype.core.tracing.http.HttpResponseTracing;
import io.opentracing.References;
import io.opentracing.Scope;
import io.opentracing.Span;
Expand Down Expand Up @@ -160,10 +160,10 @@ protected CloseableHttpResponse handleNetworkProcessing(
}

/**
* use rpc-header name when {@see RpcConstants.RPC_NAME_HEADER}
* use custom-header name
*/
private String getLocalSpanName(HttpRequest httpRequest) {
Header rpcHeader = httpRequest.getFirstHeader(RpcConstants.RPC_NAME_HEADER);
Header rpcHeader = httpRequest.getFirstHeader(CUSTOM_TRACING_NAME_HEADER);
if (rpcHeader != null) {
return rpcHeader.getValue();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2021 Kelin Inc. All rights reserved.

package com.kelin.archetype.core.tracing.http.httpclient;
package com.kelin.archetype.common.tracing.http.httpclient;

import com.kelin.archetype.core.tracing.CurrentTracer;
import com.kelin.archetype.core.tracing.http.HttpClientSpanDecorator;
import com.kelin.archetype.common.tracing.CurrentTracer;
import com.kelin.archetype.common.tracing.http.HttpClientSpanDecorator;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import org.apache.http.client.RedirectStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.kelin.archetype.common.utils;

import com.google.common.base.Preconditions;
import com.kelin.archetype.common.exception.RestExceptionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
Expand All @@ -14,7 +15,6 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.assertj.core.util.Preconditions;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Param;
Expand Down Expand Up @@ -106,7 +106,7 @@ private static String buildQueryParams(Map<String, Object> parameterMap) {
}

public static URIBuilder safeURIBuilder(String host) {
Preconditions.checkNotNullOrEmpty(host);
Preconditions.checkNotNull(host);

try {
return new URIBuilder(host);
Expand Down
3 changes: 0 additions & 3 deletions archetype-common/src/main/resources/META-INF/spring.factories

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.kelin.archetype.common.database

import com.kelin.archetype.database.DbUtils
import com.kelin.archetype.test.KtTestUtils
import org.junit.jupiter.api.Test

Expand Down
18 changes: 0 additions & 18 deletions archetype-core/build.gradle

This file was deleted.

3 changes: 0 additions & 3 deletions archetype-core/src/main/resources/META-INF/spring.factories

This file was deleted.

Loading

0 comments on commit a848de3

Please sign in to comment.