Skip to content

Client service #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 19, 2017
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Sandbox to play with spring cloud features
|----------------------|-----------------------------|--------------|--------------------------------------------------|
| Rating service | Rating Calculation Service | 8081 | |
| Hackster service| Hackster Detection Service | 8082| |
| Client service| Client Service | 8083| |
| Realtor service| Realtor Api Service | 8080| To call other services used Feign, RestTemplate |
| Storage service| Storage of Apartment Records Service | 8091| H2 based service for ApartmentRecord data storage. |
| API gateway service| Zull API Gateway Service | 8090| |
Expand Down
4 changes: 3 additions & 1 deletion api-gateway-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ zuul:
realtor:
path: /realtor-service/**
serviceId: realtor-service

client:
path: /client-service/**
serviceId: client-service
ignored-services: '*'
111 changes: 111 additions & 0 deletions client-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.lohika.jclub.client</groupId>
<artifactId>client-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Client service</name>
<description>Client service for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.lohika.jclub.storage.client</groupId>
<artifactId>storage-service-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>client-service</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lohika.jclub.client;

import com.lohika.jclub.storage.client.Apartment;
import com.lohika.jclub.storage.client.StorageServiceClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.PagedResources;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class ClientController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would like to see some integration tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will create tests in next change-set using test-containers.


@Autowired
private StorageServiceClient storageServiceClient;

@GetMapping("/apartments")
public PagedResources<Apartment> getApartments() {
PagedResources<Apartment> list = storageServiceClient.list();
return new PagedResources<>(list.getContent(), list.getMetadata());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lohika.jclub.client;

import com.lohika.jclub.storage.client.EnableStorageServiceClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableStorageServiceClient
public class ClientServiceApplication {

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

}
3 changes: 3 additions & 0 deletions client-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.application.name=client-service
server.port=8083
eureka.instance.prefer-ip-address=true
7 changes: 7 additions & 0 deletions client-service/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_________ __ _
/ ____/ (_)__ ____ / /_ ________ ______ __(_)_______
/ / / / / _ \/ __ \/ __/ / ___/ _ \/ ___/ | / / / ___/ _ \
/ /___/ / / __/ / / / /_ (__ ) __/ / | |/ / / /__/ __/
\____/_/_/\___/_/ /_/\__/ /____/\___/_/ |___/_/\___/\___/

v.${application.version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.lohika.jclub.client;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ClientServiceApplicationTests {

@Test
public void contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.lohika.jclub.client;

import com.lohika.jclub.storage.client.Apartment;
import com.lohika.jclub.storage.client.StorageServiceClient;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.LogMessageWaitStrategy;

import java.time.Duration;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ClientServiceApplication.class)
@ContextConfiguration(initializers = ClientServiceTest.Initializer.class)
@AutoConfigureMockMvc
public class ClientServiceTest {

@ClassRule
public static GenericContainer discoveryService = new GenericContainer("discovery-service:latest")
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started EurekaServerApplication in.*\\s"))
.withExposedPorts(8761);

@Rule
public GenericContainer storageService = new GenericContainer("storage-service:latest")
.withExposedPorts(8091)
.withEnv("eureka.client.serviceUrl.defaultZone", "http://" + discoveryService.getContainerIpAddress() + ":" + discoveryService.getMappedPort(8761) + "/eureka")
.withEnv("eureka.instance.preferIpAddress", "true")
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*DiscoveryClient_STORAGE-SERVICE.*registration status: 204.*\\s"));

@Autowired
private StorageServiceClient storageServiceClient;

@Autowired
private MockMvc mockMvc;

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
"eureka.client.serviceUrl.defaultZone=http://" + discoveryService.getContainerIpAddress() +
":" + discoveryService.getMappedPort(8761) + "/eureka",
"storage-service.ribbon.servers=http://");
}
}

@Test
public void testGetApartments() throws Exception {
// Given
Apartment lviv = storageServiceClient.create(
Apartment.builder()
.location("LVIV")
.mail("asfafs@asf.com")
.phone("02510505001")
.price(5225)
.realtorName("Bariga")
.sqft(55)
.build());

// When
mockMvc.perform(MockMvcRequestBuilders.get("/apartments"))
.andDo(print());

// Then
//TODO storageServiceClient does not creating the records.
// fallback is working with 0 paging
}
}
1 change: 1 addition & 0 deletions client-service/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feign.hystrix.enabled=true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to client-service/src/main/resources/application.properties

26 changes: 26 additions & 0 deletions discovery-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>discovery-service</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<module>storage-service</module>
<module>storage-service-client</module>
<module>api-gateway-service</module>
<module>client-service</module>
</modules>
</project>
</project>
3 changes: 0 additions & 3 deletions rating-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion realtor-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>jar</packaging>

<name>Realtor service</name>
<description>Demo project for Spring Boot</description>
<description>Realtor for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.List;

@Slf4j
@RestController()
@RestController
public class RealtorController {

@Autowired
Expand Down
2 changes: 1 addition & 1 deletion realtor-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
spring.application.name=realtor-service
feign.hystrix.enabled=true
feign.hystrix.enabled=true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use \n in end of each file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, to separate different types of configuration and keep clean code, but lets @banadiga answer this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will affect next changes. In a case when we add a new property to the end of the file, we will get a diff with 2 line (but we changed only one.)