Skip to content

Commit

Permalink
feat($Gateway): add reactive openfeign support
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Dec 16, 2020
1 parent 7211915 commit f0b7567
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 16 deletions.
29 changes: 18 additions & 11 deletions gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
<artifactId>gateway</artifactId>
<name>Muscle and Fitness Server :: Gateway</name>
<description>Gateway for Muscle and Fitness microservices.</description>
<properties>
<java.version>11</java.version>
<port>8080</port>
</properties>
<parent>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>muscle-and-fitness-server</artifactId>
Expand Down Expand Up @@ -90,16 +86,27 @@
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

<dependency>
<groupId>com.playtika.reactivefeign</groupId>
<artifactId>feign-reactor-webclient</artifactId>
<version>${feign-reactor.version}</version>
</dependency>

<dependency>
<groupId>com.playtika.reactivefeign</groupId>
<artifactId>feign-reactor-cloud</artifactId>
<version>${feign-reactor.version}</version>
</dependency>

<dependency>
<groupId>com.playtika.reactivefeign</groupId>
<artifactId>feign-reactor-spring-configuration</artifactId>
<version>${feign-reactor.version}</version>
</dependency>

<dependency>
<groupId>com.jmsoftware.maf</groupId>
<artifactId>common</artifactId>
<version>0.0.2-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import reactivefeign.spring.config.EnableReactiveFeignClients;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -21,8 +23,10 @@
* @date 2/15/20 3:56 PM
**/
@Slf4j
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableReactiveFeignClients
public class GatewayApplication {
private static final String LINE_SEPARATOR = System.lineSeparator();
private static ProjectProperty projectProperty;
Expand All @@ -43,7 +47,7 @@ public static void main(String[] args) {
log.info("⚙️ Environment: {}", projectProperty.getEnvironment());
log.info("⏳ Deployment duration: {} seconds ({} ms)", duration.getSeconds(), duration.toMillis());
log.info("⏰ App started at {} (timezone - {})", endInstant, TimeZone.getDefault().getDisplayName());
log.info("{} App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}/{}",
log.info("{} App running at{} - Local: http://localhost:{}{}/{} - Network: {}/{}",
LINE_SEPARATOR, LINE_SEPARATOR, serverConfiguration.getServerPort(), projectProperty.getContextPath(),
LINE_SEPARATOR, serverConfiguration.getBaseUrl(), projectProperty.getContextPath());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jmsoftware.maf.gateway.remoteapi;

import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import reactivefeign.spring.config.ReactiveFeignClient;
import reactor.core.publisher.Mono;

/**
* <h1>AuthCenterRemoteApi</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 5/10/20 4:50 PM
*/
@Validated
@ReactiveFeignClient(name = "auth-center")
public interface AuthCenterRemoteApi {
/**
* Gets role list by user id.
*
* @param userId the user id
* @return the role list by user id
*/
@GetMapping("/role-remote-api/roles/{userId}")
Mono<ResponseBodyBean<GetRoleListByUserIdResponse>> getRoleListByUserId(@PathVariable Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void postConstruct() {

@Bean
public RouterFunction<ServerResponse> home() {
return route(GET("/home"), request -> {
return route(GET("/"), request -> {
log.info("Redirect to Home page.");
return ServerResponse.temporaryRedirect(URI.create("/static/home.html")).build();
});
Expand All @@ -47,7 +47,7 @@ public RouterFunction<ServerResponse> doc() {
@Bean
public RouterFunction<ServerResponse> favicon() {
return route(GET("/favicon.ico"), request -> {
log.info("Redirect to Bootstrap Swagger API documentation.");
log.info("Redirect to favicon.");
return ServerResponse.temporaryRedirect(URI.create("/static/icon/favicon.ico")).build();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.jmsoftware.maf.gateway.universal.controller;

import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.gateway.remoteapi.AuthCenterRemoteApi;
import com.jmsoftware.maf.gateway.universal.domain.ValidationTestPayload;
import com.jmsoftware.maf.gateway.universal.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.web.bind.annotation.*;

Expand All @@ -19,17 +21,27 @@
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2/4/20 10:29 AM
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
@Api(tags = {"Common Controller"})
public class CommonController {
private final CommonService commonService;
private final AuthCenterRemoteApi authCenterRemoteApi;

@GetMapping("/app-info")
@ApiOperation(value = "/app-info", notes = "Retrieve application information")
public ResponseBodyBean<Map<String, Object>> applicationInformation() {
val data = commonService.getApplicationInfo();
final var roleListByUserId = authCenterRemoteApi.getRoleListByUserId(1L);
roleListByUserId
.map(getRoleListByUserIdResponseResponseBodyBean -> {
log.info("Response1: {}", getRoleListByUserIdResponseResponseBodyBean);
return getRoleListByUserIdResponseResponseBodyBean.getData();
})
.doOnNext(getRoleListByUserIdResponse -> log.info("Response2: {}", getRoleListByUserIdResponse.getRoleList()))
.subscribe();
return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RequestFilter implements GlobalFilter {

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("{} intercepted requester's access. Requester: {}, resource: [{}] {}",
log.info("{} intercepted requesters access. Requester: {}, resource: [{}] {}",
projectProperty.getProjectArtifactId().toUpperCase(),
RequestUtil.getRequestIpAndPort(exchange.getRequest()), exchange.getRequest().getMethod(),
exchange.getRequest().getURI());
Expand Down
16 changes: 15 additions & 1 deletion gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: @port@
port: @gateway.port@
tomcat:
uri-encoding: @project.build.sourceEncoding@

Expand Down Expand Up @@ -36,6 +36,20 @@ eureka:
client:
registryFetchIntervalSeconds: 5

feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 10000
loggerLevel: full
compression:
request:
enabled: true
response:
enabled: true
useGzipDecoder: true

management:
endpoints:
web:
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<guava.version>30.0-jre</guava.version>
<knife4j.version>3.0.2</knife4j.version>
<jjwt.version>0.11.1</jjwt.version>
<feign-reactor.version>2.0.25</feign-reactor.version>
</properties>

<!-- The modules (sometimes called subprojects) to build as a part of this project. -->
Expand Down

0 comments on commit f0b7567

Please sign in to comment.