Skip to content

Commit 36efd16

Browse files
author
iverhun
authored
Merge pull request #1 from lvivJavaClub/feign-and-storage
Feign and storage patch
2 parents 986f52c + 633d801 commit 36efd16

File tree

15 files changed

+238
-2
lines changed

15 files changed

+238
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ Sandbox to play with spring cloud features
66
| Name | Description | Default port | Details |
77
|----------------------|---------------------------|--------------|----------------------------------------------------|
88
| Configuration server | Configuration server | 8888 | You should set an `JAVA_CLUB_SRC_HOME` variable which points to the folder where your java club sources are checked out. <br/>Configs URL example: http://localhost:8888/cloud/master |
9-
| Discovery server | Discovery server | 8761 | |
9+
| Discovery server | Discovery server | 8761 | Eureka server for services registration. |
1010

1111

1212
## Service
1313
| Name | Description | Default port | Details |
1414
|----------------------|-----------------------------|--------------|--------------------------------------------------|
1515
| Rating service | Rating Calculation Service | 8081 | |
1616
| Hackster service| Hackster Detection Service | 8082| |
17+
| Realtor service| Realtor Api Service | 8080| To call other services used Feign, RestTemplate |
18+
| Storage service| Storage of Apartment Records Service | 8091| H2 based service for ApartmentRecord data storage. |
1719

1820

1921
# Dev
@@ -30,6 +32,7 @@ mvn clean install
3032

3133
```
3234
## TODO Items
35+
- [ ] Check Feign Fallback ?
3336
- [x] Storage Service (persistance + eurika client)
3437
- [x] Rieltor Service
3538
- [x] All eurika clients add eurika server address to properties

config/rating-service.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
<module>rating-service</module>
1818
<module>hackster-service</module>
1919
<module>realtor-service</module>
20+
<module>storage-service</module>>
2021
</modules>
2122
</project>

realtor-service/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
<artifactId>lombok</artifactId>
4646
<version>1.16.12</version>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.cloud</groupId>
50+
<artifactId>spring-cloud-starter-feign</artifactId>
51+
</dependency>
4852
</dependencies>
4953

5054
<dependencyManagement>

realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@NoArgsConstructor
77
@AllArgsConstructor
88
@Data
9+
@ToString
910
public class ApartmentRecord {
1011
@NonNull
1112
private String location;
@@ -17,4 +18,6 @@ public class ApartmentRecord {
1718
private String phone;
1819
@NonNull
1920
private String realtorName;
21+
@NonNull
22+
private String mail;
2023
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.lohika.jclub;
2+
3+
import feign.Headers;
4+
import feign.RequestLine;
5+
6+
/**
7+
* Created by omuliarevych on 6/8/17.
8+
*/
9+
public interface ApartmentRecordClient {
10+
11+
@RequestLine("POST /apartmentRecords")
12+
@Headers("Content-Type: application/json")
13+
void storeApartment(ApartmentRecord apartmentRecord);
14+
}

realtor-service/src/main/java/com/lohika/jclub/RealtorController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class RealtorController {
2121
@Autowired
2222
private RestTemplate restTemplate;
2323

24+
@Autowired
25+
private RealtorService realtorService;
26+
2427
@PostMapping("/apartments")
2528
public void addApartment(@RequestBody ApartmentRecord apartmentRecord) {
2629
ResponseEntity<Boolean> isHackster =
@@ -29,6 +32,15 @@ public void addApartment(@RequestBody ApartmentRecord apartmentRecord) {
2932
log.info("Is hackster " + isHackster);
3033
}
3134

35+
@PostMapping("/storeApartments")
36+
public void storeApartment(@RequestBody ApartmentRecord apartmentRecord) {
37+
realtorService.storeApartment(apartmentRecord);
38+
/*ApartmentRecordClient apartmentRecordClient = Feign.builder().encoder(new JacksonEncoder())
39+
.decoder(new JacksonDecoder()).target(ApartmentRecordClient.class, "http://storage-service");
40+
apartmentRecordClient.storeApartment(apartmentRecord);*/
41+
log.info("Stored");
42+
}
43+
3244
@RequestMapping("/service-instances/{applicationName}")
3345
public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
3446
return this.discoveryClient.getInstances(applicationName);

realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package com.lohika.jclub;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
67
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
8+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
9+
import org.springframework.cloud.netflix.feign.FeignClient;
710
import org.springframework.context.annotation.Bean;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.web.bind.annotation.PostMapping;
813
import org.springframework.web.client.RestTemplate;
914

15+
@EnableFeignClients
1016
@EnableDiscoveryClient
1117
@SpringBootApplication
1218
public class RealtorServiceApplication {
1319

20+
/* @Bean
21+
public FeignApartmentFallback createFeignFallback() {
22+
return new FeignApartmentFallback();
23+
}*/
24+
1425
public static void main(String[] args) {
1526
SpringApplication.run(RealtorServiceApplication.class, args);
1627
}
@@ -21,3 +32,19 @@ public RestTemplate restTemplate() {
2132
return new RestTemplate();
2233
}
2334
}
35+
36+
@FeignClient(value = "storage-service", fallback = FeignApartmentFallback.class)
37+
interface RealtorService {
38+
@PostMapping("/apartmentRecords")
39+
void storeApartment(ApartmentRecord apartmentRecord);
40+
}
41+
42+
@Slf4j
43+
@Component
44+
class FeignApartmentFallback implements RealtorService {
45+
46+
@Override
47+
public void storeApartment(ApartmentRecord apartmentRecord) {
48+
log.error("Error: {}", apartmentRecord);
49+
}
50+
}

storage-service/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

storage-service/pom.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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</groupId>
7+
<artifactId>storage-service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>storage-service</name>
12+
<description>Demo project 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.SR1</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-data-jpa</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-data-rest</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.h2database</groupId>
44+
<artifactId>h2</artifactId>
45+
<scope>runtime</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.projectlombok</groupId>
49+
<artifactId>lombok</artifactId>
50+
<optional>true</optional>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<dependencyManagement>
60+
<dependencies>
61+
<dependency>
62+
<groupId>org.springframework.cloud</groupId>
63+
<artifactId>spring-cloud-dependencies</artifactId>
64+
<version>${spring-cloud.version}</version>
65+
<type>pom</type>
66+
<scope>import</scope>
67+
</dependency>
68+
</dependencies>
69+
</dependencyManagement>
70+
71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-maven-plugin</artifactId>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
80+
81+
</project>

0 commit comments

Comments
 (0)