Skip to content

Commit

Permalink
adding service connection plumbing
Browse files Browse the repository at this point in the history
Signed-off-by: salaboy <Salaboy@gmail.com>
  • Loading branch information
salaboy committed Sep 3, 2024
1 parent 935f3be commit 13673ab
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,37 @@

import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.config.Properties;
import io.dapr.spring.core.client.DaprClientCustomizer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.stream.Collectors;

@AutoConfiguration
@ConditionalOnClass(DaprClient.class)
@EnableConfigurationProperties(DaprClientProperties.class)
public class DaprClientAutoConfiguration {

@Bean
@ConditionalOnMissingBean
DaprClientBuilderConfigurer daprClientBuilderConfigurer(ObjectProvider<DaprClientCustomizer> customizerProvider) {
DaprClientBuilderConfigurer configurer = new DaprClientBuilderConfigurer();
configurer.setDaprClientCustomizer(customizerProvider.orderedStream().collect(Collectors.toList()));

return configurer;
@ConditionalOnMissingBean(DaprConnectionDetails.class)
DaprConnectionDetails daprConnectionDetails(DaprClientProperties properties) {
return new PropertiesDaprConnectionDetails(properties);
}

@Bean
@ConditionalOnMissingBean
DaprClientBuilder daprClientBuilder(DaprClientBuilderConfigurer daprClientBuilderConfigurer) {
DaprClientBuilder daprClientBuilder(DaprClientProperties daprClientProperties) {
DaprClientBuilder builder = new DaprClientBuilder();

return daprClientBuilderConfigurer.configure(builder);
builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprClientProperties.httpEndpoint());
builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprClientProperties.grpcEndpoint());
builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprClientProperties.httpPort()));
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprClientProperties.grpcPort()));
return builder;
}

@Bean
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.dapr.spring.boot.autoconfigure.client;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "dapr.client")
public record DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.dapr.spring.boot.autoconfigure.client;


import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;

public interface DaprConnectionDetails extends ConnectionDetails {
String httpEndpoint();
String grpcEndpoint();
Integer httpPort();
Integer grcpPort();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.dapr.spring.boot.autoconfigure.client;

public class PropertiesDaprConnectionDetails implements DaprConnectionDetails {

private final DaprClientProperties daprClientProperties;

public PropertiesDaprConnectionDetails(DaprClientProperties daprClientProperties) {
this.daprClientProperties = daprClientProperties;
}

@Override
public String httpEndpoint() {
return this.daprClientProperties.httpEndpoint();
}

@Override
public String grpcEndpoint() {
return this.daprClientProperties.grpcEndpoint();
}

@Override
public Integer httpPort() {
return this.daprClientProperties.httpPort();
}

@Override
public Integer grcpPort() {
return this.daprClientProperties.grpcPort();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ class DaprClientAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(DaprClientAutoConfiguration.class));

@Test
void daprClientBuilderConfigurer() {
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClientBuilderConfigurer.class));
}

@Test
void daprClientBuilder() {
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClientBuilder.class));
Expand Down
45 changes: 45 additions & 0 deletions dapr-spring/dapr-spring-boot-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-parent</artifactId>
<version>0.13.0-SNAPSHOT</version>
</parent>

<artifactId>dapr-spring-boot-tests</artifactId>
<name>dapr-spring-boot-tests</name>
<description>Dapr Spring Boot Tests</description>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-boot-autoconfigure</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>testcontainers-dapr</artifactId>
<version>${dapr.sdk.alpha.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.dapr.spring.boot.tests;

import io.dapr.spring.boot.autoconfigure.client.DaprConnectionDetails;
import io.dapr.testcontainers.DaprContainer;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;

public class DaprContainerConnectionDetailsFactory extends ContainerConnectionDetailsFactory<DaprContainer, DaprConnectionDetails> {
DaprContainerConnectionDetailsFactory() {
}
protected DaprConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<DaprContainer> source) {
return new DaprContainerConnectionDetails(source);
}
private static final class DaprContainerConnectionDetails extends ContainerConnectionDetailsFactory.ContainerConnectionDetails<DaprContainer> implements DaprConnectionDetails {
private DaprContainerConnectionDetails(ContainerConnectionSource<DaprContainer> source) {
super(source);
}

@Override
public String httpEndpoint() {
return getContainer().getHttpEndpoint();
}

@Override
public String grpcEndpoint() {
return getContainer().getGrpcEndpoint();
}

@Override
public Integer httpPort() {
return getContainer().getHttpPort();
}

@Override
public Integer grcpPort() {
return getContainer().getGrpcPort();
}
}
}
1 change: 1 addition & 0 deletions dapr-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>dapr-spring-data</module>
<module>dapr-spring-messaging</module>
<module>dapr-spring-boot-autoconfigure</module>
<module>dapr-spring-boot-tests</module>
<module>dapr-spring-boot-starters/dapr-spring-boot-starter</module>
</modules>

Expand Down

This file was deleted.

0 comments on commit 13673ab

Please sign in to comment.