Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Add API consumer project #7

Merged
merged 1 commit into from
Jun 26, 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
94 changes: 94 additions & 0 deletions account-service-consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.sample.schulung.spring</groupId>
<artifactId>account-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Account Service Consumer</name>
<properties>
<java.version>17</java.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
<exclude>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.sample.schulung.accounts.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AccountServiceConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(AccountServiceConsumerApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.sample.schulung.accounts.consumer.boundary;

import de.sample.schulung.accounts.consumer.domain.client.CustomerDto;
import de.sample.schulung.accounts.consumer.domain.CustomersService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;

@Controller
@RequestMapping("/customer-names")
@RequiredArgsConstructor
public class CustomersController {

private final CustomersService service;

@GetMapping(
produces = MediaType.TEXT_PLAIN_VALUE
)
@ResponseBody
public Flux<String> getCustomerNames() {
return service
.findAll()
.map(CustomerDto::getName);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.sample.schulung.accounts.consumer.domain;

import de.sample.schulung.accounts.consumer.domain.client.CustomerDto;
import de.sample.schulung.accounts.consumer.domain.client.CustomersApi;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
@RequiredArgsConstructor
public class CustomersService {

private final CustomersApi remoteApi;

public Flux<CustomerDto> findAll() {
return remoteApi.readAllCustomers();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.sample.schulung.accounts.consumer.domain.client;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CustomerDto {

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private UUID uuid;
@Size(min = 3, max = 100)
private String name;
@JsonProperty("birthdate")
private LocalDate dateOfBirth;
private String state;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.sample.schulung.accounts.consumer.domain.client;

import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import reactor.core.publisher.Flux;

@HttpExchange("/customers")
public interface CustomersApi {

@GetExchange
Flux<CustomerDto> readAllCustomers();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.sample.schulung.accounts.consumer.domain.client;

import io.netty.channel.ChannelOption;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

@Configuration
public class CustomersApiConfiguration {

@Bean
CustomersApi customersApi() {
var httpClient = HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
.responseTimeout(Duration.ofMillis(1000));
var webClient = WebClient
.builder()
.baseUrl("http://localhost:8080/api/v1")
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
var adapter = WebClientAdapter
.create(webClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builderFor(adapter)
.build();

return factory
.createClient(CustomersApi.class);
}

}
2 changes: 2 additions & 0 deletions account-service-consumer/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8081
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<modules>
<module>account-service-provider</module>
<module>account-service-consumer</module>
</modules>

</project>
Loading