Skip to content

Commit e8485b0

Browse files
authored
Merge pull request #12 from lvivJavaClub/client-service
Client service
2 parents da1b0a5 + c492f40 commit e8485b0

File tree

16 files changed

+300
-8
lines changed

16 files changed

+300
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Sandbox to play with spring cloud features
1414
|----------------------|-----------------------------|--------------|--------------------------------------------------|
1515
| Rating service | Rating Calculation Service | 8081 | |
1616
| Hackster service| Hackster Detection Service | 8082| |
17+
| Client service| Client Service | 8083| |
1718
| Realtor service| Realtor Api Service | 8080| To call other services used Feign, RestTemplate |
1819
| Storage service| Storage of Apartment Records Service | 8091| H2 based service for ApartmentRecord data storage. |
1920
| API gateway service| Zull API Gateway Service | 8090| |

api-gateway-service/src/main/resources/application.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ zuul:
55
realtor:
66
path: /realtor-service/**
77
serviceId: realtor-service
8-
8+
client:
9+
path: /client-service/**
10+
serviceId: client-service
911
ignored-services: '*'

client-service/pom.xml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.lohika.jclub.client</groupId>
7+
<artifactId>client-service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Client service</name>
12+
<description>Client service for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-starter-eureka</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<version>1.16.12</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.cloud</groupId>
50+
<artifactId>spring-cloud-starter-feign</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.lohika.jclub.storage.client</groupId>
54+
<artifactId>storage-service-client</artifactId>
55+
<version>0.0.1-SNAPSHOT</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.testcontainers</groupId>
59+
<artifactId>testcontainers</artifactId>
60+
<version>1.3.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<dependencyManagement>
66+
<dependencies>
67+
<dependency>
68+
<groupId>org.springframework.cloud</groupId>
69+
<artifactId>spring-cloud-dependencies</artifactId>
70+
<version>${spring-cloud.version}</version>
71+
<type>pom</type>
72+
<scope>import</scope>
73+
</dependency>
74+
</dependencies>
75+
</dependencyManagement>
76+
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.springframework.boot</groupId>
81+
<artifactId>spring-boot-maven-plugin</artifactId>
82+
</plugin>
83+
<plugin>
84+
<groupId>com.spotify</groupId>
85+
<artifactId>docker-maven-plugin</artifactId>
86+
<version>1.0.0</version>
87+
<executions>
88+
<execution>
89+
<id>build-image</id>
90+
<phase>install</phase>
91+
<goals>
92+
<goal>build</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
<configuration>
97+
<imageName>client-service</imageName>
98+
<baseImage>java</baseImage>
99+
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
100+
<resources>
101+
<resource>
102+
<targetPath>/</targetPath>
103+
<directory>${project.build.directory}</directory>
104+
<include>${project.build.finalName}.jar</include>
105+
</resource>
106+
</resources>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lohika.jclub.client;
2+
3+
import com.lohika.jclub.storage.client.Apartment;
4+
import com.lohika.jclub.storage.client.StorageServiceClient;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.hateoas.PagedResources;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import lombok.extern.slf4j.Slf4j;
12+
13+
@Slf4j
14+
@RestController
15+
public class ClientController {
16+
17+
@Autowired
18+
private StorageServiceClient storageServiceClient;
19+
20+
@GetMapping("/apartments")
21+
public PagedResources<Apartment> getApartments() {
22+
PagedResources<Apartment> list = storageServiceClient.list();
23+
return new PagedResources<>(list.getContent(), list.getMetadata());
24+
}
25+
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lohika.jclub.client;
2+
3+
import com.lohika.jclub.storage.client.EnableStorageServiceClient;
4+
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
@SpringBootApplication
12+
@EnableStorageServiceClient
13+
public class ClientServiceApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(ClientServiceApplication.class, args);
17+
}
18+
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.application.name=client-service
2+
server.port=8083
3+
eureka.instance.prefer-ip-address=true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_________ __ _
2+
/ ____/ (_)__ ____ / /_ ________ ______ __(_)_______
3+
/ / / / / _ \/ __ \/ __/ / ___/ _ \/ ___/ | / / / ___/ _ \
4+
/ /___/ / / __/ / / / /_ (__ ) __/ / | |/ / / /__/ __/
5+
\____/_/_/\___/_/ /_/\__/ /____/\___/_/ |___/_/\___/\___/
6+
7+
v.${application.version}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lohika.jclub.client;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class ClientServiceApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.lohika.jclub.client;
2+
3+
import com.lohika.jclub.storage.client.Apartment;
4+
import com.lohika.jclub.storage.client.StorageServiceClient;
5+
6+
import org.junit.ClassRule;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.boot.test.util.EnvironmentTestUtils;
14+
import org.springframework.context.ApplicationContextInitializer;
15+
import org.springframework.context.ConfigurableApplicationContext;
16+
import org.springframework.test.context.ContextConfiguration;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
import org.springframework.test.web.servlet.MockMvc;
19+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
22+
23+
import java.time.Duration;
24+
25+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
26+
27+
@RunWith(SpringRunner.class)
28+
@SpringBootTest(classes = ClientServiceApplication.class)
29+
@ContextConfiguration(initializers = ClientServiceTest.Initializer.class)
30+
@AutoConfigureMockMvc
31+
public class ClientServiceTest {
32+
33+
@ClassRule
34+
public static GenericContainer discoveryService = new GenericContainer("discovery-service:latest")
35+
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started EurekaServerApplication in.*\\s"))
36+
.withExposedPorts(8761);
37+
38+
@Rule
39+
public GenericContainer storageService = new GenericContainer("storage-service:latest")
40+
.withExposedPorts(8091)
41+
.withEnv("eureka.client.serviceUrl.defaultZone", "http://" + discoveryService.getContainerIpAddress() + ":" + discoveryService.getMappedPort(8761) + "/eureka")
42+
.withEnv("eureka.instance.preferIpAddress", "true")
43+
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*DiscoveryClient_STORAGE-SERVICE.*registration status: 204.*\\s"));
44+
45+
@Autowired
46+
private StorageServiceClient storageServiceClient;
47+
48+
@Autowired
49+
private MockMvc mockMvc;
50+
51+
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
52+
@Override
53+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
54+
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
55+
"eureka.client.serviceUrl.defaultZone=http://" + discoveryService.getContainerIpAddress() +
56+
":" + discoveryService.getMappedPort(8761) + "/eureka",
57+
"storage-service.ribbon.servers=http://");
58+
}
59+
}
60+
61+
@Test
62+
public void testGetApartments() throws Exception {
63+
// Given
64+
Apartment lviv = storageServiceClient.create(
65+
Apartment.builder()
66+
.location("LVIV")
67+
.mail("asfafs@asf.com")
68+
.phone("02510505001")
69+
.price(5225)
70+
.realtorName("Bariga")
71+
.sqft(55)
72+
.build());
73+
74+
// When
75+
mockMvc.perform(MockMvcRequestBuilders.get("/apartments"))
76+
.andDo(print());
77+
78+
// Then
79+
//TODO storageServiceClient does not creating the records.
80+
// fallback is working with 0 paging
81+
}
82+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feign.hystrix.enabled=true

0 commit comments

Comments
 (0)