Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MicroServicesDemo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Divisha Agarwal
MicroServicesDemo
Commits
3e8b6767
Commit
3e8b6767
authored
Jun 16, 2020
by
Divisha Agarwal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added filters
parent
49c06e09
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
83 additions
and
10 deletions
+83
-10
FirstServiceApplication.java
...ce/src/main/java/com/example/FirstServiceApplication.java
+1
-1
FirstController.java
...src/main/java/com/example/controller/FirstController.java
+3
-2
build.gradle
gatewayServiceUsingJava/gatewayServiceUsingJava/build.gradle
+5
-1
GatewayServiceUsingJavaApplication.java
.../java/com/example/GatewayServiceUsingJavaApplication.java
+27
-0
SpringCloudConfig.java
...a/src/main/java/com/example/config/SpringCloudConfig.java
+4
-0
build.gradle
...singProperties/gatewayServiceUsingProperties/build.gradle
+4
-1
GatewayServiceUsingPropertiesApplication.java
...com/example/GatewayServiceUsingPropertiesApplication.java
+1
-1
CustomFilter.java
...erties/src/main/java/com/example/config/CustomFilter.java
+29
-0
application.yml
...ServiceUsingProperties/src/main/resources/application.yml
+3
-1
SecondServiceApplication.java
...e/src/main/java/com/example/SecondServiceApplication.java
+2
-1
SecondController.java
...rc/main/java/com/example/controller/SecondController.java
+4
-2
No files found.
firstService/firstService/src/main/java/com/example/
firstService/
FirstServiceApplication.java
→
firstService/firstService/src/main/java/com/example/FirstServiceApplication.java
View file @
3e8b6767
package
com
.
example
.
firstService
;
package
com
.
example
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
...
...
firstService/firstService/src/main/java/com/example/controller/FirstController.java
View file @
3e8b6767
...
...
@@ -7,7 +7,8 @@ import org.springframework.web.bind.annotation.*;
public
class
FirstController
{
@GetMapping
(
"/message"
)
public
String
test
()
{
return
"Hello JavaInUse Called in First Service"
;
public
String
test
(
@RequestHeader
(
"first-request"
)
String
header
)
{
System
.
out
.
println
(
header
);
return
"Hello Java Called in First Service"
;
}
}
gatewayServiceUsingJava/gatewayServiceUsingJava/build.gradle
View file @
3e8b6767
...
...
@@ -13,7 +13,11 @@ repositories {
}
dependencies
{
implementation
'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.springframework.boot:spring-boot-starter-web'
compile
group:
'org.springframework.boot'
,
name:
'spring-boot-starter'
,
version:
'2.2.8.RELEASE'
compile
group:
'org.springframework.boot'
,
name:
'spring-boot-starter-validation'
,
version:
'2.2.8.RELEASE'
runtime
group:
'org.springframework.cloud'
,
name:
'spring-cloud-dependencies'
,
version:
'Greenwich.SR2'
,
ext:
'pom'
compileOnly
'org.projectlombok:lombok'
compile
group:
'org.springframework.cloud'
,
name:
'spring-cloud-starter-gateway'
,
version:
'2.1.5.RELEASE'
annotationProcessor
'org.projectlombok:lombok'
...
...
gatewayServiceUsingJava/gatewayServiceUsingJava/src/main/java/com/example/
gatewayServiceUsingJava/
GatewayServiceUsingJavaApplication.java
→
gatewayServiceUsingJava/gatewayServiceUsingJava/src/main/java/com/example/GatewayServiceUsingJavaApplication.java
View file @
3e8b6767
package
com
.
example
.
gatewayServiceUsingJava
;
package
com
.
example
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.context.annotation.Bean
;
import
reactor.core.publisher.Mono
;
@SpringBootApplication
public
class
GatewayServiceUsingJavaApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
GatewayServiceUsingJavaApplication
.
class
,
args
);
}
@Bean
public
GlobalFilter
globalFilter
()
{
return
(
exchange
,
chain
)
->
{
System
.
out
.
println
(
"First Global filter"
);
return
chain
.
filter
(
exchange
).
then
(
Mono
.
fromRunnable
(()
->
{
System
.
out
.
println
(
"Second Global filter"
);
}));
};
}
}
gatewayServiceUsingJava/gatewayServiceUsingJava/src/main/java/com/example/config/SpringCloudConfig.java
View file @
3e8b6767
...
...
@@ -12,10 +12,14 @@ public class SpringCloudConfig {
public
RouteLocator
gatewayRoutes
(
RouteLocatorBuilder
builder
)
{
return
builder
.
routes
()
.
route
(
r
->
r
.
path
(
"/employee/**"
)
.
filters
(
f
->
f
.
addRequestHeader
(
"first-request"
,
"first-request-header"
)
.
addResponseHeader
(
"first-response"
,
"first-response-header"
))
.
uri
(
"http://localhost:8081/"
)
.
id
(
"employeeModule"
))
.
route
(
r
->
r
.
path
(
"/consumer/**"
)
.
filters
(
f
->
f
.
addRequestHeader
(
"second-request"
,
"second-request-header"
)
.
addResponseHeader
(
"second-response"
,
"second-response-header"
))
.
uri
(
"http://localhost:8082/"
)
.
id
(
"consumerModule"
))
.
build
();
...
...
gatewayServiceUsingProperties/gatewayServiceUsingProperties/build.gradle
View file @
3e8b6767
...
...
@@ -13,7 +13,10 @@ repositories {
}
dependencies
{
implementation
'org.springframework.boot:spring-boot-starter-web'
compile
group:
'org.springframework.boot'
,
name:
'spring-boot-starter'
,
version:
'2.2.8.RELEASE'
compile
group:
'org.springframework.cloud'
,
name:
'spring-cloud-starter-gateway'
,
version:
'2.1.5.RELEASE'
compile
group:
'org.springframework.boot'
,
name:
'spring-boot-starter-validation'
,
version:
'2.2.8.RELEASE'
compileOnly
'org.projectlombok:lombok'
annotationProcessor
'org.projectlombok:lombok'
testImplementation
(
'org.springframework.boot:spring-boot-starter-test'
)
{
...
...
gatewayServiceUsingProperties/gatewayServiceUsingProperties/src/main/java/com/example/
gatewayServiceUsingProperties/
GatewayServiceUsingPropertiesApplication.java
→
gatewayServiceUsingProperties/gatewayServiceUsingProperties/src/main/java/com/example/GatewayServiceUsingPropertiesApplication.java
View file @
3e8b6767
package
com
.
example
.
gatewayServiceUsingProperties
;
package
com
.
example
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
...
...
gatewayServiceUsingProperties/gatewayServiceUsingProperties/src/main/java/com/example/config/CustomFilter.java
0 → 100644
View file @
3e8b6767
package
com
.
example
.
config
;
import
org.springframework.cloud.gateway.filter.GatewayFilter
;
import
org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory
;
import
org.springframework.stereotype.Component
;
import
reactor.core.publisher.Mono
;
@Component
public
class
CustomFilter
extends
AbstractGatewayFilterFactory
<
CustomFilter
.
Config
>
{
public
CustomFilter
()
{
super
(
Config
.
class
);
}
@Override
public
GatewayFilter
apply
(
Config
config
)
{
//Custom Pre Filter. Suppose we can extract JWT and perform Authentication
return
(
exchange
,
chain
)
->
{
System
.
out
.
println
(
"First pre filter"
+
exchange
.
getRequest
());
//Custom Post Filter.Suppose we can call error response handler based on error code.
return
chain
.
filter
(
exchange
).
then
(
Mono
.
fromRunnable
(()
->
{
System
.
out
.
println
(
"First post filter"
);
}));
};
}
public
static
class
Config
{
// Put the configuration properties
}
}
gatewayServiceUsingProperties/gatewayServiceUsingProperties/src/main/resources/application.yml
View file @
3e8b6767
...
...
@@ -4,6 +4,8 @@ server:
spring
:
cloud
:
gateway
:
default-filters
:
-
name
:
CustomFilter
routes
:
-
id
:
employeeModule
uri
:
http://localhost:8081/
...
...
@@ -12,4 +14,4 @@ spring:
-
id
:
consumerModule
uri
:
http://localhost:8082/
predicates
:
-
Path=/consumer/**
\ No newline at end of file
-
Path=/consumer/**
secondService/secondService/src/main/java/com/example/
secondService/
SecondServiceApplication.java
→
secondService/secondService/src/main/java/com/example/SecondServiceApplication.java
View file @
3e8b6767
package
com
.
example
.
secondService
;
package
com
.
example
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
...
...
@@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public
class
SecondServiceApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
SecondServiceApplication
.
class
,
args
);
}
...
...
secondService/secondService/src/main/java/com/example/controller/SecondController.java
View file @
3e8b6767
package
com
.
example
.
controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
...
...
@@ -9,8 +10,9 @@ import org.springframework.web.bind.annotation.RestController;
public
class
SecondController
{
@GetMapping
(
"/message"
)
public
String
test
(){
return
"Hello JavaInUse Called in Second Service"
;
public
String
test
(
@RequestHeader
(
"second-request"
)
String
header
)
{
System
.
out
.
println
(
header
);
return
"Hello Java Called in Second Service"
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment