Skip to content
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

Adding @ServiceConnection spring boot support with Testcontainers #1118

Merged
merged 17 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions dapr-spring/dapr-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-core</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-data</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,33 @@

import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.spring.core.client.DaprClientCustomizer;
import org.springframework.beans.factory.ObjectProvider;
import io.dapr.config.Properties;
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(DaprConnectionDetails daprConnectionDetails) {
DaprClientBuilder builder = new DaprClientBuilder();

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

@Bean
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 The Dapr 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
* http://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 io.dapr.spring.boot.autoconfigure.client;

import io.dapr.spring.data.DaprKeyValueAdapterResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small question, should we use the old Java style Java Beans or should we recommend users to go with Java records?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both should be fine right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, I would go with java beans in order to add javadoc and could generate the metadata, the configuration processor is already in the classpath. See https://docs.spring.io/spring-boot/specification/configuration-metadata/annotation-processor.html

@ConfigurationProperties(prefix = "dapr.client")
public class DaprClientProperties {
private String httpEndpoint;
private String grpcEndpoint;
private Integer httpPort;
private Integer grpcPort;


/**
* Constructs a {@link DaprClientProperties}.
*/
public DaprClientProperties() {
}

/**
* Constructs a {@link DaprClientProperties}.
* @param httpEndpoint http endpoint to interact with the Dapr Sidecar
* @param grpcEndpoint grpc endpoint to interact with the Dapr Sidecar
* @param httpPort http port to interact with the Dapr Sidecar
* @param grpcPort grpc port to interact with the Dapr Sidecar
*/
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort) {
this.httpEndpoint = httpEndpoint;
this.grpcEndpoint = grpcEndpoint;
this.httpPort = httpPort;
this.grpcPort = grpcPort;
}

public String getHttpEndpoint() {
return httpEndpoint;
}

public String getGrpcEndpoint() {
return grpcEndpoint;
}

public Integer getHttpPort() {
return httpPort;
}

public Integer getGrpcPort() {
return grpcPort;
}

salaboy marked this conversation as resolved.
Show resolved Hide resolved
public void setHttpEndpoint(String httpEndpoint) {
this.httpEndpoint = httpEndpoint;
}

public void setGrpcEndpoint(String grpcEndpoint) {
this.grpcEndpoint = grpcEndpoint;
}

public void setHttpPort(Integer httpPort) {
this.httpPort = httpPort;
}

public void setGrpcPort(Integer grpcPort) {
this.grpcPort = grpcPort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@
limitations under the License.
*/

package io.dapr.spring.core.client;
package io.dapr.spring.boot.autoconfigure.client;

import io.dapr.client.DaprClientBuilder;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;

/**
* Callback interface that can be used to customize a {@link DaprClientBuilder}.
*/
@FunctionalInterface
public interface DaprClientCustomizer {
public interface DaprConnectionDetails extends ConnectionDetails {
String httpEndpoint();

/**
* Callback to customize a {@link DaprClientBuilder} instance.
*
* @param daprClientBuilder the client builder to customize
*/
void customize(DaprClientBuilder daprClientBuilder);
String grpcEndpoint();

Integer httpPort();

Integer grpcPort();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 The Dapr 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
* http://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 io.dapr.spring.boot.autoconfigure.client;

class PropertiesDaprConnectionDetails implements DaprConnectionDetails {

private final DaprClientProperties daprClientProperties;

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

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

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

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

@Override
public Integer grpcPort() {
return this.daprClientProperties.getGrpcPort();
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>dapr-spring-boot-starter-test</artifactId>
<name>dapr-spring-boot-starter-test</name>
<description>Dapr Spring Boot Starter Tests (with Testcontainers Support)</description>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-boot-tests</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>testcontainers-dapr</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,32 @@

<artifactId>dapr-spring-boot-starter</artifactId>
<name>dapr-spring-boot-starter</name>
<description>Dapr Client Spring Boot Starter</description>
<description>Dapr Spring Boot Starter</description>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk-springboot</artifactId>
<version>${dapr.sdk.version}</version>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-core</artifactId>
<artifactId>dapr-spring-boot-autoconfigure</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-boot-autoconfigure</artifactId>
<artifactId>dapr-spring-data</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>dapr-spring-messaging</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
Expand Down
Loading
Loading