Fixes Spring Cloud Gateway configuration

This commit is contained in:
Markus Günther
2021-02-05 21:40:38 +01:00
parent 6e72c0e649
commit 847a5a34d5
15 changed files with 109 additions and 115 deletions

View File

@@ -11,16 +11,16 @@ services:
- 9092:9092
environment:
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_DEFAULT_REPLICATION_FACTOR: 1
KAFKA_NUM_PARTITIONS: 5
KAFKA_CREATE_TOPICS: "topic-getting-things-done:5:1"
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
volumes:
- /var/run/docker.sock:/var/run/docker.sock
discovery:
image: getting-things-done/discovery-service
ports:

View File

@@ -24,12 +24,20 @@
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -1,27 +1,20 @@
package net.mguenther.gtd;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
/**
* @author Markus Günther (markus.guenther@gmail.com)
* @author Boris Fresow (bfresow@gmail.com)
*/
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
@EnableEurekaClient
public class GtdApiGatewayApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(GtdApiGatewayApplication.class).web(WebApplicationType.SERVLET).run(args);
}
@Bean
public OnMethodFilter onMethodZuulFilter() {
return new OnMethodFilter();
SpringApplication.run(GtdApiGatewayApplication.class, args);
}
}

View File

@@ -1,61 +0,0 @@
package net.mguenther.gtd;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
/**
* @author Markus Günther (markus.guenther@gmail.com)
* @author Boris Fresow (bfresow@gmail.com)
*/
public class OnMethodFilter extends ZuulFilter {
private static final Logger log = LoggerFactory.getLogger(OnMethodFilter.class);
private static final List<String> methodsForCommands = Arrays.asList("POST", "PUT", "PATCH", "DELETE");
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
final RequestContext ctx = RequestContext.getCurrentContext();
final String method = ctx.getRequest().getMethod();
if (isCommand(ctx)) {
log.info("Resolved incoming request using method {} to service ID 'gtd-es-command-side'.", method);
ctx.set("serviceId", "gtd-es-command-side");
ctx.setRouteHost(null);
ctx.addOriginResponseHeader("X-Zuul-ServiceId", UUID.randomUUID().toString());
} else {
log.info("Resolved incoming request using method {} to service ID 'gtd-es-query-side'.", method);
ctx.set("serviceId", "gtd-es-query-side");
ctx.setRouteHost(null);
ctx.addOriginResponseHeader("X-Zuul-ServiceId", UUID.randomUUID().toString());
}
return null;
}
private boolean isCommand(final RequestContext ctx) {
return
StringUtils.isNotEmpty(ctx.getRequest().getMethod()) &&
methodsForCommands.contains(ctx.getRequest().getMethod().toUpperCase());
}
}

View File

@@ -0,0 +1,23 @@
package net.mguenther.gtd;
import com.netflix.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
@Configuration
public class SpringCloudConfig {
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.method(HttpMethod.DELETE, HttpMethod.PUT, HttpMethod.POST, HttpMethod.PATCH)
.uri("lb://gtd-es-command-side"))
.route(r -> r.method(HttpMethod.GET)
.uri("lb://gtd-es-query-side"))
.build();
}
}

View File

@@ -1,19 +0,0 @@
spring.application.name=gtd-es-api-gateway
info.component=Edge Server
endpoints.restart.enabled=true
endpoints.shutdown.enabled=true
endpoints.health.sensitive=false
ribbon.eureka.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.client.registerWithEureka=false
server.port=8765
zuul.prefix=/api
zuul.routes.gtd.path: /**
management.security.enabled=false

View File

@@ -5,9 +5,6 @@ management:
security:
enabled: false
info:
component: Edge Server
endpoints:
restart:
enabled: true
@@ -16,9 +13,6 @@ endpoints:
health:
sensitive: false
ribbon:
eurekaEnabled: true
eureka:
client:
enabled: true
@@ -26,12 +20,23 @@ eureka:
enabled: true
serviceUrl:
defaultZone: ${EUREKA_CLIENT_SERVICEURL_DEFAULTZONE:http://localhost:8761/eureka/}
register-with-eureka: true
fetch-registry: true
instance:
appname: gtd-es-api-gateway
preferIpAddress: true
zuul:
prefix: /api
routes:
gtd:
path: /**
spring:
application:
name: gtd-es-api-gateway
cloud:
discovery:
enabled: true
client:
health-indicator:
enabled: true
gateway:
discovery:
locator:
lower-case-service-id: true
enabled: true

View File

@@ -78,7 +78,7 @@
<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -1,12 +1,15 @@
server:
contextPath: /api
port: 8089
servlet:
context-path: /api
management:
security:
enabled: false
spring:
application:
name: gtd-es-command-side
kafka:
bootstrapServers: localhost:9092
consumer:
@@ -29,6 +32,7 @@ eureka:
enabled: true
serviceUrl:
defaultZone: ${EUREKA_CLIENT_SERVICEURL_DEFAULTZONE:http://localhost:8761/eureka/}
register-with-eureka: true
instance:
appname: gtd-es-command-side
preferIpAddress: true

View File

@@ -1,3 +1,9 @@
spring:
application:
name: gtd-es-command-side
cloud:
discovery:
enabled: true
client:
health-indicator:
enabled: true

View File

@@ -25,7 +25,7 @@
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -82,7 +82,7 @@
<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -1,8 +1,11 @@
server:
contextPath: /api
port: 8090
servlet:
context-path: /api
spring:
application:
name: gtd-es-query-side
kafka:
bootstrapServers: localhost:9092
consumer:
@@ -29,6 +32,7 @@ eureka:
enabled: true
serviceUrl:
defaultZone: ${EUREKA_CLIENT_SERVICEURL_DEFAULTZONE:http://localhost:8761/eureka/}
register-with-eureka: true
instance:
appname: gtd-es-query-side
preferIpAddress: true

View File

@@ -1,3 +1,9 @@
spring:
application:
name: gtd-es-query-side
cloud:
discovery:
enabled: true
client:
health-indicator:
enabled: true

31
pom.xml
View File

@@ -28,7 +28,7 @@
<slf4j.version>1.7.22</slf4j.version>
<spring.version>5.3.3</spring.version>
<spring.kafka.version>2.6.5</spring.kafka.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
<avro.version>1.8.1</avro.version>
<commons-lang3.version>3.5</commons-lang3.version>
<!-- Plugin versions -->
@@ -88,11 +88,36 @@
<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<artifactId>spring-cloud-gateway</artifactId>
<version>3.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.1</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>