JAVA-22208 spring cloud eureka to jdk17 (#14859)

* JAVA-22208 Spring-cloud-eureka upgrade to JDK 17

* JAVA-22208 Code cleaning

* JAVA-22208 clean up

* JAVA-22208 Review changes

* JAVA-22208 Removed duplicated circuit-breaker reference
This commit is contained in:
Anastasios Ioannidis 2023-10-13 09:53:16 +03:00 committed by GitHub
parent d01cdc6ed5
commit 2adc6c45c0
15 changed files with 302 additions and 69 deletions

View File

@ -726,9 +726,11 @@
<module>spring-cloud-modules/spring-cloud-circuit-breaker</module>
<module>spring-cloud-modules/spring-cloud-contract</module>
<module>spring-cloud-modules/spring-cloud-data-flow</module>
<module>spring-cloud-modules/spring-cloud-eureka</module>
<module>spring-cloud-modules/spring-cloud-netflix-feign</module>
<module>spring-cloud-modules/spring-cloud-stream-starters</module>
<module>spring-cloud-modules/spring-cloud-zuul-eureka-integration</module>
<module>spring-exceptions</module>
<module>spring-jenkins-pipeline</module>
<module>spring-core</module>
@ -1004,11 +1006,13 @@
<module>spring-drools</module>
<module>spring-cloud-modules/spring-cloud-azure</module>
<module>spring-cloud-modules/spring-cloud-circuit-breaker</module>
<module>spring-cloud-modules/spring-cloud-eureka</module>
<module>spring-cloud-modules/spring-cloud-contract</module>
<module>spring-cloud-modules/spring-cloud-data-flow</module>
<module>spring-cloud-modules/spring-cloud-netflix-feign</module>
<module>spring-cloud-modules/spring-cloud-stream-starters</module>
<module>spring-cloud-modules/spring-cloud-zuul-eureka-integration</module>
<module>spring-exceptions</module>
<module>spring-jenkins-pipeline</module>
<module>spring-core</module>

View File

@ -19,7 +19,8 @@
<modules>
<module>spring-cloud-loadbalancer</module>
<module>spring-cloud-config</module>
<module>spring-cloud-eureka</module>
<!--Please uncomment when spring-cloud-modules is moved to JDK9+ profile-->
<!--<module>spring-cloud-eureka</module>-->
<module>spring-cloud-hystrix</module>
<module>spring-cloud-bootstrap</module>
<module>spring-cloud-ribbon-client</module>

View File

@ -45,8 +45,6 @@
</dependencies>
<properties>
<spring-boot.version>2.1.2.RELEASE</spring-boot.version>
<spring-cloud-dependencies.version>Greenwich.RELEASE</spring-cloud-dependencies.version>
<log4j2.version>2.17.1</log4j2.version>
</properties>

View File

@ -41,7 +41,7 @@
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
@ -51,6 +51,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.8</version>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
@ -75,9 +89,18 @@
</dependency>
</dependencies>
<repositories>
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/release</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
@ -87,4 +110,9 @@
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

View File

@ -1,16 +1,15 @@
package com.baeldung.spring.cloud.client;
import com.baeldung.spring.cloud.model.Book;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient("books-service")
//@FeignClient(value="simple-books-client", url="${book.service.url}")
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baeldung.spring.cloud.model.Book;
@FeignClient(name = "books-service")
public interface BooksClient {
@RequestMapping("/books")
List<Book> getBooks();
}

View File

@ -8,6 +8,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ -21,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
@ActiveProfiles("test")
@EnableFeignClients
@EnableConfigurationProperties
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { WireMockConfig.class })
@ -29,12 +31,16 @@ class BooksClientIntegrationTest {
@Autowired
private WireMockServer mockBooksService;
@Autowired
private WireMockServer mockBooksService2;
@Autowired
private BooksClient booksClient;
@BeforeEach
void setUp() throws IOException {
setupMockBooksResponse(mockBooksService);
setupMockBooksResponse(mockBooksService2);
}
@Test

View File

@ -31,9 +31,6 @@ public class EurekaContainerConfig {
+ eurekaServer.getFirstMappedPort().toString()
+ "/eureka")
.applyTo(configurableApplicationContext);
}
}
}

View File

