initial commit
This commit is contained in:
parent
85db5610ea
commit
f3d390b684
@ -19,6 +19,7 @@
|
||||
<module>spring-cloud-eureka-server</module>
|
||||
<module>spring-cloud-eureka-client</module>
|
||||
<module>spring-cloud-eureka-feign-client</module>
|
||||
<module>spring-cloud-eureka-feign-client-integration-test</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
@ -0,0 +1,86 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-cloud-eureka-feign-client-integration-test</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>spring-cloud-eureka-feign-client-integration-test</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring Cloud Eureka - Feign Client Integration Tests</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-eureka</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit</groupId>
|
||||
<artifactId>junit-bom</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-parent</artifactId>
|
||||
<version>${spring-cloud-dependencies.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.cloud</groupId>-->
|
||||
<!-- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.tomakehurst</groupId>
|
||||
<artifactId>wiremock</artifactId>
|
||||
<version>2.27.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers</artifactId>
|
||||
<version>1.14.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>4.0.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.cloud;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@EnableFeignClients
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
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(value="simple-books-client", url="${book.service.url}")
|
||||
@FeignClient("books-client")
|
||||
public interface BooksClient {
|
||||
|
||||
@RequestMapping("/books")
|
||||
List<Book> getBooks();
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.cloud.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Book {
|
||||
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#book:
|
||||
# service:
|
||||
# url: http://book.service.com
|
||||
|
||||
books-client:
|
||||
ribbon:
|
||||
eureka:
|
||||
enabled: false
|
||||
listOfServers: http://book.service.com
|
||||
ServerListRefreshInterval: 15000
|
||||
MaxAutoRetries: 3
|
||||
retryableStatusCodes: 503, 408
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.spring.cloud.client;
|
||||
|
||||
import com.baeldung.spring.cloud.Application;
|
||||
import com.baeldung.spring.cloud.model.Book;
|
||||
import com.netflix.discovery.EurekaClient;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.context.annotation.Lazy;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@ActiveProfiles("test")
|
||||
@EnableConfigurationProperties
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ContextConfiguration(classes = {MockBookServiceConfig.class}, initializers = {EurekaContainerConfig.Initializer.class})
|
||||
class BooksClientIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private BooksClient booksClient;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private EurekaClient eurekaClient;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
await().atMost(60, SECONDS).until(() -> eurekaClient.getApplications().size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBooks_thenListBooksSizeGreaterThanZero() throws InterruptedException {
|
||||
List<Book> books = booksClient.getBooks();
|
||||
|
||||
assertTrue(books.size() == 1);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.spring.cloud.client;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.lifecycle.Startables;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class EurekaContainerConfig {
|
||||
|
||||
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
public static GenericContainer eurekaServer
|
||||
= new GenericContainer("springcloud/eureka")
|
||||
.withExposedPorts(8761);
|
||||
|
||||
@Override
|
||||
public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) {
|
||||
|
||||
Startables.deepStart(Stream.of(eurekaServer)).join();
|
||||
|
||||
TestPropertyValues
|
||||
.of("eureka.client.serviceUrl.defaultZone=http://localhost:" + eurekaServer.getFirstMappedPort().toString() + "/eureka")
|
||||
.applyTo(configurableApplicationContext);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.baeldung.spring.cloud.client;
|
||||
|
||||
import com.baeldung.spring.cloud.model.Book;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@EnableConfigurationProperties
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = {WireMockConfig.class})
|
||||
class GreetingClientIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WireMockServer wireMockServer;
|
||||
|
||||
@Autowired
|
||||
private BooksClient booksClient;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
wireMockServer.stubFor(WireMock.get(WireMock.urlEqualTo("/books"))
|
||||
.willReturn(WireMock.aResponse()
|
||||
.withStatus(HttpStatus.OK.value())
|
||||
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
|
||||
.withBody(StreamUtils.copyToString(getClass().getClassLoader().getResourceAsStream("payload/get-books-response.json"), Charset.defaultCharset()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBooks_thenListBooksSizeGreaterThanZero() {
|
||||
List<Book> books = booksClient.getBooks();
|
||||
|
||||
assertFalse(books.isEmpty());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.spring.cloud.client;
|
||||
|
||||
import com.baeldung.spring.cloud.model.Book;
|
||||
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.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.cloud.netflix.ribbon.StaticServerList;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
public class MockBookServiceConfig {
|
||||
|
||||
@RequestMapping("/books")
|
||||
public List<Book> getBooks() {
|
||||
return Collections.singletonList(new Book("some title", "some author"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
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 static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
||||
|
||||
@TestConfiguration
|
||||
public class WireMockConfig {
|
||||
|
||||
@Autowired
|
||||
private WireMockServer wireMockServer;
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public WireMockServer booksMockService() {
|
||||
return new WireMockServer(options().dynamicPort());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServerList<Server> ribbonServerList() {
|
||||
return new StaticServerList<>(new Server("localhost", wireMockServer.port()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#book:
|
||||
# service:
|
||||
# url: http://localhost:9561
|
||||
|
||||
#books-client:
|
||||
# ribbon:
|
||||
# listOfServers: http://localhost:9561
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: books-client
|
||||
|
||||
server:
|
||||
port: 0
|
||||
|
||||
eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
|
||||
instance:
|
||||
preferIpAddress: true
|
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"title": "Dune",
|
||||
"author": "Frank Herbert"
|
||||
},
|
||||
{
|
||||
"title": "Foundation",
|
||||
"author": "Isaac Asimov"
|
||||
}
|
||||
]
|
Loading…
x
Reference in New Issue
Block a user