-
Notifications
You must be signed in to change notification settings - Fork 735
Add Grpc Client for Spring Cloud Loadbalancer #1677
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
Closed
Closed
Changes from all commits
Commits
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
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
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
54 changes: 54 additions & 0 deletions
54
...ncer/src/main/java/org/springframework/cloud/loadbalancer/annotation/grpc/GrpcClient.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,54 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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.springframework.cloud.loadbalancer.annotation.grpc; | ||
|
|
||
| import org.springframework.core.annotation.AliasFor; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * @author KouShenhai(laokou) | ||
| */ | ||
| @Inherited | ||
| @Documented | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) | ||
| public @interface GrpcClient { | ||
|
|
||
| /** | ||
| * Synonym for name (the name of the grpc client). | ||
| * | ||
| * @see #name() | ||
| * @return the name of the load balancer grpc client | ||
| */ | ||
| @AliasFor("name") | ||
| String value() default ""; | ||
|
|
||
| /** | ||
| * The name of the load balancer grpc client, uniquely identifying a set of client | ||
| * resources, including a load balancer. | ||
| * @return the name of the load balancer grpc client | ||
| */ | ||
| @AliasFor("value") | ||
| String name() default ""; | ||
|
|
||
| } |
91 changes: 91 additions & 0 deletions
91
...a/org/springframework/cloud/loadbalancer/annotation/grpc/GrpcClientBeanPostProcessor.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,91 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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.springframework.cloud.loadbalancer.annotation.grpc; | ||
|
|
||
| import org.jspecify.annotations.NonNull; | ||
| import org.springframework.beans.BeansException; | ||
| import org.springframework.beans.factory.config.BeanPostProcessor; | ||
| import org.springframework.core.annotation.AnnotationUtils; | ||
| import org.springframework.grpc.client.GrpcClientFactory; | ||
| import org.springframework.util.ObjectUtils; | ||
| import org.springframework.util.ReflectionUtils; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| /** | ||
| * @author KouShenhai(laokou) | ||
| */ | ||
| public class GrpcClientBeanPostProcessor implements BeanPostProcessor { | ||
|
|
||
| private final GrpcClientFactory grpcClientFactory; | ||
|
|
||
| public GrpcClientBeanPostProcessor(GrpcClientFactory grpcClientFactory) { | ||
| this.grpcClientFactory = grpcClientFactory; | ||
| } | ||
|
|
||
| @Override | ||
| public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName) | ||
| throws BeansException { | ||
| Class<?> clazz = bean.getClass(); | ||
| do { | ||
| setFields(bean, clazz); | ||
| setMethods(bean, clazz); | ||
| clazz = clazz.getSuperclass(); | ||
| } | ||
| while (!ObjectUtils.isEmpty(clazz)); | ||
| return bean; | ||
| } | ||
|
|
||
| private void setFields(Object bean, Class<?> clazz) { | ||
| for (Field field : clazz.getDeclaredFields()) { | ||
| GrpcClient grpcClient = AnnotationUtils.findAnnotation(field, GrpcClient.class); | ||
| if (!ObjectUtils.isEmpty(grpcClient)) { | ||
| ReflectionUtils.makeAccessible(field); | ||
| ReflectionUtils.setField(field, bean, getClient(field.getType(), grpcClient)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void setMethods(Object bean, Class<?> clazz) { | ||
| for (Method method : clazz.getDeclaredMethods()) { | ||
| GrpcClient grpcClient = AnnotationUtils.findAnnotation(method, GrpcClient.class); | ||
| if (!ObjectUtils.isEmpty(grpcClient)) { | ||
| ReflectionUtils.makeAccessible(method); | ||
| ReflectionUtils.invokeMethod(method, bean, getClient(method.getParameterTypes()[0], grpcClient)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private <T> T getClient(Class<T> type, @NonNull GrpcClient grpcClient) { | ||
| String target = String.format("discovery://%s", getClientName(grpcClient)); | ||
| return grpcClientFactory.getClient(target, type, null); | ||
| } | ||
|
|
||
| private String getClientName(GrpcClient grpcClient) { | ||
| String value = grpcClient.value(); | ||
| if (!StringUtils.hasText(value)) { | ||
| value = grpcClient.name(); | ||
| } | ||
| if (StringUtils.hasText(value)) { | ||
| return value; | ||
| } | ||
| throw new IllegalStateException("Either 'name' or 'value' must be provided in @GrpcClient"); | ||
| } | ||
|
|
||
| } |
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
58 changes: 58 additions & 0 deletions
58
...java/org/springframework/cloud/loadbalancer/support/grpc/DiscoveryGrpcChannelFactory.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,58 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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.springframework.cloud.loadbalancer.support.grpc; | ||
|
|
||
| import io.grpc.ChannelCredentials; | ||
| import io.grpc.netty.NettyChannelBuilder; | ||
| import org.jspecify.annotations.NonNull; | ||
| import org.springframework.grpc.client.ClientInterceptorsConfigurer; | ||
| import org.springframework.grpc.client.GrpcChannelBuilderCustomizer; | ||
| import org.springframework.grpc.client.NettyGrpcChannelFactory; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Discovery Grpc ChannelFactory. | ||
| * | ||
| * @author KouShenhai(laokou) | ||
| */ | ||
| public class DiscoveryGrpcChannelFactory extends NettyGrpcChannelFactory { | ||
|
|
||
| /** | ||
| * 构建通道工厂实例. | ||
| * @param globalCustomizers 要应用于所有已创建频道的全局自定义设置 | ||
| * @param interceptorsConfigurer 配置已创建通道上的客户端拦截器 | ||
| */ | ||
| public DiscoveryGrpcChannelFactory( | ||
| List<GrpcChannelBuilderCustomizer<@NonNull NettyChannelBuilder>> globalCustomizers, | ||
| ClientInterceptorsConfigurer interceptorsConfigurer) { | ||
| super(globalCustomizers, interceptorsConfigurer); | ||
| setVirtualTargets((p) -> p.substring(12)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(String target) { | ||
| return target.startsWith("discovery:"); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| protected NettyChannelBuilder newChannelBuilder(@NonNull String target, @NonNull ChannelCredentials credentials) { | ||
| return NettyChannelBuilder.forTarget(String.format("discovery://%s", target), credentials); | ||
| } | ||
|
|
||
| } |
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.
This should all be managed by spring boot. Since this won't be merged until summer time (when we start on our new minor), this will be based on boot 4.2, but in the meantime you can start with 4.1 and spring grpc 1.1