-
Notifications
You must be signed in to change notification settings - Fork 858
新增对ServiceConfig配置的支持 #574
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
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 1 commit
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 hidden or 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 |
---|---|---|
|
@@ -22,10 +22,7 @@ | |
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.*; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
|
@@ -34,6 +31,7 @@ | |
import javax.annotation.PreDestroy; | ||
import javax.annotation.concurrent.GuardedBy; | ||
|
||
import net.devh.boot.grpc.client.config.*; | ||
import org.springframework.util.unit.DataSize; | ||
|
||
import com.google.common.collect.Lists; | ||
|
@@ -45,10 +43,7 @@ | |
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.NegotiationType; | ||
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry; | ||
|
||
/** | ||
|
@@ -168,11 +163,59 @@ 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 | ||
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. |
||
} | ||
} | ||
|
||
/** | ||
* build service config object | ||
* | ||
* @param properties The properties of | ||
*/ | ||
protected Map<String, Object> buildDefaultServiceConfig(final GrpcChannelProperties properties) { | ||
Map<String, Object> serviceConfig = new HashMap<>(); | ||
List<MethodConfig> methodConfigList = properties.getMethodConfig(); | ||
if (null == methodConfigList || methodConfigList.isEmpty()) { | ||
return serviceConfig; | ||
} | ||
List<Map<String, Object>> methodConfigJsonList = new ArrayList<>(); | ||
methodConfigList.forEach(methodConfig -> { | ||
Map<String, Object> methodConfigMap = new HashMap<>(); | ||
//set method config | ||
List<NameConfig> nameConfigList = methodConfig.getName(); | ||
if (null != nameConfigList && !nameConfigList.isEmpty()) { | ||
List<Map<String, Object>> list = new ArrayList<>(); | ||
nameConfigList.forEach(nameConfig -> list.add(nameConfig.buildMap())); | ||
methodConfigMap.put("name", list); | ||
} | ||
//set retry policy | ||
RetryPolicyConfig retryConfig = methodConfig.getRetryPolicy(); | ||
if (retryConfig != null) { | ||
methodConfigMap.put("retryPolicy", retryConfig.buildMap()); | ||
} | ||
methodConfigJsonList.add(methodConfigMap); | ||
}); | ||
serviceConfig.put("methodConfig", methodConfigJsonList); | ||
return serviceConfig; | ||
} | ||
|
||
/** | ||
* Configures the keep alive options that should be used by the channel. | ||
* | ||
|
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
...pring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/MethodConfig.java
This file contains hidden or 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,64 @@ | ||
package net.devh.boot.grpc.client.config; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.ToString; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @Title 方法配置信息实体, 请参考如下链接: | ||
* https://github.com/grpc/proposal/blob/37e658b12f1684f29b3acca04f0167b84d502876/A6-client-retries.md#grpc-retry-design | ||
* https://github.com/grpc/grpc/blob/master/doc/service_config.md | ||
* @Description MethodConfig | ||
* @Program spring-cloud-tcl-starter | ||
* @Author wushengju | ||
* @Version 1.0 | ||
* @Date 2021-08-10 13:38 | ||
* @Copyright Copyright (c) 2021 TCL Inc. All rights reserved | ||
wushengju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
**/ | ||
@ToString | ||
@EqualsAndHashCode | ||
public class MethodConfig { | ||
/** | ||
* retry policy config | ||
*/ | ||
private RetryPolicyConfig retryPolicy; | ||
/** | ||
* name for list | ||
*/ | ||
private List<NameConfig> name; | ||
|
||
public RetryPolicyConfig getRetryPolicy() { | ||
return retryPolicy; | ||
} | ||
|
||
public void setRetryPolicy(RetryPolicyConfig retryPolicy) { | ||
this.retryPolicy = retryPolicy; | ||
} | ||
|
||
public List<NameConfig> getName() { | ||
return name; | ||
} | ||
|
||
public void setName(List<NameConfig> name) { | ||
this.name = name; | ||
} | ||
|
||
public void copyDefaultsFrom(final MethodConfig config) { | ||
if (this == config) { | ||
return; | ||
} | ||
this.retryPolicy.copyDefaultsFrom(config.retryPolicy); | ||
if (this.name == null || this.name.isEmpty()) { | ||
if (config.getName() != null && !config.getName().isEmpty()) { | ||
this.name = new ArrayList<>(); | ||
config.getName().forEach(nameConfig -> { | ||
NameConfig newConfig = new NameConfig(); | ||
newConfig.copyDefaultsFrom(nameConfig); | ||
this.name.add(newConfig); | ||
}); | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/NameConfig.java
This file contains hidden or 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,63 @@ | ||
package net.devh.boot.grpc.client.config; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.ToString; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @Title method config | ||
* @Description MethodConfig | ||
* @Program spring-cloud-tcl-starter | ||
* @Author wushengju | ||
* @Version 1.0 | ||
* @Date 2021-08-10 10:55 | ||
* @Copyright Copyright (c) 2021 TCL Inc. All rights reserved | ||
**/ | ||
@ToString | ||
@EqualsAndHashCode | ||
public class NameConfig { | ||
/** | ||
* the service name which defined in xx.proto | ||
*/ | ||
private String service; | ||
/** | ||
* the method name which you will call | ||
*/ | ||
private String method; | ||
|
||
public Map<String, Object> buildMap() { | ||
Map<String, Object> map = new HashMap<>(); | ||
map.put("service", this.getService()); | ||
map.put("method", this.getMethod()); | ||
return map; | ||
} | ||
|
||
public void copyDefaultsFrom(final NameConfig config) { | ||
if (this == config) { | ||
return; | ||
} | ||
if (this.service == null) { | ||
this.service = config.service; | ||
} | ||
if (this.method == null) { | ||
this.method = config.method; | ||
} | ||
} | ||
public String getService() { | ||
return service; | ||
} | ||
|
||
public void setService(String service) { | ||
this.service = service; | ||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public void setMethod(String method) { | ||
this.method = method; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.