@ -9,23 +9,39 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.loadbalancer.support.ServiceInstanceListSuppliers;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static com.baeldung.spring.cloud.client.BookMocks.setupMockBooksResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.moreThan;
import static java.util.Arrays.asList;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.jupiter.api.Assertions.assertTrue;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@SpringBootTest
@ActiveProfiles("ribbon-test")
@ActiveProfiles("test")
@EnableConfigurationProperties
@EnableFeignClients
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { RibbonTestConfig.class })
@ContextConfiguration(classes = { TestConfig.class })
class LoadBalancerBooksClientIntegrationTest {
@Autowired
@ -37,10 +53,22 @@ class LoadBalancerBooksClientIntegrationTest {
@Autowired
private BooksClient booksClient;
@Autowired
private LoadBalancerClientFactory clientFactory;
@BeforeEach
void setUp() throws IOException {
setupMockBooksResponse(mockBooksService);
setupMockBooksResponse(secondMockBooksService);
String serviceId = "books-service";
RoundRobinLoadBalancer loadBalancer = new RoundRobinLoadBalancer(ServiceInstanceListSuppliers
.toProvider(serviceId, instance(serviceId, "localhost", false), instance(serviceId, "localhost", true)),
serviceId, -1);
}
private static DefaultServiceInstance instance(String serviceId, String host, boolean secure) {
return new DefaultServiceInstance(serviceId, serviceId, host, 80, secure);
}
@Test
@ -62,4 +90,53 @@ class LoadBalancerBooksClientIntegrationTest {
new Book("Dune", "Frank Herbert"),
new Book("Foundation", "Isaac Asimov"))));
}
@Test
void loadbalancerWorks() throws IOException {
setupMockBooksResponse(mockBooksService);
setupMockBooksResponse(secondMockBooksService);
ReactiveLoadBalancer<ServiceInstance> reactiveLoadBalancer = this.clientFactory.getInstance("books-service",
ReactiveLoadBalancer.class, ServiceInstance.class);
then(reactiveLoadBalancer).isInstanceOf(RoundRobinLoadBalancer.class);
then(reactiveLoadBalancer).isInstanceOf(ReactorLoadBalancer.class);
ReactorLoadBalancer<ServiceInstance> loadBalancer = (ReactorLoadBalancer<ServiceInstance>) reactiveLoadBalancer;
for (int k = 0; k < 10; k++) {
booksClient.getBooks();
}
// order dependent on seedPosition -1 of RoundRobinLoadBalancer
List<String> hosts = Arrays.asList("localhost", "localhost");
assertLoadBalancer(loadBalancer, hosts);
mockBooksService.verify(
moreThan(0), getRequestedFor(WireMock.urlEqualTo("/books")));
secondMockBooksService.verify(
moreThan(0), getRequestedFor(WireMock.urlEqualTo("/books")));
}
private void assertLoadBalancer(ReactorLoadBalancer<ServiceInstance> loadBalancer, List<String> hosts) {
for (String host : hosts) {
Mono<Response<ServiceInstance>> source = loadBalancer.choose();
StepVerifier.create(source).consumeNextWith(response -> {
then(response).isNotNull();
then(response.hasServer()).isTrue();
ServiceInstance instance = response.getServer();
then(instance).isNotNull();
then(instance.getHost()).as("instance host is incorrect %s", host).isEqualTo(host);
if (host.contains("secure")) {
then(instance.isSecure()).isTrue();
}
else {
then(instance.isSecure()).isFalse();
}
}).verifyComplete();
}
}
}

View File

