Skip to content

Commit

Permalink
✨ Support auto config remote grpc client (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro authored Apr 28, 2023
1 parent f8d72b4 commit eada044
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ class KubernetesClientRepository : ClientRepository {
it.name == labelValue || it.port.toString() == labelValue
}?.port ?: continue
val host = k8sService.metadata?.name ?: continue
val tls = labelValue == "grpcs"
logger.info("GRPC service '$service' discovered in kubernetes service '$host:$port'.")
val channel = createGrpcChannel("$host:$port", channelBuilderInterceptors.values, managedChannelLifecycle)
val channel = createGrpcChannel("$host:$port", tls, channelBuilderInterceptors.values, managedChannelLifecycle)
val client = getClientFromService(service.javaClass.declaringClass)
val clientBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(client as Class<Any>) {
interceptStub(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ interface ClientRepository : Ordered {

fun createGrpcChannel(
target: String,
tls: Boolean,
builderInterceptors: Iterable<ChannelBuilderInterceptor>,
lifecycle: ManagedChannelLifecycle
): Channel {
var builder = ManagedChannelBuilder.forTarget(target).usePlaintext().userAgent("Generated by Sisyphus")
var builder = ManagedChannelBuilder
.forTarget(target)
.userAgent("Generated by Sisyphus")
.enableRetry()
.maxRetryAttempts(3)
.let {
if (tls) {
it.useTransportSecurity()
} else {
it.usePlaintext()
}
}

for (interceptor in builderInterceptors) {
builder = interceptor.intercept(builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.bybutter.sisyphus.middleware.grpc

import io.grpc.CallOptions
import org.springframework.boot.context.properties.NestedConfigurationProperty

data class GrpcChannelProperty(
val name: String,
val target: String,
val services: Set<Class<*>>,
val tls: Boolean = false,
val options: CallOptions = CallOptions.DEFAULT,
val extensions: Map<String, Any> = mapOf()
)

data class GrpcChannelProperties(
@NestedConfigurationProperty
val grpc: Map<String, GrpcChannelProperty>
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.springframework.beans.factory.getBean
import org.springframework.beans.factory.getBeansOfType
import org.springframework.beans.factory.support.AbstractBeanDefinition
import org.springframework.beans.factory.support.BeanDefinitionBuilder
import org.springframework.boot.context.properties.bind.Binder
import org.springframework.core.env.Environment

class RemoteClientRepository : ClientRepository {
Expand All @@ -19,7 +20,12 @@ class RemoteClientRepository : ClientRepository {
beanFactory: ConfigurableListableBeanFactory,
environment: Environment
): List<AbstractBeanDefinition> {
val properties = beanFactory.getBeansOfType<GrpcChannelProperty>()
val properties = beanFactory.getBeansOfType<GrpcChannelProperty>().toMutableMap()
val grpcProperties = Binder.get(environment)
.bind("sisyphus", GrpcChannelProperties::class.java)
.orElse(null)?.grpc ?: mapOf()
properties += grpcProperties

if (properties.isEmpty()) return arrayListOf()
val beanDefinitionList = arrayListOf<AbstractBeanDefinition>()

Expand All @@ -31,7 +37,12 @@ class RemoteClientRepository : ClientRepository {
beanFactory.getBean<ManagedChannelLifecycle>(ClientRegistrar.QUALIFIER_AUTO_CONFIGURED_GRPC_CHANNEL_LIFECYCLE)

for (property in properties.values) {
val channel = createGrpcChannel(property.target, channelBuilderInterceptors.values, managedChannelLifecycle)
val channel = createGrpcChannel(
property.target,
property.tls,
channelBuilderInterceptors.values,
managedChannelLifecycle
)
beanFactory.registerSingleton(property.name, channel)
for (service in property.services) {
val client = getClientFromService(service)
Expand Down

0 comments on commit eada044

Please sign in to comment.