From 7068f6b791ff840526357535a05c408236faec57 Mon Sep 17 00:00:00 2001 From: alex-muliarevych Date: Thu, 8 Jun 2017 10:42:58 +0300 Subject: [PATCH 1/3] Added storage service, add save via feign. --- config/rating-service.properties | 1 - pom.xml | 1 + realtor-service/pom.xml | 15 +++++++++++ .../com/lohika/jclub/ApartmentRecord.java | 2 ++ .../lohika/jclub/ApartmentRecordClient.java | 14 +++++++++++ .../com/lohika/jclub/RealtorController.java | 12 +++++++++ .../jclub/RealtorServiceApplication.java | 10 ++++++++ .../com/lohika/jclub/ApartmentRecord.java | 25 +++++++++++++++++++ .../com/lohika/jclub/ApartmentRepository.java | 11 ++++++++ 9 files changed, 90 insertions(+), 1 deletion(-) delete mode 100644 config/rating-service.properties create mode 100644 realtor-service/src/main/java/com/lohika/jclub/ApartmentRecordClient.java create mode 100644 storage-service/src/main/java/com/lohika/jclub/ApartmentRecord.java create mode 100644 storage-service/src/main/java/com/lohika/jclub/ApartmentRepository.java diff --git a/config/rating-service.properties b/config/rating-service.properties deleted file mode 100644 index d2f966e..0000000 --- a/config/rating-service.properties +++ /dev/null @@ -1 +0,0 @@ -rate=15 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0fe1526..9d39839 100644 --- a/pom.xml +++ b/pom.xml @@ -17,5 +17,6 @@ rating-service hackster-service realtor-service + storage-service> diff --git a/realtor-service/pom.xml b/realtor-service/pom.xml index 6ef2bc4..6f323d6 100644 --- a/realtor-service/pom.xml +++ b/realtor-service/pom.xml @@ -45,6 +45,21 @@ lombok 1.16.12 + + io.github.openfeign + feign-okhttp + 9.3.1 + + + io.github.openfeign + feign-gson + 9.3.1 + + + io.github.openfeign + feign-slf4j + 9.3.1 + diff --git a/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java b/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java index 675e911..5b9762a 100644 --- a/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java +++ b/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java @@ -17,4 +17,6 @@ public class ApartmentRecord { private String phone; @NonNull private String realtorName; + @NonNull + private String mail; } diff --git a/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecordClient.java b/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecordClient.java new file mode 100644 index 0000000..6802af8 --- /dev/null +++ b/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecordClient.java @@ -0,0 +1,14 @@ +package com.lohika.jclub; + +import feign.Headers; +import feign.RequestLine; + +/** + * Created by omuliarevych on 6/8/17. + */ +public interface ApartmentRecordClient { + + @RequestLine("POST /apartmentRecords") + @Headers("Content-Type: application/json") + void storeApartment(ApartmentRecord apartmentRecord); +} diff --git a/realtor-service/src/main/java/com/lohika/jclub/RealtorController.java b/realtor-service/src/main/java/com/lohika/jclub/RealtorController.java index 8911fe0..b9a350b 100644 --- a/realtor-service/src/main/java/com/lohika/jclub/RealtorController.java +++ b/realtor-service/src/main/java/com/lohika/jclub/RealtorController.java @@ -21,6 +21,9 @@ public class RealtorController { @Autowired private RestTemplate restTemplate; + @Autowired + private RealtorService realtorService; + @PostMapping("/apartments") public void addApartment(@RequestBody ApartmentRecord apartmentRecord) { ResponseEntity isHackster = @@ -29,6 +32,15 @@ public void addApartment(@RequestBody ApartmentRecord apartmentRecord) { log.info("Is hackster " + isHackster); } + @PostMapping("/storeApartments") + public void storeApartment(@RequestBody ApartmentRecord apartmentRecord) { + realtorService.storeApartment(apartmentRecord); + /*ApartmentRecordClient apartmentRecordClient = Feign.builder().encoder(new JacksonEncoder()) + .decoder(new JacksonDecoder()).target(ApartmentRecordClient.class, "http://storage-service"); + apartmentRecordClient.storeApartment(apartmentRecord);*/ + log.info("Stored"); + } + @RequestMapping("/service-instances/{applicationName}") public List serviceInstancesByApplicationName(@PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); diff --git a/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java b/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java index 06d9573..c83df13 100644 --- a/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java +++ b/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java @@ -4,9 +4,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.client.RestTemplate; +@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class RealtorServiceApplication { @@ -21,3 +25,9 @@ public RestTemplate restTemplate() { return new RestTemplate(); } } + +@FeignClient("storage-service") +interface RealtorService { + @PostMapping("/apartmentRecords") + void storeApartment(ApartmentRecord apartmentRecord); +} diff --git a/storage-service/src/main/java/com/lohika/jclub/ApartmentRecord.java b/storage-service/src/main/java/com/lohika/jclub/ApartmentRecord.java new file mode 100644 index 0000000..f6b539b --- /dev/null +++ b/storage-service/src/main/java/com/lohika/jclub/ApartmentRecord.java @@ -0,0 +1,25 @@ +package com.lohika.jclub; + +import lombok.*; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@NoArgsConstructor +@AllArgsConstructor +@Data +@Entity +public class ApartmentRecord { + @NonNull + private String location; + @NonNull + private double price; + @NonNull + private double sqft; + @NonNull + private String phone; + @NonNull + private String realtorName; + @Id + private String mail; +} diff --git a/storage-service/src/main/java/com/lohika/jclub/ApartmentRepository.java b/storage-service/src/main/java/com/lohika/jclub/ApartmentRepository.java new file mode 100644 index 0000000..0d8e2f0 --- /dev/null +++ b/storage-service/src/main/java/com/lohika/jclub/ApartmentRepository.java @@ -0,0 +1,11 @@ +package com.lohika.jclub; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * Created by omuliarevych on 6/8/17. + */ +@RepositoryRestResource +public interface ApartmentRepository extends CrudRepository { +} From 33da37bb760da93dbf88ccc4549e09da4db0ac5a Mon Sep 17 00:00:00 2001 From: alex-muliarevych Date: Thu, 8 Jun 2017 11:09:46 +0300 Subject: [PATCH 2/3] Added FeignFallback for RealtorService. --- realtor-service/pom.xml | 15 +--- .../com/lohika/jclub/ApartmentRecord.java | 1 + .../jclub/RealtorServiceApplication.java | 19 ++++- storage-service/.gitignore | 24 ++++++ storage-service/pom.xml | 81 +++++++++++++++++++ .../jclub/StorageServiceApplication.java | 14 ++++ .../src/main/resources/application.properties | 2 + .../jclub/StorageServiceApplicationTests.java | 16 ++++ 8 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 storage-service/.gitignore create mode 100644 storage-service/pom.xml create mode 100644 storage-service/src/main/java/com/lohika/jclub/StorageServiceApplication.java create mode 100644 storage-service/src/main/resources/application.properties create mode 100644 storage-service/src/test/java/com/lohika/jclub/StorageServiceApplicationTests.java diff --git a/realtor-service/pom.xml b/realtor-service/pom.xml index 6f323d6..bd0f893 100644 --- a/realtor-service/pom.xml +++ b/realtor-service/pom.xml @@ -46,19 +46,8 @@ 1.16.12 - io.github.openfeign - feign-okhttp - 9.3.1 - - - io.github.openfeign - feign-gson - 9.3.1 - - - io.github.openfeign - feign-slf4j - 9.3.1 + org.springframework.cloud + spring-cloud-starter-feign diff --git a/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java b/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java index 5b9762a..e6eacdf 100644 --- a/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java +++ b/realtor-service/src/main/java/com/lohika/jclub/ApartmentRecord.java @@ -6,6 +6,7 @@ @NoArgsConstructor @AllArgsConstructor @Data +@ToString public class ApartmentRecord { @NonNull private String location; diff --git a/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java b/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java index c83df13..6608802 100644 --- a/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java +++ b/realtor-service/src/main/java/com/lohika/jclub/RealtorServiceApplication.java @@ -1,5 +1,6 @@ package com.lohika.jclub; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @@ -7,6 +8,7 @@ import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.client.RestTemplate; @@ -15,6 +17,11 @@ @SpringBootApplication public class RealtorServiceApplication { +/* @Bean + public FeignApartmentFallback createFeignFallback() { + return new FeignApartmentFallback(); + }*/ + public static void main(String[] args) { SpringApplication.run(RealtorServiceApplication.class, args); } @@ -26,8 +33,18 @@ public RestTemplate restTemplate() { } } -@FeignClient("storage-service") +@FeignClient(value = "storage-service", fallback = FeignApartmentFallback.class) interface RealtorService { @PostMapping("/apartmentRecords") void storeApartment(ApartmentRecord apartmentRecord); } + +@Slf4j +@Component +class FeignApartmentFallback implements RealtorService { + + @Override + public void storeApartment(ApartmentRecord apartmentRecord) { + log.error("Error: {}", apartmentRecord); + } +} diff --git a/storage-service/.gitignore b/storage-service/.gitignore new file mode 100644 index 0000000..2af7cef --- /dev/null +++ b/storage-service/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/storage-service/pom.xml b/storage-service/pom.xml new file mode 100644 index 0000000..d871792 --- /dev/null +++ b/storage-service/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.lohika.jclub + storage-service + 0.0.1-SNAPSHOT + jar + + storage-service + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + Dalston.SR1 + + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-data-rest + + + + com.h2database + h2 + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/storage-service/src/main/java/com/lohika/jclub/StorageServiceApplication.java b/storage-service/src/main/java/com/lohika/jclub/StorageServiceApplication.java new file mode 100644 index 0000000..2dc477a --- /dev/null +++ b/storage-service/src/main/java/com/lohika/jclub/StorageServiceApplication.java @@ -0,0 +1,14 @@ +package com.lohika.jclub; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@EnableDiscoveryClient +@SpringBootApplication +public class StorageServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(StorageServiceApplication.class, args); + } +} diff --git a/storage-service/src/main/resources/application.properties b/storage-service/src/main/resources/application.properties new file mode 100644 index 0000000..fcc5530 --- /dev/null +++ b/storage-service/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=storage-service +server.port=8091 \ No newline at end of file diff --git a/storage-service/src/test/java/com/lohika/jclub/StorageServiceApplicationTests.java b/storage-service/src/test/java/com/lohika/jclub/StorageServiceApplicationTests.java new file mode 100644 index 0000000..8f1a234 --- /dev/null +++ b/storage-service/src/test/java/com/lohika/jclub/StorageServiceApplicationTests.java @@ -0,0 +1,16 @@ +package com.lohika.jclub; + +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 StorageServiceApplicationTests { + + @Test + public void contextLoads() { + } + +} From 234c3cc85db71710d28e63c2a7a4a2e71e846ee9 Mon Sep 17 00:00:00 2001 From: alex-muliarevych Date: Thu, 8 Jun 2017 11:36:49 +0300 Subject: [PATCH 3/3] update readme --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 06cd369..894b506 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Sandbox to play with spring cloud features | Name | Description | Default port | Details | |----------------------|---------------------------|--------------|----------------------------------------------------| | 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.
Configs URL example: http://localhost:8888/cloud/master | -| Discovery server | Discovery server | 8761 | | +| Discovery server | Discovery server | 8761 | Eureka server for services registration. | ## Service @@ -14,6 +14,8 @@ Sandbox to play with spring cloud features |----------------------|-----------------------------|--------------|--------------------------------------------------| | Rating service | Rating Calculation Service | 8081 | | | Hackster service| Hackster Detection Service | 8082| | +| 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. | # Dev @@ -30,8 +32,7 @@ mvn clean install ``` ## TODO Items -- [ ] Storage Service (persistance + eurika client) -- [ ] Rieltor Service +- [ ] Check Feign Fallback ? - [ ] All eurika clients add eurika server address to properties - [ ] Client service for search - [ ] Zuul like a proxy API gateway