Skip to content

Commit 4d72cff

Browse files
authored
Merge pull request #8 from lvivJavaClub/RF-Rating-service
RF Rating service
2 parents 34e648c + 3672a4d commit 4d72cff

26 files changed

+549
-48
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>config-server</module>
1616
<module>discovery-server</module>
1717
<module>rating-service</module>
18+
<module>rating-service-client</module>
1819
<module>hackster-service</module>
1920
<module>realtor-service</module>
2021
<module>storage-service</module>

rating-service-client/pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.rating.client</groupId>
7+
<artifactId>rating-service-client</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Rating service client</name>
12+
<description>Client for rating-service</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.4.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.cloud</groupId>
35+
<artifactId>spring-cloud-starter-feign</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-hateoas</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.netflix.hystrix</groupId>
43+
<artifactId>hystrix-javanica</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.projectlombok</groupId>
48+
<artifactId>lombok</artifactId>
49+
<optional>true</optional>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.lohika.jclub</groupId>
53+
<artifactId>discovery-server</artifactId>
54+
<version>0.0.1-SNAPSHOT</version>
55+
<scope>test</scope>
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+
<dependency>
64+
<groupId>com.lohika.jclub.rating.service</groupId>
65+
<artifactId>rating-service</artifactId>
66+
<version>0.0.1-SNAPSHOT</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-test</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
</dependencies>
75+
76+
<dependencyManagement>
77+
<dependencies>
78+
<dependency>
79+
<groupId>org.springframework.cloud</groupId>
80+
<artifactId>spring-cloud-dependencies</artifactId>
81+
<version>${spring-cloud.version}</version>
82+
<type>pom</type>
83+
<scope>import</scope>
84+
</dependency>
85+
</dependencies>
86+
</dependencyManagement>
87+
88+
</project>

rating-service/src/main/java/com/lohika/jclub/Apartment.java renamed to rating-service-client/src/main/java/com/lohika/jclub/rating/client/Apartment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package com.lohika.jclub;
1+
package com.lohika.jclub.rating.client;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
35
import lombok.Data;
46
import lombok.NonNull;
57

6-
/**
7-
* @author Andriy Levchenko
8-
*/
98
@Data
9+
@Builder
10+
@AllArgsConstructor
1011
public class Apartment {
1112
@NonNull
1213
private String location;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
4+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
5+
import org.springframework.context.annotation.Import;
6+
import org.springframework.hateoas.config.EnableHypermediaSupport;
7+
8+
import java.lang.annotation.Documented;
9+
import java.lang.annotation.ElementType;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
@Target(ElementType.TYPE)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
18+
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
19+
@EnableFeignClients(clients = {RatingServiceClient.class})
20+
@Import({FeignMappingDefaultConfiguration.class, RatingServiceClientConfiguration.class})
21+
@EnableDiscoveryClient
22+
public @interface EnableRatingServiceClient {
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import feign.Feign;
4+
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
6+
import org.springframework.boot.autoconfigure.web.WebMvcRegistrations;
7+
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;
8+
import org.springframework.cloud.netflix.feign.FeignClient;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.core.annotation.AnnotationUtils;
12+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
13+
14+
/**
15+
* https://github.com/spring-cloud/spring-cloud-netflix/issues/466
16+
*/
17+
@Configuration
18+
@ConditionalOnClass({Feign.class})
19+
public class FeignMappingDefaultConfiguration {
20+
@Bean
21+
public WebMvcRegistrations feignWebRegistrations() {
22+
return new WebMvcRegistrationsAdapter() {
23+
@Override
24+
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
25+
return new FeignFilterRequestMappingHandlerMapping();
26+
}
27+
};
28+
}
29+
30+
private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
31+
@Override
32+
protected boolean isHandler(Class<?> beanType) {
33+
return super.isHandler(beanType) && (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null);
34+
}
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import lombok.ToString;
8+
9+
import java.math.BigDecimal;
10+
11+
@Builder(toBuilder = true)
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@Data
15+
@ToString
16+
public class Rating {
17+
private BigDecimal rating;
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
7+
@FeignClient(
8+
value = "rating-service",
9+
url = "${rating-service.ribbon.servers:}",
10+
decode404 = true,
11+
fallback = RatingServiceClientFallback.class)
12+
@RequestMapping("/rating")
13+
public interface RatingServiceClient {
14+
15+
@PostMapping
16+
Rating getRating(Apartment apartment);
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class RatingServiceClientConfiguration {
8+
9+
@Bean
10+
public RatingServiceClientFallback RatingServiceClientFallback() {
11+
return new RatingServiceClientFallback();
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
@Slf4j
6+
public class RatingServiceClientFallback implements RatingServiceClient {
7+
8+
@Override
9+
public Rating getRating(Apartment apartment) {
10+
log.error("Can get rating");
11+
return null;
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lohika.jclub.rating.client;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest(classes = RatingServiceClientTestApplication.class)
11+
public class RatingServiceClientApplicationTests {
12+
13+
@Autowired
14+
private RatingServiceClientFallback ratingServiceClientFallback;
15+
16+
@Test
17+
public void contextLoads() {
18+
}
19+
}

0 commit comments

Comments
 (0)