@ -0,0 +1,112 @@
package com.baeldung.spring.cloud.client;
import static org.assertj.core.api.BDDAssertions.then;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.loadbalancer.support.ServiceInstanceListSuppliers;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.github.tomakehurst.wiremock.WireMockServer;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@SpringBootTest
@EnableFeignClients
@EnableConfigurationProperties
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@ContextConfiguration(classes = { TestConfig.class })
class LoadBalancerIntegrationTest {
@Autowired
private LoadBalancerClientFactory clientFactory;
@Autowired
private BooksClient booksClient;
@Autowired
private WireMockServer mockBooksService;
@Autowired
private WireMockServer secondMockBooksService;
private void assertLoadBalancer(ReactorLoadBalancer<ServiceInstance> loadBalancer, List<String> hosts) {
for (String host : hosts) {
Mono<Response<ServiceInstance>> source = loadBalancer.choose();
StepVerifier.create(source).consumeNextWith(response -> {
then(response).isNotNull();
then(response.hasServer()).isTrue();
ServiceInstance instance = response.getServer();
then(instance).isNotNull();
then(instance.getHost()).as("instance host is incorrect %s", host).isEqualTo(host);
if (host.contains("secure")) {
then(instance.isSecure()).isTrue();
}
else {
then(instance.isSecure()).isFalse();
}
}).verifyComplete();
}
}
@Test
void staticConfigurationWorks() {
String serviceId = "test-book-service";
RoundRobinLoadBalancer loadBalancer = new RoundRobinLoadBalancer(ServiceInstanceListSuppliers
.toProvider(serviceId, instance(serviceId, "bookservice1", false), instance(serviceId, "bookservice2", false)),
serviceId, -1);
assertLoadBalancer(loadBalancer, Arrays.asList("bookservice1", "bookservice2"));
}
private static DefaultServiceInstance instance(String serviceId, String host, boolean secure) {
return new DefaultServiceInstance(serviceId, serviceId, host, 80, secure);
}
@EnableAutoConfiguration
@SpringBootConfiguration(proxyBeanMethods = false)
@LoadBalancerClients({ @LoadBalancerClient(name = "books-service", configuration = MyBooksServiceConfig.class) })
@EnableCaching
protected static class Config {
}
protected static class MyBooksServiceConfig {
@Bean
public RoundRobinLoadBalancer roundRobinContextLoadBalancer(LoadBalancerClientFactory clientFactory,
Environment env) {
String serviceId = LoadBalancerClientFactory.getName(env);
return new RoundRobinLoadBalancer(
clientFactory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class), serviceId, -1);
}
}
}

View File

@ -1,41 +0,0 @@
package com.baeldung.spring.cloud.client;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ActiveProfiles;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@TestConfiguration
@ActiveProfiles("ribbon-test")
public class RibbonTestConfig {
@Autowired
private WireMockServer mockBooksService;
@Autowired
private WireMockServer secondMockBooksService;
@Bean(initMethod = "start", destroyMethod = "stop")
public WireMockServer mockBooksService() {
return new WireMockServer(options().dynamicPort());
}
@Bean(name="secondMockBooksService", initMethod = "start", destroyMethod = "stop")
public WireMockServer secondBooksMockService() {
return new WireMockServer(options().dynamicPort());
}
@Bean
public ServerList<Server> ribbonServerList() {
return new StaticServerList<>(
new Server("localhost", mockBooksService.port()),
new Server("localhost", secondMockBooksService.port()));
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.cloud.client;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ActiveProfiles;
import com.github.tomakehurst.wiremock.WireMockServer;
@TestConfiguration
@ActiveProfiles("test")
public class TestConfig {
@Bean(initMethod = "start", destroyMethod = "stop")
public WireMockServer mockBooksService() {
return new WireMockServer(options().port(80));
}
@Bean(name="secondMockBooksService", initMethod = "start", destroyMethod = "stop")
public WireMockServer secondBooksMockService() {
return new WireMockServer(options().port(81));
}
}

View File

@ -11,7 +11,11 @@ public class WireMockConfig {
@Bean(initMethod = "start", destroyMethod = "stop")
public WireMockServer mockBooksService() {
return new WireMockServer(9561);
return new WireMockServer(80);
}
@Bean(initMethod = "start", destroyMethod = "stop")
public WireMockServer mockBooksService2() {
return new WireMockServer(81);
}
}

View File

@ -0,0 +1,19 @@
eureka:
client:
enabled: false
spring:
application:
name: books-service
cloud:
loadbalancer:
ribbon:
enabled: false
discovery:
client:
simple:
instances:
books-service[0]:
uri: http://localhost:80
books-service[1]:
uri: http://localhost:81

View File

@ -1,11 +1,19 @@
#book:
# service:
# url: http://localhost:9561
books-service:
ribbon:
listOfServers: http://localhost:9561
eureka:
client:
enabled: false
spring:
application:
name: books-service
cloud:
loadbalancer:
ribbon:
enabled: false
discovery:
client:
simple:
instances:
books-service[0]:
uri: http://localhost:80
books-service[1]:
uri: http://localhost:81