Skip to content

Commit f77152f

Browse files
authored
Merge pull request #13 from reactivegroup/refactor/optimize_rpc
refactor: optimize rpc app mesh module
2 parents 2ac8aeb + 8ad98f2 commit f77152f

File tree

11 files changed

+67
-58
lines changed

11 files changed

+67
-58
lines changed

capa-spi-aws-config/src/main/java/group/rxcloud/capa/spi/aws/config/package-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
1817
package group.rxcloud.capa.spi.aws.config;

capa-spi-aws-mesh/pom.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<name>capa-skd-spi-aws-appmesh</name>
3131

3232
<properties>
33-
<capa-serialzer.version>1.0.0.RELEASE</capa-serialzer.version>
33+
<capa-serializer.version>1.0.1.RELEASE</capa-serializer.version>
3434
</properties>
3535

3636
<dependencies>
@@ -39,16 +39,18 @@
3939
<artifactId>capa-spi-aws-infrastructure</artifactId>
4040
</dependency>
4141

42+
<!-- https://mvnrepository.com/artifact/group.rxcloud/capa-serializer -->
4243
<dependency>
4344
<groupId>group.rxcloud</groupId>
44-
<artifactId>capa-serialzer</artifactId>
45-
<version>${capa-serialzer.version}</version>
45+
<artifactId>capa-serializer</artifactId>
46+
<version>${capa-serializer.version}</version>
4647
</dependency>
4748

