diff --git a/README.md b/README.md index fe0c483..a93821d 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,6 +32,7 @@ mvn clean install ``` ## TODO Items +- [ ] Check Feign Fallback ? - [x] Storage Service (persistance + eurika client) - [x] Rieltor Service - [x] All eurika clients add eurika server address to properties 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..bd0f893 100644 --- a/realtor-service/pom.xml +++ b/realtor-service/pom.xml @@ -45,6 +45,10 @@ lombok 1.16.12 + + 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 675e911..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; @@ -17,4 +18,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..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,16 +1,27 @@ 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; 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.stereotype.Component; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.client.RestTemplate; +@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class RealtorServiceApplication { +/* @Bean + public FeignApartmentFallback createFeignFallback() { + return new FeignApartmentFallback(); + }*/ + public static void main(String[] args) { SpringApplication.run(RealtorServiceApplication.class, args); } @@ -21,3 +32,19 @@ public RestTemplate restTemplate() { return new RestTemplate(); } } + +@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/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 { +} 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() { + } + +}