This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 443fb4a85 [type:feat] java sdk module init. (#3926)
443fb4a85 is described below
commit 443fb4a85151787f5fa1d00eb766c09b1f2c2a60
Author: yunlongn <[email protected]>
AuthorDate: Thu Sep 8 10:33:00 2022 +0800
[type:feat] java sdk module init. (#3926)
* [type:feat] java sdk module init.
* [type:feat] java sdk module init.
* [type:feat] java sdk module init.
* [type:feat] java sdk module init.
* [type:feat] java sdk module init.
* [type:feat] java sdk module init.
* [type:feat] java sdk module init.
* [type:test] Maybe 3 milliseconds cause occasional exceptions.
---
pom.xml | 1 +
.../loadbalancer/spi/HashLoadBalanceTest.java | 4 +-
.../common/subsriber/WatcherListener.java} | 32 +--
.../api/ShenyuInstanceRegisterRepository.java | 15 ++
shenyu-sdk/pom.xml | 43 ++++
.../apache/shenyu/sdk/core/RequestTemplate.java | 269 +++++++++++++++++++++
.../org/apache/shenyu/sdk/core/ShenyuRequest.java | 132 ++++++++++
.../org/apache/shenyu/sdk/core/ShenyuResponse.java | 101 ++++++++
.../shenyu/sdk/http/api/ShenyuHttpClient.java | 37 +--
9 files changed, 585 insertions(+), 49 deletions(-)
diff --git a/pom.xml b/pom.xml
index 58b6bb969..b7df26f02 100644
--- a/pom.xml
+++ b/pom.xml
@@ -43,6 +43,7 @@
<module>shenyu-loadbalancer</module>
<module>shenyu-dist</module>
<module>shenyu-alert</module>
+ <module>shenyu-sdk</module>
</modules>
<licenses>
diff --git
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalanceTest.java
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalanceTest.java
index bd85b7a4a..9f643fa58 100644
---
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalanceTest.java
+++
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalanceTest.java
@@ -129,8 +129,8 @@ public final class HashLoadBalanceTest {
.weight(1)
.build()), 1);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
- .timestamp(System.currentTimeMillis() - 3L)
- .warmup(4)
+ .timestamp(System.currentTimeMillis() - 3000L)
+ .warmup(4000)
.weight(4)
.build()), 3);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
diff --git
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/subsriber/WatcherListener.java
similarity index 61%
copy from
shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
copy to
shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/subsriber/WatcherListener.java
index 785baddcb..8082aed6b 100644
---
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/subsriber/WatcherListener.java
@@ -15,36 +15,22 @@
* limitations under the License.
*/
-package org.apache.shenyu.register.instance.api;
+package org.apache.shenyu.register.common.subsriber;
-import org.apache.shenyu.common.config.ShenyuConfig.InstanceConfig;
import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
-import org.apache.shenyu.spi.SPI;
+
+import java.util.List;
/**
- * Shenyu instance register repository.
+ * WatcherListener InstanceRegisterDTO.
*/
-@SPI
-public interface ShenyuInstanceRegisterRepository {
+@FunctionalInterface
+public interface WatcherListener {
/**
- * Init.
- *
- * @param config the config
- */
- default void init(InstanceConfig config) {
- }
-
- /**
- * Persist instance.
+ * listener InstanceRegisterDTO.
*
- * @param instance instance
- */
- void persistInstance(InstanceRegisterDTO instance);
-
- /**
- * Close.
+ * @param instanceRegisterDTOs instanceRegisterDTOs
*/
- default void close() {
- }
+ void listener(List<InstanceRegisterDTO> instanceRegisterDTOs);
}
diff --git
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
index 785baddcb..384137a21 100644
---
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
@@ -19,8 +19,12 @@ package org.apache.shenyu.register.instance.api;
import org.apache.shenyu.common.config.ShenyuConfig.InstanceConfig;
import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
+import org.apache.shenyu.register.common.subsriber.WatcherListener;
import org.apache.shenyu.spi.SPI;
+import java.util.Collections;
+import java.util.List;
+
/**
* Shenyu instance register repository.
*/
@@ -41,6 +45,17 @@ public interface ShenyuInstanceRegisterRepository {
* @param instance instance
*/
void persistInstance(InstanceRegisterDTO instance);
+
+ /**
+ * selectInstancesAndWatcher.
+ *
+ * @param selectKey selectKey
+ * @param watcherListener watcherListener
+ * @return {@link List}
+ */
+ default List<InstanceRegisterDTO> selectInstancesAndWatcher(String
selectKey, WatcherListener watcherListener) {
+ return Collections.emptyList();
+ }
/**
* Close.
diff --git a/shenyu-sdk/pom.xml b/shenyu-sdk/pom.xml
new file mode 100644
index 000000000..38ba9d30c
--- /dev/null
+++ b/shenyu-sdk/pom.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<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>
+ <artifactId>shenyu</artifactId>
+ <groupId>org.apache.shenyu</groupId>
+ <version>2.5.1-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>shenyu-sdk</artifactId>
+
+ <properties>
+ <maven.compiler.source>8</maven.compiler.source>
+ <maven.compiler.target>8</maven.compiler.target>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-spi</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+</project>
\ No newline at end of file
diff --git
a/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/RequestTemplate.java
b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/RequestTemplate.java
new file mode 100644
index 000000000..47268457f
--- /dev/null
+++ b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/RequestTemplate.java
@@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.sdk.core;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Request Builder for an HTTP.
+ */
+public final class RequestTemplate implements Serializable {
+
+ private String url;
+
+ private String path;
+
+ private ShenyuRequest.HttpMethod httpMethod;
+
+ private List<ParamMetadata> paramMetadataList;
+
+ private Map<String, Collection<String>> headers = new HashMap<>();
+
+ private String body;
+
+ /**
+ * request.
+ *
+ * @return {@link ShenyuRequest}
+ */
+ public ShenyuRequest request() {
+ return ShenyuRequest.create(this.httpMethod, this.url + this.path,
this.headers, this.body, this);
+ }
+
+ /**
+ * getUrl.
+ *
+ * @return {@link String}
+ */
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ * setUrl.
+ *
+ * @param url url
+ * @return {@link RequestTemplate}
+ */
+ public RequestTemplate setUrl(final String url) {
+ this.url = url;
+ return this;
+ }
+
+ /**
+ * getHttpMethod.
+ *
+ * @return {@link ShenyuRequest.HttpMethod}
+ */
+ public ShenyuRequest.HttpMethod getHttpMethod() {
+ return httpMethod;
+ }
+
+ /**
+ * setHttpMethod.
+ *
+ * @param method method
+ * @return {@link RequestTemplate}
+ */
+ public RequestTemplate setHttpMethod(final ShenyuRequest.HttpMethod
method) {
+ this.httpMethod = method;
+ return this;
+ }
+
+ /**
+ * getHeaders.
+ *
+ * @return {@link Map}
+ */
+ public Map<String, Collection<String>> getHeaders() {
+ return headers;
+ }
+
+ /**
+ * setHeaders.
+ *
+ * @param headers headers
+ * @return {@link RequestTemplate}
+ */
+ public RequestTemplate setHeaders(final Map<String, Collection<String>>
headers) {
+ this.headers = headers;
+ return this;
+ }
+
+ /**
+ * getBody.
+ *
+ * @return {@link String}
+ */
+ public String getBody() {
+ return body;
+ }
+
+ /**
+ * setBody.
+ *
+ * @param body body
+ */
+ public void setBody(final String body) {
+ this.body = body;
+ }
+
+ /**
+ * getPath.
+ *
+ * @return {@link String}
+ */
+ public String getPath() {
+ return path;
+ }
+
+ /**
+ * setPath.
+ *
+ * @param path path
+ * @return {@link RequestTemplate}
+ */
+ public RequestTemplate setPath(final String path) {
+ this.path = path;
+ return this;
+ }
+
+ /**
+ * getParamMetadataList.
+ *
+ * @return {@link List}
+ */
+ public List<ParamMetadata> getParamMetadataList() {
+ return paramMetadataList;
+ }
+
+ /**
+ * setParamMetadataList.
+ *
+ * @param paramMetadataList paramMetadataList
+ * @return {@link RequestTemplate}
+ */
+ public RequestTemplate setParamMetadataList(final List<ParamMetadata>
paramMetadataList) {
+ this.paramMetadataList = paramMetadataList;
+ return this;
+ }
+
+ /**
+ * method ParamMetadata.
+ */
+ public static class ParamMetadata {
+
+ /**
+ * paramAnnotations.
+ */
+ private Annotation[] paramAnnotations;
+
+ /**
+ * paramType.
+ */
+ private Class<?> paramType;
+
+ /**
+ * isPrimitive.
+ */
+ private boolean isPrimitive;
+
+ /**
+ * paramIndexOnMethod.
+ */
+ private int paramIndexOnMethod;
+
+ /**
+ * getParamAnnotations.
+ *
+ * @return {@link Annotation}
+ */
+ public Annotation[] getParamAnnotations() {
+ return paramAnnotations;
+ }
+
+ /**
+ * setParamAnnotations.
+ *
+ * @param paramAnnotations paramAnnotations
+ */
+ public void setParamAnnotations(final Annotation[] paramAnnotations) {
+ this.paramAnnotations = paramAnnotations;
+ }
+
+ /**
+ * getParamType.
+ *
+ * @return {@link Class}
+ */
+ public Class<?> getParamType() {
+ return paramType;
+ }
+
+ /**
+ * setParamType.
+ *
+ * @param paramType paramType
+ */
+ public void setParamType(final Class<?> paramType) {
+ this.paramType = paramType;
+ }
+
+ /**
+ * isPrimitive.
+ *
+ * @return {@link boolean}
+ */
+ public boolean isPrimitive() {
+ return isPrimitive;
+ }
+
+ /**
+ * setPrimitive.
+ *
+ * @param primitive primitive
+ */
+ public void setPrimitive(final boolean primitive) {
+ isPrimitive = primitive;
+ }
+
+ /**
+ * getParamIndexOnMethod.
+ *
+ * @return {@link int}
+ */
+ public int getParamIndexOnMethod() {
+ return paramIndexOnMethod;
+ }
+
+ /**
+ * setParamIndexOnMethod.
+ *
+ * @param paramIndexOnMethod paramIndexOnMethod
+ */
+ public void setParamIndexOnMethod(final int paramIndexOnMethod) {
+ this.paramIndexOnMethod = paramIndexOnMethod;
+ }
+
+ }
+}
diff --git
a/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/ShenyuRequest.java
b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/ShenyuRequest.java
new file mode 100644
index 000000000..5625f8af5
--- /dev/null
+++ b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/ShenyuRequest.java
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.sdk.core;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Map;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * An immutable request to an http server.
+ */
+public final class ShenyuRequest implements Serializable {
+
+ private final HttpMethod httpMethod;
+
+ private final String url;
+
+ private final Map<String, Collection<String>> headers;
+
+ private final String body;
+
+ private final RequestTemplate requestTemplate;
+
+ /**
+ * Creates a new Request.
+ *
+ * @param method of the request.
+ * @param url for the request.
+ * @param headers for the request.
+ * @param body for the request, optional.
+ * @param requestTemplate used to build the request.
+ */
+ ShenyuRequest(final HttpMethod method,
+ final String url,
+ final Map<String, Collection<String>> headers,
+ final String body,
+ final RequestTemplate requestTemplate) {
+ this.httpMethod = checkNotNull(method, "httpMethod of %s",
method.name());
+ this.url = checkNotNull(url, "url");
+ this.headers = checkNotNull(headers, "headers of %s %s", method, url);
+ this.body = body;
+ this.requestTemplate = requestTemplate;
+ }
+
+ public enum HttpMethod {
+
+ /**
+ * HttpMethod.
+ */
+ GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH
+ }
+
+ /**
+ * Builds a Request. All parameters must be effectively immutable, via
safe copies.
+ *
+ * @param httpMethod for the request.
+ * @param url for the request.
+ * @param headers to include.
+ * @param body of the request, can be {@literal null}
+ * @param requestTemplate requestTemplate
+ * @return a Request
+ */
+ public static ShenyuRequest create(final HttpMethod httpMethod,
+ final String url,
+ final Map<String, Collection<String>>
headers,
+ final String body,
+ final RequestTemplate requestTemplate) {
+ return new ShenyuRequest(httpMethod, url, headers, body,
requestTemplate);
+ }
+
+ /**
+ * getHttpMethod.
+ *
+ * @return {@link HttpMethod}
+ */
+ public HttpMethod getHttpMethod() {
+ return httpMethod;
+ }
+
+ /**
+ * getUrl.
+ *
+ * @return {@link String}
+ */
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ * getHeaders.
+ *
+ * @return {@link Map}
+ */
+ public Map<String, Collection<String>> getHeaders() {
+ return headers;
+ }
+
+ /**
+ * getBody.
+ *
+ * @return {@link String}
+ */
+ public String getBody() {
+ return body;
+ }
+
+ /**
+ * getRequestTemplate.
+ *
+ * @return {@link RequestTemplate}
+ */
+ public RequestTemplate getRequestTemplate() {
+ return requestTemplate;
+ }
+}
diff --git
a/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/ShenyuResponse.java
b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/ShenyuResponse.java
new file mode 100644
index 000000000..85731a534
--- /dev/null
+++ b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/core/ShenyuResponse.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.sdk.core;
+
+import java.util.Collection;
+import java.util.Map;
+
+
+/**
+ * ShenyuResponse.
+ */
+public final class ShenyuResponse {
+
+ private final int status;
+
+ private final String reason;
+
+ private final Map<String, Collection<String>> headers;
+
+ private final String body;
+
+ private final ShenyuRequest request;
+
+ /**
+ * ShenyuResponse.
+ *
+ * @param status status
+ * @param reason reason
+ * @param headers headers
+ * @param body body
+ * @param request request
+ */
+ public ShenyuResponse(final int status, final String reason, final
Map<String, Collection<String>> headers,
+ final String body, final ShenyuRequest request) {
+ this.status = status;
+ this.reason = reason;
+ this.headers = headers;
+ this.body = body;
+ this.request = request;
+ }
+
+ /**
+ * getStatus.
+ *
+ * @return {@link int}
+ */
+ public int getStatus() {
+ return status;
+ }
+
+ /**
+ * getReason.
+ *
+ * @return {@link String}
+ */
+ public String getReason() {
+ return reason;
+ }
+
+ /**
+ * getHeaders.
+ *
+ * @return {@link Map}
+ */
+ public Map<String, Collection<String>> getHeaders() {
+ return headers;
+ }
+
+ /**
+ * getBody.
+ *
+ * @return {@link String}
+ */
+ public String getBody() {
+ return body;
+ }
+
+ /**
+ * getRequest.
+ *
+ * @return {@link ShenyuRequest}
+ */
+ public ShenyuRequest getRequest() {
+ return request;
+ }
+}
diff --git
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/http/api/ShenyuHttpClient.java
similarity index 57%
copy from
shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
copy to
shenyu-sdk/src/main/java/org/apache/shenyu/sdk/http/api/ShenyuHttpClient.java
index 785baddcb..c08d6da4a 100644
---
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-api/src/main/java/org/apache/shenyu/register/instance/api/ShenyuInstanceRegisterRepository.java
+++
b/shenyu-sdk/src/main/java/org/apache/shenyu/sdk/http/api/ShenyuHttpClient.java
@@ -15,36 +15,25 @@
* limitations under the License.
*/
-package org.apache.shenyu.register.instance.api;
+package org.apache.shenyu.sdk.http.api;
-import org.apache.shenyu.common.config.ShenyuConfig.InstanceConfig;
-import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
-import org.apache.shenyu.spi.SPI;
+import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.apache.shenyu.sdk.core.ShenyuResponse;
+
+import java.io.IOException;
/**
- * Shenyu instance register repository.
+ * ShenyuHttpClient.
*/
-@SPI
-public interface ShenyuInstanceRegisterRepository {
+public interface ShenyuHttpClient {
/**
- * Init.
- *
- * @param config the config
- */
- default void init(InstanceConfig config) {
- }
-
- /**
- * Persist instance.
+ * execute.
*
- * @param instance instance
+ * @param request request
+ * @return {@link ShenyuResponse}
+ * @throws IOException error
*/
- void persistInstance(InstanceRegisterDTO instance);
-
- /**
- * Close.
- */
- default void close() {
- }
+ ShenyuResponse execute(ShenyuRequest request) throws IOException;
+
}