4849
<dependency>
4950
<groupId>software.amazon.awssdk</groupId>
5051
<artifactId>appmesh</artifactId>
5152
</dependency>
53+
5254
<!-- unit test -->
5355
<dependency>
5456
<groupId>org.junit.jupiter</groupId>

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/config/AwsSpiOptionsLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public AwsRpcServiceOptions loadRpcServiceOptions(String appId) {
3434

3535
// get variable
3636
CapaEnvironment.DeployVpcEnvironment deployVpcEnvironment = CapaEnvironment.getDeployVpcEnvironment();
37-
int servicePort = AwsRpcEnvironment.getServicePort();
37+
final int servicePort = AwsRpcEnvironment.getServicePort();
3838

3939
// generate awsToAwsServiceOptions
4040
// appid is serviceId

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/constants/AwsRpcConstants.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@
1616
*/
1717
package group.rxcloud.capa.spi.aws.mesh.constants;
1818

19+
/**
20+
* The Aws rpc constants.
21+
*/
1922
public interface AwsRpcConstants {
2023

21-
interface Environments {
22-
23-
String AWS_RPC_APP_MESH_DEFAULT_PORT = "AWS_RPC_APP_MESH_DEFAULT_PORT";
24-
25-
}
26-
27-
interface RpcProperties {
24+
interface AppMeshProperties {
2825

2926
/**
3027
* The aws app mesh http url template
3128
* {serviceId}.svc.cluster.local is virtual service name (https://docs.aws.amazon.com/zh_cn/zh_cn/app-mesh/latest/userguide/virtual_services.html)
3229
*/
3330
String AWS_APP_MESH_TEMPLATE = "http://{serviceId}.svc.cluster.local:{servicePort}/{operation}";
3431

32+
String RPC_AWS_APP_MESH_DEFAULT_PORT = "8080";
33+
34+
String RPC_AWS_APP_MESH_PORT = "CAPA_RPC_AWS_APP_MESH_PORT";
3535
}
3636

3737
interface SerializerProperties {
3838

39-
String AWS_RPC_APP_MESH_SERIALIZER = "AWS_RPC_APP_MESH_SERIALIZER";
39+
String RPC_AWS_APP_MESH_DEFAULT_SERIALIZER = "baiji";
4040

41+
String RPC_AWS_APP_MESH_SERIALIZER = "CAPA_RPC_AWS_APP_MESH_SERIALIZER";
4142
}
4243
}

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/env/AwsRpcEnvironment.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
import java.util.Objects;
2424

25-
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.Environments.AWS_RPC_APP_MESH_DEFAULT_PORT;
26-
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.SerializerProperties.AWS_RPC_APP_MESH_SERIALIZER;
25+
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.AppMeshProperties.RPC_AWS_APP_MESH_DEFAULT_PORT;
26+
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.AppMeshProperties.RPC_AWS_APP_MESH_PORT;
27+
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.SerializerProperties.RPC_AWS_APP_MESH_DEFAULT_SERIALIZER;
28+
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.SerializerProperties.RPC_AWS_APP_MESH_SERIALIZER;
2729

2830
/**
2931
* Rpc System Environment Properties In Aws.
@@ -42,20 +44,28 @@ public abstract class AwsRpcEnvironment {
4244

4345
static {
4446
// setup server port
45-
String awsRpcAppMeshPort = System.getProperty(AWS_RPC_APP_MESH_DEFAULT_PORT);
47+
initPort();
48+
49+
// setup serializer
50+
initSerializer();
51+
}
52+
53+
private static void initPort() {
54+
String awsRpcAppMeshPort = System.getProperty(RPC_AWS_APP_MESH_PORT);
4655
if (StringUtils.isBlank(awsRpcAppMeshPort)) {
47-
awsRpcAppMeshPort = "8080";
56+
awsRpcAppMeshPort = RPC_AWS_APP_MESH_DEFAULT_PORT;
4857
}
4958
try {
50-
servicePort = Integer.valueOf(awsRpcAppMeshPort);
59+
servicePort = Integer.parseInt(awsRpcAppMeshPort);
5160
} catch (Exception e) {
52-
new CapaException(CapaErrorContext.PARAMETER_ERROR, "Rpc Port: " + awsRpcAppMeshPort);
61+
throw new CapaException(CapaErrorContext.PARAMETER_ERROR, "Rpc Port: " + awsRpcAppMeshPort);
5362
}
63+
}
5464

55-
// setup serializer
56-
String awsRpcAppMeshSerializer = System.getProperty(AWS_RPC_APP_MESH_SERIALIZER);
65+
private static void initSerializer() {
66+
String awsRpcAppMeshSerializer = System.getProperty(RPC_AWS_APP_MESH_SERIALIZER);
5767
if (StringUtils.isBlank(awsRpcAppMeshSerializer)) {
58-
awsRpcAppMeshSerializer = "baiji";
68+
awsRpcAppMeshSerializer = RPC_AWS_APP_MESH_DEFAULT_SERIALIZER;
5969
}
6070
serializer = awsRpcAppMeshSerializer;
6171
}
@@ -67,5 +77,4 @@ public static int getServicePort() {
6777
public static String getSerializer() {
6878
return Objects.requireNonNull(serializer, "Capa Serializer");
6979
}
70-
7180
}

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/http/AwsCapaHttp.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import group.rxcloud.capa.infrastructure.exceptions.CapaException;
2222
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
2323
import group.rxcloud.capa.spi.aws.mesh.config.AwsRpcServiceOptions;
24-
import group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants;
25-
import group.rxcloud.capa.spi.aws.mesh.http.serialzer.AwsCapaSerialzerProvider;
24+
import group.rxcloud.capa.spi.aws.mesh.http.serializer.AwsCapaSerializerProvider;
2625
import group.rxcloud.capa.spi.config.RpcServiceOptions;
2726
import group.rxcloud.capa.spi.http.CapaSerializeHttpSpi;
2827
import group.rxcloud.cloudruntimes.utils.TypeRef;
@@ -38,6 +37,8 @@
3837
import java.util.Objects;
3938
import java.util.concurrent.CompletableFuture;
4039

40+
import static group.rxcloud.capa.spi.aws.mesh.constants.AwsRpcConstants.AppMeshProperties.AWS_APP_MESH_TEMPLATE;
41+
4142
public class AwsCapaHttp extends CapaSerializeHttpSpi {
4243

4344
private static final Logger logger = LoggerFactory.getLogger(AwsCapaHttp.class);
@@ -49,7 +50,7 @@ public class AwsCapaHttp extends CapaSerializeHttpSpi {
4950
* @param objectSerializer the object serializer
5051
*/
5152
public AwsCapaHttp(OkHttpClient httpClient, CapaObjectSerializer objectSerializer) {
52-
super(httpClient, AwsCapaSerialzerProvider.getSerializerOrDefault(objectSerializer));
53+
super(httpClient, AwsCapaSerializerProvider.getSerializerOrDefault(objectSerializer));
5354
}
5455

5556
@Override
@@ -59,22 +60,19 @@ protected <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(String appId,
5960
Map<String, String> headers,
6061
TypeRef<T> type,
6162
RpcServiceOptions rpcServiceOptions) {
62-
6363
Objects.requireNonNull(rpcServiceOptions, "rpcServiceOptions");
6464
AwsToAwsHttpServiceMeshInvoker awsToAwsHttpServiceMeshInvoker = new AwsToAwsHttpServiceMeshInvoker();
65-
CompletableFuture<HttpResponse<T>> httpResponseCompletableFuture
66-
= awsToAwsHttpServiceMeshInvoker.doInvokeSpiApi(
67-
appId,
68-
method,
69-
requestData,
70-
headers,
71-
type,
72-
(AwsRpcServiceOptions) rpcServiceOptions);
73-
65+
CompletableFuture<HttpResponse<T>> httpResponseCompletableFuture =
66+
awsToAwsHttpServiceMeshInvoker.doInvokeSpiApi(
67+
appId,
68+
method,
69+
requestData,
70+
headers,
71+
type,
72+
(AwsRpcServiceOptions) rpcServiceOptions);
7473
return httpResponseCompletableFuture;
7574
}
7675

77-
7876
private interface AwsHttpInvoker {
7977

8078
/**
@@ -95,7 +93,6 @@ <T> CompletableFuture<HttpResponse<T>> doInvokeSpiApi(String appId,
9593
Map<String, String> headers,
9694
TypeRef<T> type,
9795
AwsRpcServiceOptions rpcServiceOptions);
98-
9996
}
10097

10198
/**
@@ -115,8 +112,8 @@ public <T> CompletableFuture<HttpResponse<T>> doInvokeSpiApi(String appId,
115112
Map<String, String> headers,
116113
TypeRef<T> type,
117114
AwsRpcServiceOptions rpcServiceOptions) {
118-
AwsRpcServiceOptions.AwsToAwsServiceOptions awsToAwsServiceOptions
119-
= rpcServiceOptions.getAwsToAwsServiceOptions();
115+
AwsRpcServiceOptions.AwsToAwsServiceOptions awsToAwsServiceOptions =
116+
rpcServiceOptions.getAwsToAwsServiceOptions();
120117

121118
final String serviceId = awsToAwsServiceOptions.getServiceId();
122119
final int servicePort = awsToAwsServiceOptions.getServicePort();
@@ -129,15 +126,14 @@ public <T> CompletableFuture<HttpResponse<T>> doInvokeSpiApi(String appId,
129126
return doAsyncInvoke(method, requestData, headers, type, serviceId, servicePort);
130127
}
131128

132-
133129
private <T> CompletableFuture<HttpResponse<T>> doAsyncInvoke(String method,
134130
Object requestData,
135131
Map<String, String> headers,
136132
TypeRef<T> type,
137133
String serviceId,
138134
int servicePort) {
139135
// generate app mesh http url
140-
final String appMeshHttpUrl = AwsRpcConstants.RpcProperties.AWS_APP_MESH_TEMPLATE
136+
final String appMeshHttpUrl = AWS_APP_MESH_TEMPLATE
141137
.replace("{serviceId}", serviceId)
142138
.replace("{servicePort}", String.valueOf(servicePort))
143139
.replace("{operation}", method);
@@ -146,7 +142,7 @@ private <T> CompletableFuture<HttpResponse<T>> doAsyncInvoke(String method,
146142
CompletableFuture<HttpResponse<T>> asyncInvoke0 = post(appMeshHttpUrl, requestData, headers, type);
147143
asyncInvoke0.exceptionally(throwable -> {
148144
if (logger.isWarnEnabled()) {
149-
logger.warn("[CtripCapaHttp] async invoke error", throwable);
145+
logger.warn("[AwsCapaHttp] async invoke error", throwable);
150146
}
151147
throw new CapaException(CapaErrorContext.DEPENDENT_SERVICE_ERROR, throwable);
152148
});
@@ -170,6 +166,5 @@ private <T> CompletableFuture<HttpResponse<T>> post(String url,
170166

171167
return doAsyncInvoke0(request, type);
172168
}
173-
174169
}
175170
}

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/http/serialzer/AwsCapaSerialzerProvider.java renamed to capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/http/serializer/AwsCapaSerializerProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package group.rxcloud.capa.spi.aws.mesh.http.serialzer;
17+
package group.rxcloud.capa.spi.aws.mesh.http.serializer;
1818

1919
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
2020
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;
@@ -24,9 +24,9 @@
2424
import java.util.Map;
2525

2626
/**
27-
* The capa serialzer provider.
27+
* The capa serializer provider.
2828
*/
29-
public interface AwsCapaSerialzerProvider {
29+
public interface AwsCapaSerializerProvider {
3030

3131
/**
3232
* Gets serializer or default.
@@ -51,8 +51,7 @@ final class AwsCapaSerializerFactory {
5151
SERIALIZER_FACTORY = new HashMap<>(2, 1);
5252

5353
SERIALIZER_FACTORY.put("default", new DefaultObjectSerializer());
54-
SERIALIZER_FACTORY.put("baiji", new BaijiSSJsonObjectSerialzer());
54+
SERIALIZER_FACTORY.put("baiji", new BaijiSSJsonObjectSerializer());
5555
}
5656
}
57-
5857
}

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/http/serialzer/BaijiSSJsonObjectSerialzer.java renamed to capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/http/serializer/BaijiSSJsonObjectSerializer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package group.rxcloud.capa.spi.aws.mesh.http.serialzer;
17+
package group.rxcloud.capa.spi.aws.mesh.http.serializer;
1818

19-
import group.rxcloud.capa.addons.serialzer.CapaSerialzer;
20-
import group.rxcloud.capa.addons.serialzer.ssjson.SSJsonSerializer;
19+
import group.rxcloud.capa.addons.serializer.CapaSerialzer;
20+
import group.rxcloud.capa.addons.serializer.ssjson.SSJsonSerializer;
2121
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
2222
import group.rxcloud.cloudruntimes.utils.TypeRef;
2323

@@ -28,7 +28,7 @@
2828
/**
2929
* Serializes and deserializes the object with baiji.
3030
*/
31-
public class BaijiSSJsonObjectSerialzer implements CapaObjectSerializer {
31+
public class BaijiSSJsonObjectSerializer implements CapaObjectSerializer {
3232

3333
private static final SSJsonSerializer SERIALIZER = CapaSerialzer.ssJsonSerializer;
3434

@@ -62,7 +62,6 @@ public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
6262
return (T) deserialize;
6363
}
6464

65-
6665
/**
6766
* Returns the content type of the request.
6867
*
@@ -72,5 +71,4 @@ public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
7271
public String getContentType() {
7372
return "application/bjjson";
7473
}
75-
7674
}

capa-spi-aws-mesh/src/test/java/group/rxcloud/capa/spi/aws/mesh/http/AwsCapaSerialzerProviderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919

2020
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
21-
import group.rxcloud.capa.spi.aws.mesh.http.serialzer.AwsCapaSerialzerProvider;
21+
import group.rxcloud.capa.spi.aws.mesh.http.serializer.AwsCapaSerializerProvider;
2222
import org.junit.jupiter.api.Assertions;
2323
import org.junit.jupiter.api.Test;
2424

2525
public class AwsCapaSerialzerProviderTest {
2626

2727
@Test
2828
public void testGetSerializerOrDefault_Success() {
29-
CapaObjectSerializer serializerOrDefault = AwsCapaSerialzerProvider.getSerializerOrDefault(null);
29+
CapaObjectSerializer serializerOrDefault = AwsCapaSerializerProvider.getSerializerOrDefault(null);
3030
Assertions.assertEquals("application/bjjson", serializerOrDefault.getContentType());
3131
}
3232

capa-spi-aws/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<groupId>group.rxcloud</groupId>
4040
<artifactId>capa-spi-aws-config</artifactId>
4141
</dependency>
42+
4243
<!-- unit test -->
4344
<dependency>
4445
<groupId>org.junit.jupiter</groupId>

0 commit comments

Comments
 (0)