-
Notifications
You must be signed in to change notification settings - Fork 838
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
新增对ServiceConfig配置的支持 #574
Open
wushengju
wants to merge
7
commits into
grpc-ecosystem:master
Choose a base branch
from
wushengju:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89e9a34
新增对ServiceConfig配置的支持
wushengju d438e02
新增对ServiceConfig配置的支持
wushengju 4bb0f8f
add ServiceConfig for retry policy
wushengju 20c2d06
add ServiceConfig for retry policy
wushengju 495fb11
Temp: Cleanup, slight refactor, add retry tests
ST-DDT 1da6fea
Merge branch 'master' into master
ST-DDT e3ffb21
Add missing license headers
ST-DDT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
@@ -48,6 +49,7 @@ | |
import net.devh.boot.grpc.client.config.GrpcChannelProperties; | ||
import net.devh.boot.grpc.client.config.GrpcChannelProperties.Security; | ||
import net.devh.boot.grpc.client.config.GrpcChannelsProperties; | ||
import net.devh.boot.grpc.client.config.MethodConfig; | ||
import net.devh.boot.grpc.client.config.NegotiationType; | ||
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry; | ||
|
||
|
@@ -56,7 +58,6 @@ | |
* connection pooling and thus needs to be {@link #close() closed} after usage. | ||
* | ||
* @param <T> The type of builder used by this channel factory. | ||
* | ||
* @author Michael ([email protected]) | ||
* @author Daniel Theuke ([email protected]) | ||
* @since 5/17/16 | ||
|
@@ -168,11 +169,43 @@ protected void configure(final T builder, final String name) { | |
configureSecurity(builder, name); | ||
configureLimits(builder, name); | ||
configureCompression(builder, name); | ||
configureRetryEnabled(builder, name); | ||
for (final GrpcChannelConfigurer channelConfigurer : this.channelConfigurers) { | ||
channelConfigurer.accept(builder, name); | ||
} | ||
} | ||
|
||
/** | ||
* Configures the retry options that should be used by the channel. | ||
* | ||
* @param builder The channel builder to configure. | ||
* @param name The name of the client to configure. | ||
*/ | ||
protected void configureRetryEnabled(final T builder, final String name) { | ||
final GrpcChannelProperties properties = getPropertiesFor(name); | ||
if (properties.isRetryEnabled()) { | ||
builder.enableRetry(); | ||
// build retry policy by default service config | ||
// TODO: Wrap field in defaultServiceConfig | ||
builder.defaultServiceConfig(buildDefaultServiceConfig(properties)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default service config might be used outside of the retry logic. |
||
} | ||
} | ||
|
||
/** | ||
* Builds the service config object. | ||
* | ||
* @param properties The properties of | ||
* @return The json alike service config. | ||
*/ | ||
protected Map<String, Object> buildDefaultServiceConfig(final GrpcChannelProperties properties) { | ||
final Map<String, Object> serviceConfig = new LinkedHashMap<>(); | ||
final List<MethodConfig> methodConfigList = properties.getMethodConfig(); | ||
if (methodConfigList != null && !methodConfigList.isEmpty()) { | ||
serviceConfig.put("methodConfig", MethodConfig.buildMaps(methodConfigList)); | ||
} | ||
return serviceConfig; | ||
} | ||
|
||
/** | ||
* Configures the keep alive options that should be used by the channel. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...pring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/MethodConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2016-2021 Michael Zhang <[email protected]> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
* Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package net.devh.boot.grpc.client.config; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.Data; | ||
|
||
|
||
/** | ||
* The method config for retry policy config. | ||
* | ||
* <p> | ||
* For the exact specification refer to: | ||
* <a href="https://github.com/grpc/proposal/blob/master/A6-client-retries.md">A6-client-retries</a> | ||
* </p> | ||
* | ||
* @author wushengju | ||
*/ | ||
@Data | ||
public class MethodConfig { | ||
|
||
/** | ||
* retry policy config | ||
*/ | ||
private RetryPolicyConfig retryPolicy; | ||
/** | ||
* name for list | ||
*/ | ||
private List<NameConfig> name; | ||
|
||
|
||
/** | ||
* Creates a copy of this instance. | ||
* | ||
* @return The newly created copy. | ||
*/ | ||
public MethodConfig copy() { | ||
final MethodConfig copy = new MethodConfig(); | ||
copy.retryPolicy = requireNonNull(this.retryPolicy, "retryPolicy").copy(); | ||
copy.name = NameConfig.copy(this.name); | ||
return copy; | ||
} | ||
|
||
/** | ||
* Creates a copy of the given instances. | ||
* | ||
* @param configs The configs to copy. | ||
* @return The copied instances. | ||
*/ | ||
public static List<MethodConfig> copy(final List<MethodConfig> configs) { | ||
return requireNonNull(configs, "configs").stream() | ||
.map(MethodConfig::copy) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Builds a json like map from this instance. | ||
* | ||
* @return The json like map representation of this instance. | ||
*/ | ||
public Map<String, Object> buildMap() { | ||
final Map<String, Object> map = new LinkedHashMap<>(); | ||
if (this.name != null && !this.name.isEmpty()) { | ||
map.put("name", NameConfig.buildMaps(this.name)); | ||
} | ||
if (this.retryPolicy != null) { | ||
map.put("retryPolicy", this.retryPolicy.buildMap()); | ||
} | ||
return map; | ||
} | ||
|
||
/** | ||
* Builds a json like map from the given instances. | ||
* | ||
* @param configs The configs to convert. | ||
* @return The json like array of maps representation of the instances. | ||
*/ | ||
public static List<Map<String, Object>> buildMaps(final List<MethodConfig> configs) { | ||
return requireNonNull(configs, "configs").stream() | ||
.map(MethodConfig::buildMap) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
...-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/NameConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) 2016-2021 Michael Zhang <[email protected]> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
* Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package net.devh.boot.grpc.client.config; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* The name config for service and method. | ||
* | ||
* <p> | ||
* If both the service and method name are empty, then this applies to all requests. | ||
* </p> | ||
* | ||
* <p> | ||
* If only the method name is empty, then this applies to all methods in the given service. | ||
* </p> | ||
* | ||
* <p> | ||
* For the exact specification refer to: | ||
* <a href="https://github.com/grpc/proposal/blob/master/A6-client-retries.md">A6-client-retries</a> | ||
* </p> | ||
* | ||
* @author wushengju | ||
*/ | ||
@Data | ||
public class NameConfig { | ||
|
||
/** | ||
* The service name as defined in your proto file. May be empty to match all services, but may never be null. | ||
*/ | ||
private String service = ""; | ||
/** | ||
* The method name which you will call. May be empty to match all method within the service, but may never be null. | ||
*/ | ||
private String method = ""; | ||
|
||
/** | ||
* Creates a copy of this instance. | ||
* | ||
* @return The newly created copy. | ||
*/ | ||
public NameConfig copy() { | ||
final NameConfig copy = new NameConfig(); | ||
copy.service = this.service; | ||
copy.method = this.method; | ||
return copy; | ||
} | ||
|
||
/** | ||
* Creates a copy of the given instances. | ||
* | ||
* @param configs The configs to copy. | ||
* @return The copied instances. | ||
*/ | ||
public static List<NameConfig> copy(final List<NameConfig> configs) { | ||
return requireNonNull(configs, "configs").stream() | ||
.map(NameConfig::copy) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Builds a json like map from this instance. | ||
* | ||
* @return The json like map representation of this instance. | ||
*/ | ||
public Map<String, Object> buildMap() { | ||
final Map<String, Object> map = new LinkedHashMap<>(); | ||
map.put("service", this.service); | ||
map.put("method", this.method); | ||
return map; | ||
} | ||
|
||
/** | ||
* Builds a json like map from the given instances. | ||
* | ||
* @param configs The configs to convert. | ||
* @return The json like array of maps representation of the instances. | ||
*/ | ||
public static List<Map<String, Object>> buildMaps(final List<NameConfig> configs) { | ||
return requireNonNull(configs, "configs").stream() | ||
.map(NameConfig::buildMap) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid conflicts with other values in the
serviceConfig
it might be neccessary to wrap themethodConfig
property in adefaultServiceConfig
property for clarity.