Skip to content
Snippets Groups Projects
Commit 348c7e78 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3630 cancel change of grpc library

parent afa6ca15
Branches
Tags
No related merge requests found
......@@ -52,14 +52,19 @@
</dependency>
<!-- spring -->
<!-- <dependency>-->
<!-- <groupId>io.github.lognet</groupId>-->
<!-- <artifactId>grpc-client-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.github.lognet</groupId>-->
<!-- <artifactId>grpc-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>io.github.lognet</groupId>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.github.lognet</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
</dependency>
<!-- Tools -->
<dependency>
......
......@@ -23,28 +23,29 @@
*/
package de.itvsh.kop.eingangsadapter.router;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import jakarta.annotation.PostConstruct;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import de.itvsh.kop.eingangsadapter.common.errorhandling.TechnicalException;
import de.itvsh.kop.eingangsadapter.router.GrpcClientsProperties.ClientProperty;
import de.itvsh.kop.eingangsadapter.router.PlutoListProperties.FallbackStrategy;
import de.itvsh.kop.eingangsadapter.router.PlutoListProperties.RoutingStrategy;
import de.itvsh.kop.eingangsadapter.router.errorhandling.AdapterConfigurationException;
import de.itvsh.kop.eingangsadapter.router.errorhandling.UnknownOrganisationseinheitException;
import de.itvsh.ozg.pluto.grpc.binaryFile.BinaryFileServiceGrpc;
import de.itvsh.ozg.pluto.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServiceStub;
import de.itvsh.ozg.pluto.vorgang.VorgangServiceGrpc;
import de.itvsh.ozg.pluto.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub;
import io.grpc.Channel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.AbstractStub;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.devh.boot.grpc.client.channelfactory.GrpcChannelFactory;
import net.devh.boot.grpc.client.inject.StubTransformer;
import net.devh.boot.grpc.client.stubfactory.StubFactory;
@Component
@Log4j2
......@@ -52,14 +53,31 @@ public class PlutoServerResolver {
static final String CHANNEL_NAME_PREFIX = "pluto-";
@Autowired
private GrpcChannelFactory grpcChannelFactory;
@Autowired
@Valid
private PlutoListProperties properties;
// TODO cache / shutdown connection
private Channel lastChannel;
private StubFactory vorgangStubFactory = VorgangServiceGrpc::newBlockingStub;
private StubFactory binaryFileAsynStubFactory = BinaryFileServiceGrpc::newStub;
@Autowired(required = false)
private Collection<StubFactory> stubFactories = Collections.emptyList();
@Autowired(required = false)
private Collection<StubTransformer> stubTransformers = Collections.emptyList();
private StubFactory vorgangStubFactory;
private StubFactory binaryFileAsynStubFactory;
@PostConstruct
void findApplicableStubFactories() {
vorgangStubFactory = findStubFactory(VorgangServiceBlockingStub.class);
binaryFileAsynStubFactory = findStubFactory(BinaryFileServiceStub.class);
}
StubFactory findStubFactory(Class<? extends AbstractStub<?>> stubClass) {
return stubFactories.stream()
.filter(factory -> factory.isApplicable(stubClass))
.findFirst().orElseThrow(() -> new AdapterConfigurationException("Cannot find Stub-Factory for GRPC-" + stubClass));
}
public VorgangServiceBlockingStub resolveVorgangServiceBlockingStubByOrganisationseinheitenId(Optional<String> organisationsEinheitId) {
return (VorgangServiceBlockingStub) createStub(organisationsEinheitId, vorgangStubFactory, VorgangServiceBlockingStub.class);
......@@ -71,8 +89,16 @@ public class PlutoServerResolver {
AbstractStub<?> createStub(Optional<String> organisationsEinheitId, StubFactory stubFactory, Class<? extends AbstractStub<?>> stubClass) {// NOSONAR
var channelName = getChannelName(organisationsEinheitId);
// return applyStubTransformers(stub, channelName);
return stubFactory.createStub(createChannelByName(channelName));
var stub = stubFactory.createStub(stubClass, createChannelByName(channelName));
return applyStubTransformers(stub, channelName);
}
AbstractStub<?> applyStubTransformers(AbstractStub<?> stub, String channelName) { // NOSONAR wildcard given by StubTransformer
for (var transformer : stubTransformers) {
stub = transformer.transform(channelName, stub);
}
return stub;
}
String getChannelName(Optional<String> organisationsEinheitId) {
......@@ -106,25 +132,6 @@ public class PlutoServerResolver {
}
Channel createChannelByName(String name) {
return properties.getClientProperties().map(GrpcClientsProperties::getClient).map(clientsByName -> clientsByName.get(name))
.map(this::createChannel)
.orElseThrow(() -> new TechnicalException("Configuration-Error on creating Channel to VorgangMananger: " + name));
// TODO do not use get
// TODO check if there is a client
// var clientProperty = properties.getClientProperties().get().getClient().get(name);
// lastChannel = ManagedChannelBuilder.forTarget(clientProperty.getAddress()).usePlaintext().build();
// return lastChannel;
// return grpcChannelFactory.createChannel(name);
}
private Channel createChannel(ClientProperty clientProperty) {
return ManagedChannelBuilder.forTarget(clientProperty.getAddress()).usePlaintext().build();
}
return grpcChannelFactory.createChannel(name);
}
@FunctionalInterface
interface StubFactory {
AbstractStub<?> createStub(Channel channel);
}
\ No newline at end of file
# Autoconfiguration.imports
Fix for using grpc starter with spring-boot 3.
Remove wenn PR ist released:
https://github.com/yidongnan/grpc-spring-boot-starter/pull/775/commits/836fcabaa9327d75640c37dbb0bc7f45a20b563e
\ No newline at end of file
net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration
net.devh.boot.grpc.client.autoconfigure.GrpcClientMetricAutoConfiguration
net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration
net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration
net.devh.boot.grpc.client.autoconfigure.GrpcClientTraceAutoConfiguration
net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration
\ No newline at end of file
......@@ -35,7 +35,7 @@ import de.itvsh.ozg.pluto.grpc.binaryFile.BinaryFileServiceGrpc.BinaryFileServic
import de.itvsh.ozg.pluto.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub;
@SpringBootTest(properties = {
"grpc.client.pluto-kiel.address=127.0.0.1:9090"
"grpc.client.pluto-kiel.address=static://127.0.0.1:9090"
})
class PlutoServerResolverITCase {
......
......@@ -28,6 +28,8 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
......@@ -43,27 +45,29 @@ import de.itvsh.kop.eingangsadapter.router.errorhandling.UnknownOrganisationsein
import de.itvsh.ozg.pluto.vorgang.VorgangServiceGrpc.VorgangServiceBlockingStub;
import io.grpc.Channel;
import io.grpc.stub.AbstractStub;
import net.devh.boot.grpc.client.channelfactory.GrpcChannelFactory;
import net.devh.boot.grpc.client.inject.StubTransformer;
import net.devh.boot.grpc.client.stubfactory.StubFactory;
class PlutoServerResolverTest {
@Spy
@InjectMocks
private PlutoServerResolver resolver;
@Spy
private PlutoListProperties properties = PlutoListPropertiesTestFactory.createForSingleRouting();
@Mock
private GrpcChannelFactory channelFactory;
@Mock
private StubFactory stubFactory;
@Nested
class TestCreateChannel {
@Test
void shouldCreateChannel() {
var channel = resolver.createChannelByName(PlutoListPropertiesTestFactory.CHANNEL_NAME);
void shouldCallChannelFactory() {
resolver.createChannelByName(PlutoListPropertiesTestFactory.PLUTO_NAME);
assertThat(channel).isNotNull().extracting(ch -> ch.authority()).isEqualTo(PlutoListPropertiesTestFactory.ADDRESS);
verify(channelFactory).createChannel(PlutoListPropertiesTestFactory.PLUTO_NAME);
}
// TODO missing channel
}
@Nested
......@@ -135,6 +139,34 @@ class PlutoServerResolverTest {
}
}
@Nested
class TestFindStubFactory {
@Mock
private StubFactory stubFactory;
@Test
void shouldSetApplicableFactory() {
when(stubFactory.isApplicable(any())).thenReturn(true);
setStubFactories(stubFactory, stubFactory);
resolver.findApplicableStubFactories();
assertThat(ReflectionTestUtils.getField(resolver, "vorgangStubFactory")).isSameAs(stubFactory);
}
@Test
void shouldThrowExceptionIfNotFound() {
setStubFactories(stubFactory);
assertThrows(AdapterConfigurationException.class, () -> resolver.findApplicableStubFactories());
}
private void setStubFactories(StubFactory... factories) {
ReflectionTestUtils.setField(resolver, "stubFactories", Arrays.asList(factories));
}
}
@Nested
class TestCreateStub {
......@@ -158,11 +190,18 @@ class PlutoServerResolverTest {
verify(resolver).createChannelByName(PlutoListPropertiesTestFactory.CHANNEL_NAME);
}
@Test
void shouldApplyTransformers() {
createStub();
verify(resolver).applyStubTransformers(any(), any());
}
@Test
void shouldCreateStubByFactory() {
createStub();
verify(stubFactory).createStub(any());
verify(stubFactory).createStub(eq(stubClass), any());
}
private AbstractStub<?> createStub() {
......@@ -170,6 +209,27 @@ class PlutoServerResolverTest {
}
}
@Nested
class TestApplyStubTransformer {
@Mock
private AbstractStub<?> stub;
@Mock
private StubTransformer transformer;
@BeforeEach
void init() {
ReflectionTestUtils.setField(resolver, "stubTransformers", Collections.singleton(transformer));
}
@Test
void shouldCallTransform() {
resolver.applyStubTransformers(stub, PlutoListPropertiesTestFactory.CHANNEL_NAME);
verify(transformer).transform(PlutoListPropertiesTestFactory.CHANNEL_NAME, stub);
}
}
private void setProperties(PlutoListProperties properties) {
ReflectionTestUtils.setField(resolver, "properties", properties);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment