├── spring-cloud-bus-apache-kafka
├── config
│ ├── external-dev.properties
│ ├── external-prod.properties
│ └── external.properties
├── readme.rst
├── spring-dynamic-runtime-configurations-client
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── dynamic
│ │ │ └── runtime
│ │ │ └── configurations
│ │ │ └── client
│ │ │ ├── SpringDynamicRuntimeConfigClientApp.java
│ │ │ └── rest
│ │ │ └── controller
│ │ │ └── SpringRestController.java
│ │ └── resources
│ │ └── application.properties
├── spring-dynamic-runtime-configurations-client2
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── dynamic
│ │ │ └── runtime
│ │ │ └── configurations
│ │ │ └── client
│ │ │ ├── SpringDynamicRuntimeConfigClient2App.java
│ │ │ └── rest
│ │ │ └── controller
│ │ │ └── SpringRestController.java
│ │ └── resources
│ │ └── application.properties
└── spring-dynamic-runtime-configurations-server
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── dynamic
│ │ └── runtime
│ │ └── configurations
│ │ └── server
│ │ └── SpringDynamicRuntimeConfigServerApp.java
│ └── resources
│ └── application.properties
├── spring-cloud-gateway-circuit-breaker-hystrix
├── build.gradle
├── readme.rst
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── cloud
│ │ └── gateway
│ │ ├── SpringCloudGatewayApp.java
│ │ └── rest
│ │ └── controller
│ │ └── HystrixFallbackRestController.java
│ └── resources
│ └── application.yml
├── spring-cloud-gateway-security-jwt-spring-boot-3
├── readme.rst
├── spring-boot-cloud-gateway
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── cloud
│ │ │ └── gateway
│ │ │ └── CloudGatewayApp.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-alert
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-auth
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-echo
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-eureka
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── resources
│ │ └── application.properties
└── spring-boot-microservice-hello
│ ├── pom.xml
│ └── src
│ └── main
│ └── resources
│ └── application.properties
├── spring-cloud-gateway-security-jwt
├── readme.rst
├── spring-boot-cloud-gateway
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── cloud
│ │ │ └── gateway
│ │ │ ├── CloudGatewayApp.java
│ │ │ ├── config
│ │ │ └── GatewayConfig.java
│ │ │ ├── exception
│ │ │ ├── JwtTokenMalformedException.java
│ │ │ └── JwtTokenMissingException.java
│ │ │ ├── filter
│ │ │ └── JwtAuthenticationFilter.java
│ │ │ └── util
│ │ │ └── JwtUtil.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-alert
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── microservice
│ │ │ └── alert
│ │ │ └── AlertServiceApp.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-auth
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── auth
│ │ │ └── service
│ │ │ ├── AuthServiceApp.java
│ │ │ ├── exception
│ │ │ ├── JwtTokenMalformedException.java
│ │ │ └── JwtTokenMissingException.java
│ │ │ ├── rest
│ │ │ └── controller
│ │ │ │ └── AuthRestController.java
│ │ │ └── util
│ │ │ └── JwtUtil.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-echo
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── microservice
│ │ │ └── echo
│ │ │ └── EchoServiceApp.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-eureka
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── microservice
│ │ │ └── eureka
│ │ │ └── EurekaServerApp.java
│ │ └── resources
│ │ └── application.properties
└── spring-boot-microservice-hello
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── boot
│ │ └── microservice
│ │ └── hello
│ │ └── HelloServiceApp.java
│ └── resources
│ └── application.properties
├── spring-cloud-gateway
├── build.gradle
├── readme.rst
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── cloud
│ │ └── gateway
│ │ └── SpringCloudGatewayApp.java
│ └── resources
│ └── application.yml
├── spring-cloud-load-balancer
├── currency-conversion-service
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── cc
│ │ │ ├── CurrencyConversionApplication.java
│ │ │ ├── config
│ │ │ ├── ForexConfiguration.java
│ │ │ └── WebClientConfig.java
│ │ │ ├── model
│ │ │ └── CurrencyConversion.java
│ │ │ └── rest
│ │ │ └── controller
│ │ │ └── CurrencyConversionRestController.java
│ │ └── resources
│ │ └── application.yml
├── eureka-server-config
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── eureka
│ │ │ └── server
│ │ │ └── config
│ │ │ └── com.roytuts.eureka.server.config.java
│ │ └── resources
│ │ └── application.yml
├── forex-service
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── com
│ │ │ └── roytuts
│ │ │ └── forex
│ │ │ ├── ForexApplication.java
│ │ │ ├── entity
│ │ │ └── ForexValue.java
│ │ │ ├── repository
│ │ │ └── ForexRepository.java
│ │ │ └── rest
│ │ │ └── controller
│ │ │ └── ForexRestController.java
│ │ └── resources
│ │ └── application.yml
├── forex.sql
└── readme.rst
├── spring-cloud-microservices-random-ports
├── readme.rst
├── spring-boot-microservice-alert
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── microservice
│ │ │ └── alert
│ │ │ ├── AlertServiceApp.java
│ │ │ └── ServerPortsRangeConfig.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-echo
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── microservice
│ │ │ └── echo
│ │ │ ├── EchoServiceApp.java
│ │ │ └── ServerPortsRangeConfig.java
│ │ └── resources
│ │ └── application.properties
├── spring-boot-microservice-eureka
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── microservice
│ │ │ └── eureka
│ │ │ └── EurekaServerApp.java
│ │ └── resources
│ │ └── application.properties
└── spring-boot-microservice-hello
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── boot
│ │ └── microservice
│ │ └── hello
│ │ ├── HelloServiceApp.java
│ │ └── ServerPortsRangeConfig.java
│ └── resources
│ └── application.properties
├── spring-cloud-microservices-trace-logs-sleuth-zipkin
├── readme.rst
├── spring-microservices-celsius-to-fahrenheit-converter
│ ├── build.gradle
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── celsius
│ │ │ └── to
│ │ │ └── fahrenheit
│ │ │ └── converter
│ │ │ └── CelsiusToFahrenheitConverterApp.java
│ │ └── resources
│ │ └── application.properties
├── spring-microservices-eureka-server
│ ├── build.gradle
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── microservices
│ │ │ └── eureka
│ │ │ └── server
│ │ │ └── EurekaServerApp.java
│ │ └── resources
│ │ └── application.properties
└── spring-microservices-fahrenheit-to-celsius-converter
│ ├── build.gradle
│ └── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── fahrenheit
│ │ └── to
│ │ └── celsius
│ │ └── converter
│ │ └── FahrenheitToCelsiusConverterApp.java
│ └── resources
│ └── application.properties
├── spring-cloud-stream-kafka
├── readme.rst
├── spring-cloud-stream-kafka-ordercheck
│ ├── build.gradle
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── roytuts
│ │ │ └── spring
│ │ │ └── cloud
│ │ │ └── stream
│ │ │ └── kafka
│ │ │ └── ordercheck
│ │ │ ├── OrderCheckApp.java
│ │ │ ├── checker
│ │ │ └── OrderChecker.java
│ │ │ ├── enums
│ │ │ └── Status.java
│ │ │ ├── model
│ │ │ └── Order.java
│ │ │ └── processor
│ │ │ └── OrderProcessor.java
│ │ └── resources
│ │ └── application.properties
└── spring-cloud-stream-kafka-ordersource
│ ├── build.gradle
│ └── src
│ └── main
│ └── java
│ └── com
│ └── roytuts
│ └── spring
│ └── cloud
│ └── stream
│ └── using
│ └── kafka
│ ├── OrderSourceApp.java
│ ├── enums
│ └── Status.java
│ └── model
│ └── Order.java
└── spring-dynamic-configuration-runtime
├── config
└── external.properties
├── readme.rst
├── spring-dynamic-runtime-configurations-client
├── build.gradle
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── roytuts
│ │ └── spring
│ │ └── dynamic
│ │ └── runtime
│ │ └── configurations
│ │ └── client
│ │ ├── SpringDynamicRuntimeConfigClientApp.java
│ │ └── rest
│ │ └── controller
│ │ └── SpringRestController.java
│ └── resources
│ └── application.properties
└── spring-dynamic-runtime-configurations-server
├── build.gradle
├── pom.xml
└── src
└── main
├── java
└── com
│ └── roytuts
│ └── spring
│ └── dynamic
│ └── runtime
│ └── configurations
│ └── server
│ └── SpringDynamicRuntimeConfigServerApp.java
└── resources
└── application.properties
/spring-cloud-bus-apache-kafka/config/external-dev.properties:
--------------------------------------------------------------------------------
1 | msg=Hello Dev
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/config/external-prod.properties:
--------------------------------------------------------------------------------
1 | msg=Hello Prod
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/config/external.properties:
--------------------------------------------------------------------------------
1 | msg=Hello Developer
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/readme.rst:
--------------------------------------------------------------------------------
1 | You can go through the tutorial https://roytuts.com/spring-cloud-bus-example/
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-dynamic-runtime-configurations-client
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.3
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-config
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-actuator
38 |
39 |
40 |
41 | org.springframework.cloud
42 | spring-cloud-starter-bus-kafka
43 |
44 |
45 |
46 | javax.xml.bind
47 | jaxb-api
48 | runtime
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.cloud
56 | spring-cloud-dependencies
57 | 2020.0.0
58 | pom
59 | import
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 | org.springframework.boot
68 | spring-boot-maven-plugin
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/client/SpringDynamicRuntimeConfigClientApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.client;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringDynamicRuntimeConfigClientApp {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringDynamicRuntimeConfigClientApp.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/client/rest/controller/SpringRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.client.rest.controller;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.cloud.context.config.annotation.RefreshScope;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | @RefreshScope
11 | @RestController
12 | public class SpringRestController {
13 |
14 | @Value("${msg:Hey}")
15 | private String msg;
16 |
17 | @GetMapping("/msg")
18 | public ResponseEntity greeting() {
19 | return new ResponseEntity(msg, HttpStatus.OK);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=external
2 | management.security.enabled=false
3 | management.endpoints.web.exposure.include=*
4 | spring.config.import=optional:configserver:http://localhost:8888/
5 |
6 | #Kafka config
7 | spring.cloud.bus.refresh.enabled=true
8 | spring.cloud.bus.env.enabled=true
9 | endpoints.spring.cloud.bus.refresh.enabled=true
10 | endpoints.spring.cloud.bus.env.enabled=true
11 | spring.cloud.stream.kafka.binder.autoAddPartitions=true
12 | spring.kafka.bootstrap-servers=http://localhost:9092
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-dynamic-runtime-configurations-client2
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.3
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-config
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-actuator
38 |
39 |
40 |
41 | org.springframework.cloud
42 | spring-cloud-starter-bus-kafka
43 |
44 |
45 |
46 | javax.xml.bind
47 | jaxb-api
48 | runtime
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.cloud
56 | spring-cloud-dependencies
57 | 2020.0.0
58 | pom
59 | import
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 | org.springframework.boot
68 | spring-boot-maven-plugin
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client2/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/client/SpringDynamicRuntimeConfigClient2App.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.client;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringDynamicRuntimeConfigClient2App {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringDynamicRuntimeConfigClient2App.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client2/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/client/rest/controller/SpringRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.client.rest.controller;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.cloud.context.config.annotation.RefreshScope;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | @RefreshScope
11 | @RestController
12 | public class SpringRestController {
13 |
14 | @Value("${msg:Hey}")
15 | private String msg;
16 |
17 | @GetMapping("/msg2")
18 | public ResponseEntity greeting() {
19 | return new ResponseEntity(msg, HttpStatus.OK);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-client2/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=8081
2 | spring.application.name=external
3 | management.security.enabled=false
4 | management.endpoints.web.exposure.include=*
5 | spring.config.import=optional:configserver:http://localhost:8888/
6 |
7 | #Kafka config
8 | spring.cloud.bus.refresh.enabled=true
9 | spring.cloud.bus.env.enabled=true
10 | endpoints.spring.cloud.bus.refresh.enabled=true
11 | endpoints.spring.cloud.bus.env.enabled=true
12 | spring.cloud.stream.kafka.binder.autoAddPartitions=true
13 | spring.kafka.bootstrap-servers=http://localhost:9092
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-dynamic-runtime-configurations-server
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.3
22 |
23 |
24 |
25 |
26 | org.springframework.cloud
27 | spring-cloud-config-server
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-bus-kafka
33 |
34 |
35 |
36 | javax.xml.bind
37 | jaxb-api
38 | runtime
39 |
40 |
41 |
42 |
43 |
44 |
45 | org.springframework.cloud
46 | spring-cloud-dependencies
47 | 2020.0.0
48 | pom
49 | import
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-maven-plugin
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-server/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/server/SpringDynamicRuntimeConfigServerApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.server;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.config.server.EnableConfigServer;
6 |
7 | @EnableConfigServer
8 | @SpringBootApplication
9 | public class SpringDynamicRuntimeConfigServerApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(SpringDynamicRuntimeConfigServerApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-bus-apache-kafka/spring-dynamic-runtime-configurations-server/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=config-server
2 | server.port=8888
3 | spring.profiles.active=native
4 | spring.cloud.config.server.native.search-locations=file:///C:/config
5 |
6 | #Kafka config
7 | spring.cloud.bus.enabled=true
8 | management.endpoints.web.exposure.include=bus-refresh
9 | spring.kafka.bootstrap-servers=http://localhost:9092
--------------------------------------------------------------------------------
/spring-cloud-gateway-circuit-breaker-hystrix/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.2.6.RELEASE'
4 | }
5 | repositories {
6 | mavenLocal()
7 | mavenCentral()
8 | }
9 | dependencies {
10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
11 | }
12 | }
13 |
14 | plugins {
15 | id "io.spring.dependency-management" version "1.0.9.RELEASE"
16 | }
17 |
18 | apply plugin: 'java'
19 | apply plugin: 'org.springframework.boot'
20 |
21 | sourceCompatibility = 12
22 | targetCompatibility = 12
23 |
24 | repositories {
25 | mavenLocal()
26 | mavenCentral()
27 | }
28 |
29 | dependencies {
30 | implementation("org.springframework.cloud:spring-cloud-starter")
31 | implementation("org.springframework.cloud:spring-cloud-starter-gateway")
32 | implementation("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
33 | implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
34 | }
35 |
36 | dependencyManagement {
37 | imports {
38 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR3'
39 | }
40 | }
--------------------------------------------------------------------------------
/spring-cloud-gateway-circuit-breaker-hystrix/readme.rst:
--------------------------------------------------------------------------------
1 | Please follow the tutorial https://roytuts.com/how-to-implement-circuit-breaker-pattern-using-hystrix-in-spring-cloud-gateway/
2 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-circuit-breaker-hystrix/src/main/java/com/roytuts/spring/cloud/gateway/SpringCloudGatewayApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.gateway;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6 |
7 | @EnableEurekaClient
8 | @SpringBootApplication
9 | public class SpringCloudGatewayApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(SpringCloudGatewayApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-circuit-breaker-hystrix/src/main/java/com/roytuts/spring/cloud/gateway/rest/controller/HystrixFallbackRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.gateway.rest.controller;
2 |
3 | import org.springframework.web.bind.annotation.GetMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | import reactor.core.publisher.Mono;
7 |
8 | @RestController
9 | public class HystrixFallbackRestController {
10 |
11 | @GetMapping("/fx-exchange/fallback")
12 | public Mono getFxSvcMsg() {
13 | return Mono.just("No response fron Forex Service and will be back shortly");
14 | }
15 |
16 | @GetMapping("/cc-converter/fallback")
17 | public Mono getCcSvcMsg() {
18 | return Mono.just("No response fron Currency Conversion Service and will be back shortly");
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-circuit-breaker-hystrix/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 2000
2 |
3 | server:
4 | port: 9090
5 |
6 | spring:
7 | application:
8 | name: gateway
9 | cloud:
10 | gateway:
11 | routes:
12 | - id: currency-conversion-service
13 | #uri: http://localhost:9100/
14 | uri: lb://currency-conversion-service/
15 | predicates:
16 | - Path=/cc-converter/**
17 | filters:
18 | - RewritePath=/cc-converter/from/(?.*)/to/(?.*)/quantity/(?.*), /currency-converter/from/$\{from}/to/$\{to}/quantity/$\{quantity}
19 | - name: Hystrix
20 | args:
21 | name: hystrixFx
22 | fallbackUri: forward:/cc-converter/fallback
23 | - id: forex-service
24 | #uri: http://localhost:9000/, http://localhost:9001/
25 | uri: lb://forex-service/
26 | predicates:
27 | - Path=/fx-exchange/**
28 | filters:
29 | - RewritePath=/fx-exchange/from/(?.*)/to/(?.*), /forex-exchange/from/$\{from}/to/$\{to}
30 | - name: Hystrix
31 | args:
32 | name: hystrixCc
33 | fallbackUri: forward:/fx-exchange/fallback
34 | discovery:
35 | locator:
36 | enabled: true
37 | lower-case-service-id: true
38 |
39 | eureka:
40 | client:
41 | service-url:
42 | default-zone: http://localhost:8761/eureka
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/readme.rst:
--------------------------------------------------------------------------------
1 | Please follow the tutorial https://roytuts.com/spring-cloud-gateway-security-with-jwt-json-web-token/
2 |
3 | Spring Boot - 3.0.2
4 | Maven - 3.8.5
5 | Java - 19.0.1
6 |
7 | This repo only contains the changes from the actual repo https://github.com/roytuts/spring-cloud/tree/master/spring-cloud-gateway-security-jwt
8 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-cloud-gateway/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | com.roytuts
5 | spring-boot-cloud-gateway
6 | 0.0.1-SNAPSHOT
7 |
8 | UTF-8
9 | 19
10 | 19
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 3.0.2
16 |
17 |
18 |
19 | org.springframework.cloud
20 | spring-cloud-starter
21 |
22 |
23 | org.springframework.cloud
24 | spring-cloud-starter-gateway
25 |
26 |
27 | org.springframework.cloud
28 | spring-cloud-starter-netflix-eureka-client
29 |
30 |
31 | javax.xml.bind
32 | jaxb-api
33 | 2.4.0-b180830.0359
34 |
35 |
36 | io.jsonwebtoken
37 | jjwt-api
38 | 0.11.5
39 |
40 |
41 |
42 |
43 |
44 | org.springframework.cloud
45 | spring-cloud-dependencies
46 | 2022.0.1
47 | pom
48 | import
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-maven-plugin
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/CloudGatewayApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 |
7 | @EnableDiscoveryClient
8 | @SpringBootApplication
9 | public class CloudGatewayApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(CloudGatewayApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-cloud-gateway/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=gateway
2 | eureka.client.service-url.default-zone=http://localhost:8761/eureka
3 |
4 | eureka.instance.prefer-ip-address=true
5 | eureka.instance.hostname=localhost
6 | eureka.instance.instance-id=
7 |
8 | #secret key - should be encrypted
9 | jwt.secret=secretkey
10 | #ignore null fields in json
11 | spring.jackson.default-property-inclusion=NON_NULL
12 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-alert/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 |
6 | com.roytuts
7 | spring-boot-microservice-alert
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 19
13 | 19
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-parent
19 | 3.0.2
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-starter-netflix-eureka-client
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.springframework.cloud
38 | spring-cloud-dependencies
39 | 2022.0.1
40 | pom
41 | import
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-maven-plugin
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-alert/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=alert
2 | #server.port=0
3 | server.port=7000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 | eureka.instance.instance-id=
8 |
9 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
10 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-auth/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 |
6 | com.roytuts
7 | spring-boot-auth-service
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 19
13 | 19
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-parent
19 | 3.0.2
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-starter-netflix-eureka-client
31 |
32 |
33 |
34 | javax.xml.bind
35 | jaxb-api
36 | 2.4.0-b180830.0359
37 |
38 |
39 |
40 | io.jsonwebtoken
41 | jjwt-api
42 | 0.11.5
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.cloud
50 | spring-cloud-dependencies
51 | 2022.0.1
52 | pom
53 | import
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.springframework.boot
62 | spring-boot-maven-plugin
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-auth/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=auth
2 | #server.port=0
3 | server.port=6000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 | eureka.instance.instance-id=
8 |
9 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
10 |
11 | #secret key - should be encrypted
12 | jwt.secret=secretkey
13 | #3 minutes validity
14 | jwt.token.validity=180000
15 | #ignore null fields in json
16 | spring.jackson.default-property-inclusion=NON_NULL
17 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-echo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 |
6 | com.roytuts
7 | spring-boot-microservice-echo
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 19
13 | 19
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-parent
19 | 3.0.2
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-starter-netflix-eureka-client
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.springframework.cloud
38 | spring-cloud-dependencies
39 | 2022.0.1
40 | pom
41 | import
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-maven-plugin
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-echo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=echo
2 | #server.port=0
3 | server.port=9000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 | eureka.instance.instance-id=
8 |
9 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
10 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-eureka/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 |
6 | com.roytuts
7 | spring-boot-microservice-eureka
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 19
13 | 19
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-parent
19 | 3.0.2
20 |
21 |
22 |
23 |
24 | org.springframework.cloud
25 | spring-cloud-starter-netflix-eureka-server
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.cloud
33 | spring-cloud-dependencies
34 | 2022.0.1
35 | pom
36 | import
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-maven-plugin
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-eureka/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=eureka
2 | server.port=8761
3 |
4 | #eureka.instance.hostname=localhost
5 | eureka.client.register-with-eureka=false
6 | eureka.client.fetch-registry=false
7 |
8 | eureka.instance.prefer-ip-address=true
9 | eureka.instance.hostname=localhost
10 | eureka.instance.instance-id=
11 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-hello/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 |
6 | com.roytuts
7 | spring-boot-microservice-hello
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 19
13 | 19
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-parent
19 | 3.0.2
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-starter-netflix-eureka-client
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.springframework.cloud
38 | spring-cloud-dependencies
39 | 2022.0.1
40 | pom
41 | import
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-maven-plugin
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt-spring-boot-3/spring-boot-microservice-hello/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=hello
2 | #server.port=0
3 | server.port=8000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 | eureka.instance.instance-id=
8 |
9 | eureka.client.service-url.default-zone=http://localhost:8761/eureka/
10 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/readme.rst:
--------------------------------------------------------------------------------
1 | You can read tutorial https://roytuts.com/spring-cloud-gateway-security-with-jwt-json-web-token/
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-cloud-gateway
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.5.0
22 | 2.6.6
23 |
24 |
25 |
26 |
27 | org.springframework.cloud
28 | spring-cloud-starter
29 |
30 |
31 |
32 | org.springframework.cloud
33 | spring-cloud-starter-gateway
34 |
35 |
36 |
37 | org.springframework.cloud
38 | spring-cloud-starter-netflix-eureka-client
39 |
40 |
41 |
42 | javax.xml.bind
43 | jaxb-api
44 |
45 |
46 |
47 | io.jsonwebtoken
48 | jjwt
49 | 0.9.1
50 |
51 |
52 |
53 |
54 |
55 |
56 | org.springframework.cloud
57 | spring-cloud-dependencies
58 | 2020.0.2
59 | 2021.0.1
60 | pom
61 | import
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | org.springframework.boot
70 | spring-boot-maven-plugin
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/CloudGatewayApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6 |
7 | @EnableEurekaClient
8 | @SpringBootApplication
9 | public class CloudGatewayApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(CloudGatewayApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/config/GatewayConfig.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway.config;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.cloud.gateway.route.RouteLocator;
5 | import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 |
9 | import com.roytuts.spring.boot.cloud.gateway.filter.JwtAuthenticationFilter;
10 |
11 | @Configuration
12 | public class GatewayConfig {
13 |
14 | @Autowired
15 | private JwtAuthenticationFilter filter;
16 |
17 | @Bean
18 | public RouteLocator routes(RouteLocatorBuilder builder) {
19 | return builder.routes().route("auth", r -> r.path("/auth/**").filters(f -> f.filter(filter)).uri("lb://auth"))
20 | .route("alert", r -> r.path("/alert/**").filters(f -> f.filter(filter)).uri("lb://alert"))
21 | .route("echo", r -> r.path("/echo/**").filters(f -> f.filter(filter)).uri("lb://echo"))
22 | .route("hello", r -> r.path("/hello/**").filters(f -> f.filter(filter)).uri("lb://hello")).build();
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/exception/JwtTokenMalformedException.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway.exception;
2 |
3 | import javax.naming.AuthenticationException;
4 |
5 | public class JwtTokenMalformedException extends AuthenticationException {
6 |
7 | private static final long serialVersionUID = 1L;
8 |
9 | public JwtTokenMalformedException(String msg) {
10 | super(msg);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/exception/JwtTokenMissingException.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway.exception;
2 |
3 | import javax.naming.AuthenticationException;
4 |
5 | public class JwtTokenMissingException extends AuthenticationException {
6 |
7 | private static final long serialVersionUID = 1L;
8 |
9 | public JwtTokenMissingException(String msg) {
10 | super(msg);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/filter/JwtAuthenticationFilter.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway.filter;
2 |
3 | import java.util.List;
4 | import java.util.function.Predicate;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.cloud.gateway.filter.GatewayFilter;
8 | import org.springframework.cloud.gateway.filter.GatewayFilterChain;
9 | import org.springframework.http.HttpStatus;
10 | import org.springframework.http.server.reactive.ServerHttpRequest;
11 | import org.springframework.http.server.reactive.ServerHttpResponse;
12 | import org.springframework.stereotype.Component;
13 | import org.springframework.web.server.ServerWebExchange;
14 |
15 | import com.roytuts.spring.boot.cloud.gateway.exception.JwtTokenMalformedException;
16 | import com.roytuts.spring.boot.cloud.gateway.exception.JwtTokenMissingException;
17 | import com.roytuts.spring.boot.cloud.gateway.util.JwtUtil;
18 |
19 | import io.jsonwebtoken.Claims;
20 | import reactor.core.publisher.Mono;
21 |
22 | @Component
23 | public class JwtAuthenticationFilter implements GatewayFilter {
24 |
25 | @Autowired
26 | private JwtUtil jwtUtil;
27 |
28 | @Override
29 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
30 | ServerHttpRequest request = (ServerHttpRequest) exchange.getRequest();
31 |
32 | final List apiEndpoints = List.of("/register", "/login");
33 |
34 | Predicate isApiSecured = r -> apiEndpoints.stream()
35 | .noneMatch(uri -> r.getURI().getPath().contains(uri));
36 |
37 | if (isApiSecured.test(request)) {
38 | if (!request.getHeaders().containsKey("Authorization")) {
39 | ServerHttpResponse response = exchange.getResponse();
40 | response.setStatusCode(HttpStatus.UNAUTHORIZED);
41 |
42 | return response.setComplete();
43 | }
44 |
45 | final String token = request.getHeaders().getOrEmpty("Authorization").get(0);
46 |
47 | try {
48 | jwtUtil.validateToken(token);
49 | } catch (JwtTokenMalformedException | JwtTokenMissingException e) {
50 | // e.printStackTrace();
51 |
52 | ServerHttpResponse response = exchange.getResponse();
53 | response.setStatusCode(HttpStatus.BAD_REQUEST);
54 |
55 | return response.setComplete();
56 | }
57 |
58 | Claims claims = jwtUtil.getClaims(token);
59 | exchange.getRequest().mutate().header("id", String.valueOf(claims.get("id"))).build();
60 | }
61 |
62 | return chain.filter(exchange);
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/java/com/roytuts/spring/boot/cloud/gateway/util/JwtUtil.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.cloud.gateway.util;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Component;
5 |
6 | import com.roytuts.spring.boot.cloud.gateway.exception.JwtTokenMalformedException;
7 | import com.roytuts.spring.boot.cloud.gateway.exception.JwtTokenMissingException;
8 |
9 | import io.jsonwebtoken.Claims;
10 | import io.jsonwebtoken.ExpiredJwtException;
11 | import io.jsonwebtoken.Jwts;
12 | import io.jsonwebtoken.MalformedJwtException;
13 | import io.jsonwebtoken.SignatureException;
14 | import io.jsonwebtoken.UnsupportedJwtException;
15 |
16 | @Component
17 | public class JwtUtil {
18 |
19 | @Value("${jwt.secret}")
20 | private String jwtSecret;
21 |
22 | public Claims getClaims(final String token) {
23 | try {
24 | Claims body = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody();
25 | return body;
26 | } catch (Exception e) {
27 | System.out.println(e.getMessage() + " => " + e);
28 | }
29 | return null;
30 | }
31 |
32 | public void validateToken(final String token) throws JwtTokenMalformedException, JwtTokenMissingException {
33 | try {
34 | Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token);
35 | } catch (SignatureException ex) {
36 | throw new JwtTokenMalformedException("Invalid JWT signature");
37 | } catch (MalformedJwtException ex) {
38 | throw new JwtTokenMalformedException("Invalid JWT token");
39 | } catch (ExpiredJwtException ex) {
40 | throw new JwtTokenMalformedException("Expired JWT token");
41 | } catch (UnsupportedJwtException ex) {
42 | throw new JwtTokenMalformedException("Unsupported JWT token");
43 | } catch (IllegalArgumentException ex) {
44 | throw new JwtTokenMissingException("JWT claims string is empty.");
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-cloud-gateway/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | eureka.client.service-url.default-zone=http://localhost:8761/eureka
2 |
3 | eureka.instance.prefer-ip-address=true
4 | eureka.instance.hostname=localhost
5 |
6 | #secret key - should be encrypted
7 | jwt.secret=secretkey
8 | #ignore null fields in json
9 | spring.jackson.default-property-inclusion=NON_NULL
10 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-alert/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-alert
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.5.0
22 | 2.6.6
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.cloud
33 | spring-cloud-starter-netflix-eureka-client
34 |
35 |
36 |
37 |
38 |
39 |
40 | org.springframework.cloud
41 | spring-cloud-dependencies
42 | 2020.0.2
43 | 2021.0.1
44 | pom
45 | import
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-maven-plugin
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-alert/src/main/java/com/roytuts/spring/boot/microservice/alert/AlertServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.alert;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @SpringBootApplication
13 | public class AlertServiceApp {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(AlertServiceApp.class, args);
17 | }
18 |
19 | @GetMapping("/alert/{msg}")
20 | public ResponseEntity echo(@PathVariable String msg) {
21 | return new ResponseEntity("Your alert message, " + msg, HttpStatus.OK);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-alert/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=alert
2 | #server.port=0
3 | server.port=7000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 |
8 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
9 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-auth-service
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.5.0
22 | 2.6.6
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.cloud
33 | spring-cloud-starter-netflix-eureka-client
34 |
35 |
36 |
37 | javax.xml.bind
38 | jaxb-api
39 |
40 |
41 |
42 | io.jsonwebtoken
43 | jjwt
44 | 0.9.1
45 |
46 |
47 |
48 |
49 |
50 |
51 | org.springframework.cloud
52 | spring-cloud-dependencies
53 | 2020.0.2
54 | 2021.0.1
55 | pom
56 | import
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | org.springframework.boot
65 | spring-boot-maven-plugin
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/src/main/java/com/roytuts/spring/boot/auth/service/AuthServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.auth.service;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class AuthServiceApp {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(AuthServiceApp.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/src/main/java/com/roytuts/spring/boot/auth/service/exception/JwtTokenMalformedException.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.auth.service.exception;
2 |
3 | import javax.naming.AuthenticationException;
4 |
5 | public class JwtTokenMalformedException extends AuthenticationException {
6 |
7 | private static final long serialVersionUID = 1L;
8 |
9 | public JwtTokenMalformedException(String msg) {
10 | super(msg);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/src/main/java/com/roytuts/spring/boot/auth/service/exception/JwtTokenMissingException.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.auth.service.exception;
2 |
3 | import javax.naming.AuthenticationException;
4 |
5 | public class JwtTokenMissingException extends AuthenticationException {
6 |
7 | private static final long serialVersionUID = 1L;
8 |
9 | public JwtTokenMissingException(String msg) {
10 | super(msg);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/src/main/java/com/roytuts/spring/boot/auth/service/rest/controller/AuthRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.auth.service.rest.controller;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.http.HttpStatus;
5 | import org.springframework.http.ResponseEntity;
6 | import org.springframework.web.bind.annotation.PostMapping;
7 | import org.springframework.web.bind.annotation.RequestBody;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | import com.roytuts.spring.boot.auth.service.util.JwtUtil;
11 |
12 | @RestController
13 | public class AuthRestController {
14 |
15 | @Autowired
16 | private JwtUtil jwtUtil;
17 |
18 | @PostMapping("/auth/login")
19 | public ResponseEntity login(@RequestBody String userName) {
20 | String token = jwtUtil.generateToken(userName);
21 |
22 | return new ResponseEntity(token, HttpStatus.OK);
23 | }
24 |
25 | @PostMapping("/auth/register")
26 | public ResponseEntity register(@RequestBody String userName) {
27 | // Persist user to some persistent storage
28 | System.out.println("Info saved...");
29 |
30 | return new ResponseEntity("Registered", HttpStatus.OK);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/src/main/java/com/roytuts/spring/boot/auth/service/util/JwtUtil.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.auth.service.util;
2 |
3 | import java.util.Date;
4 |
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.stereotype.Component;
7 |
8 | import com.roytuts.spring.boot.auth.service.exception.JwtTokenMalformedException;
9 | import com.roytuts.spring.boot.auth.service.exception.JwtTokenMissingException;
10 |
11 | import io.jsonwebtoken.Claims;
12 | import io.jsonwebtoken.ExpiredJwtException;
13 | import io.jsonwebtoken.Jwts;
14 | import io.jsonwebtoken.MalformedJwtException;
15 | import io.jsonwebtoken.SignatureAlgorithm;
16 | import io.jsonwebtoken.SignatureException;
17 | import io.jsonwebtoken.UnsupportedJwtException;
18 |
19 | @Component
20 | public class JwtUtil {
21 |
22 | @Value("${jwt.secret}")
23 | private String jwtSecret;
24 |
25 | @Value("${jwt.token.validity}")
26 | private long tokenValidity;
27 |
28 | public Claims getClaims(final String token) {
29 | try {
30 | Claims body = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody();
31 | return body;
32 | } catch (Exception e) {
33 | System.out.println(e.getMessage() + " => " + e);
34 | }
35 | return null;
36 | }
37 |
38 | public String generateToken(String id) {
39 | Claims claims = Jwts.claims().setSubject(id);
40 | long nowMillis = System.currentTimeMillis();
41 | long expMillis = nowMillis + tokenValidity;
42 | Date exp = new Date(expMillis);
43 | return Jwts.builder().setClaims(claims).setIssuedAt(new Date(nowMillis)).setExpiration(exp)
44 | .signWith(SignatureAlgorithm.HS512, jwtSecret).compact();
45 | }
46 |
47 | public void validateToken(final String token) throws JwtTokenMalformedException, JwtTokenMissingException {
48 | try {
49 | Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token);
50 | } catch (SignatureException ex) {
51 | throw new JwtTokenMalformedException("Invalid JWT signature");
52 | } catch (MalformedJwtException ex) {
53 | throw new JwtTokenMalformedException("Invalid JWT token");
54 | } catch (ExpiredJwtException ex) {
55 | throw new JwtTokenMalformedException("Expired JWT token");
56 | } catch (UnsupportedJwtException ex) {
57 | throw new JwtTokenMalformedException("Unsupported JWT token");
58 | } catch (IllegalArgumentException ex) {
59 | throw new JwtTokenMissingException("JWT claims string is empty.");
60 | }
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-auth/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=auth
2 | #server.port=0
3 | server.port=6000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 |
8 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
9 |
10 | #secret key - should be encrypted
11 | jwt.secret=secretkey
12 | #3 minutes validity
13 | jwt.token.validity=180000
14 | #ignore null fields in json
15 | spring.jackson.default-property-inclusion=NON_NULL
16 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-echo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-echo
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.5.0
22 | 2.6.6
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.cloud
33 | spring-cloud-starter-netflix-eureka-client
34 |
35 |
36 |
37 |
38 |
39 |
40 | org.springframework.cloud
41 | spring-cloud-dependencies
42 | 2020.0.2
43 | 2021.0.1
44 | pom
45 | import
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-maven-plugin
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-echo/src/main/java/com/roytuts/spring/boot/microservice/echo/EchoServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.echo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @SpringBootApplication
13 | public class EchoServiceApp {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(EchoServiceApp.class, args);
17 | }
18 |
19 | @GetMapping("/echo/{msg}")
20 | public ResponseEntity echo(@PathVariable String msg) {
21 | return new ResponseEntity("Your message, " + msg, HttpStatus.OK);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-echo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=echo
2 | #server.port=0
3 | server.port=9000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 |
8 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
9 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-eureka/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-eureka
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.5.0
22 | 2.6.6
23 |
24 |
25 |
26 |
27 | org.springframework.cloud
28 | spring-cloud-starter-netflix-eureka-server
29 |
30 |
31 |
32 |
33 |
34 |
35 | org.springframework.cloud
36 | spring-cloud-dependencies
37 | 2020.0.2
38 | 2021.0.1
39 | pom
40 | import
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-maven-plugin
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-eureka/src/main/java/com/roytuts/spring/boot/microservice/eureka/EurekaServerApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.eureka;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 |
7 | @EnableEurekaServer
8 | @SpringBootApplication
9 | public class EurekaServerApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(EurekaServerApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-eureka/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=eureka
2 | server.port=8761
3 |
4 | #eureka.instance.hostname=localhost
5 | eureka.client.register-with-eureka=false
6 | eureka.client.fetch-registry=false
7 |
8 | eureka.instance.prefer-ip-address=true
9 | eureka.instance.hostname=localhost
10 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-hello/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-hello
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.5.0
22 | 2.6.6
23 |
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.cloud
33 | spring-cloud-starter-netflix-eureka-client
34 |
35 |
36 |
37 |
38 |
39 |
40 | org.springframework.cloud
41 | spring-cloud-dependencies
42 | 2020.0.2
43 | 2021.0.1
44 | pom
45 | import
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-maven-plugin
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-hello/src/main/java/com/roytuts/spring/boot/microservice/hello/HelloServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.hello;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @SpringBootApplication
13 | public class HelloServiceApp {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(HelloServiceApp.class, args);
17 | }
18 |
19 | @GetMapping("/hello/{name}")
20 | public ResponseEntity hello(@PathVariable String name) {
21 | return new ResponseEntity("Hello, " + name, HttpStatus.OK);
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/spring-cloud-gateway-security-jwt/spring-boot-microservice-hello/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=hello
2 | #server.port=0
3 | server.port=8000
4 |
5 | eureka.instance.prefer-ip-address=true
6 | eureka.instance.hostname=localhost
7 |
8 | eureka.client.service-url.default-zone=http://localhost:8761/eureka/
9 |
--------------------------------------------------------------------------------
/spring-cloud-gateway/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.2.6.RELEASE'
4 | }
5 | repositories {
6 | mavenLocal()
7 | mavenCentral()
8 | }
9 | dependencies {
10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
11 | }
12 | }
13 |
14 | plugins {
15 | id "io.spring.dependency-management" version "1.0.9.RELEASE"
16 | }
17 |
18 | apply plugin: 'java'
19 | apply plugin: 'org.springframework.boot'
20 |
21 | sourceCompatibility = 12
22 | targetCompatibility = 12
23 |
24 | repositories {
25 | mavenLocal()
26 | mavenCentral()
27 | }
28 |
29 | dependencies {
30 | implementation("org.springframework.cloud:spring-cloud-starter")
31 | implementation("org.springframework.cloud:spring-cloud-starter-gateway")
32 | }
33 |
34 | dependencyManagement {
35 | imports {
36 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR3'
37 | }
38 | }
--------------------------------------------------------------------------------
/spring-cloud-gateway/readme.rst:
--------------------------------------------------------------------------------
1 | Please follow the tutorial https://roytuts.com/how-to-build-spring-cloud-gateway-for-microservices/
2 |
--------------------------------------------------------------------------------
/spring-cloud-gateway/src/main/java/com/roytuts/spring/cloud/gateway/SpringCloudGatewayApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.gateway;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringCloudGatewayApp {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringCloudGatewayApp.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cloud-gateway/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9090
3 |
4 | spring:
5 | cloud:
6 | gateway:
7 | routes:
8 | - id: currency-conversion-service
9 | uri: http://localhost:9100/
10 | predicates:
11 | - Path=/cc-converter/**
12 | filters:
13 | - RewritePath=/cc-converter/from/(?.*)/to/(?.*)/quantity/(?.*), /currency-converter/from/$\{from}/to/$\{to}/quantity/$\{quantity}
14 | - id: forex-service
15 | uri: http://localhost:9000/, http://localhost:9001/
16 | predicates:
17 | - Path=/fx-exchange/**
18 | filters:
19 | - RewritePath=/fx-exchange/from/(?.*)/to/(?.*), /forex-exchange/from/$\{from}/to/$\{to}
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | currency-conversion-service
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | org.springframework.boot
14 | spring-boot-starter-parent
15 | 3.1.5
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.cloud
22 | spring-cloud-dependencies
23 | 2022.0.4
24 | pom
25 | import
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-webflux
34 |
35 |
36 |
37 | org.springframework.cloud
38 | spring-cloud-starter-loadbalancer
39 |
40 |
41 |
42 | org.springframework.cloud
43 | spring-cloud-starter-netflix-eureka-client
44 |
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-maven-plugin
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/src/main/java/com/roytuts/cc/CurrencyConversionApplication.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.cc;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 |
7 | @EnableDiscoveryClient
8 | @SpringBootApplication
9 | public class CurrencyConversionApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(CurrencyConversionApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/src/main/java/com/roytuts/cc/config/ForexConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.cc.config;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | import org.springframework.cloud.client.DefaultServiceInstance;
7 | import org.springframework.cloud.client.ServiceInstance;
8 | import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.Primary;
11 |
12 | import reactor.core.publisher.Flux;
13 |
14 | public class ForexConfiguration {
15 |
16 | @Bean
17 | @Primary
18 | ServiceInstanceListSupplier serviceInstanceListSupplier() {
19 | return new ForexServiceInstanceListSuppler("forex-service");
20 | }
21 |
22 | }
23 |
24 | class ForexServiceInstanceListSuppler implements ServiceInstanceListSupplier {
25 |
26 | private final String serviceId;
27 |
28 | ForexServiceInstanceListSuppler(String serviceId) {
29 | this.serviceId = serviceId;
30 | }
31 |
32 | @Override
33 | public String getServiceId() {
34 | return serviceId;
35 | }
36 |
37 | @Override
38 | public Flux> get() {
39 | return Flux.just(Arrays.asList(new DefaultServiceInstance(serviceId + "1", serviceId, "localhost", 9000, false),
40 | new DefaultServiceInstance(serviceId + "2", serviceId, "localhost", 9001, false)));
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/src/main/java/com/roytuts/cc/config/WebClientConfig.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.cc.config;
2 |
3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced;
4 | import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.web.reactive.function.client.WebClient;
8 |
9 | @Configuration
10 | @LoadBalancerClient(name = "forex-service", configuration = ForexConfiguration.class)
11 | public class WebClientConfig {
12 |
13 | @Bean
14 | @LoadBalanced
15 | WebClient.Builder webClientBuilder() {
16 | return WebClient.builder();
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/src/main/java/com/roytuts/cc/model/CurrencyConversion.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.cc.model;
2 |
3 | public class CurrencyConversion {
4 |
5 | private Integer id;
6 | private String from;
7 | private String to;
8 | private Double rate;
9 | private Integer quantity;
10 | private Double totalAmount;
11 |
12 | public CurrencyConversion() {
13 | }
14 |
15 | public CurrencyConversion(Integer id, String from, String to, Double rate, Integer quantity, Double totalAmount) {
16 | this.id = id;
17 | this.from = from;
18 | this.to = to;
19 | this.rate = rate;
20 | this.quantity = quantity;
21 | this.totalAmount = totalAmount;
22 | }
23 |
24 | public Integer getId() {
25 | return id;
26 | }
27 |
28 | public void setId(Integer id) {
29 | this.id = id;
30 | }
31 |
32 | public String getFrom() {
33 | return from;
34 | }
35 |
36 | public void setFrom(String from) {
37 | this.from = from;
38 | }
39 |
40 | public String getTo() {
41 | return to;
42 | }
43 |
44 | public void setTo(String to) {
45 | this.to = to;
46 | }
47 |
48 | public Double getRate() {
49 | return rate;
50 | }
51 |
52 | public void setRate(Double rate) {
53 | this.rate = rate;
54 | }
55 |
56 | public Integer getQuantity() {
57 | return quantity;
58 | }
59 |
60 | public void setQuantity(Integer quantity) {
61 | this.quantity = quantity;
62 | }
63 |
64 | public Double getTotalAmount() {
65 | return totalAmount;
66 | }
67 |
68 | public void setTotalAmount(Double totalAmount) {
69 | this.totalAmount = totalAmount;
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/src/main/java/com/roytuts/cc/rest/controller/CurrencyConversionRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.cc.rest.controller;
2 |
3 | import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
4 | import org.springframework.web.bind.annotation.GetMapping;
5 | import org.springframework.web.bind.annotation.PathVariable;
6 | import org.springframework.web.bind.annotation.RestController;
7 | import org.springframework.web.reactive.function.client.WebClient;
8 |
9 | import com.roytuts.cc.model.CurrencyConversion;
10 |
11 | import reactor.core.publisher.Mono;
12 |
13 | @RestController
14 | public class CurrencyConversionRestController {
15 |
16 | private final WebClient.Builder loadBalancedWebClientBuilder;
17 | private final ReactorLoadBalancerExchangeFilterFunction lbFunction;
18 |
19 | public CurrencyConversionRestController(WebClient.Builder webClientBuilder,
20 | ReactorLoadBalancerExchangeFilterFunction lbFunction) {
21 | this.loadBalancedWebClientBuilder = webClientBuilder;
22 | this.lbFunction = lbFunction;
23 | }
24 |
25 | @GetMapping("currency-converter/from/{from}/to/{to}/quantity/{quantity}")
26 | public Mono getCurrencyExchange(@PathVariable String from, @PathVariable String to,
27 | @PathVariable Integer quantity) {
28 |
29 | Mono response = WebClient.builder().filter(lbFunction).build().get()
30 | // Mono response =
31 | // loadBalancedWebClientBuilder.build().get()
32 | .uri("http://forex-service/forex-exchange/from/{from}/to/{to}", from, to).retrieve()
33 | .bodyToMono(CurrencyConversion.class)
34 | .map(r -> new CurrencyConversion(quantity, from, to, r.getRate(), quantity, quantity * r.getRate()));
35 |
36 | return response;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/currency-conversion-service/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | application:
3 | name: currency-conversion-service
4 | server:
5 | port: 9100
6 | eureka:
7 | client:
8 | service-url:
9 | default-zone: http://localhost:8761/eureka
10 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/eureka-server-config/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | eureka-server-config
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 19
15 | 19
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 3.1.5
22 |
23 |
24 |
25 |
26 |
27 | org.springframework.cloud
28 | spring-cloud-dependencies
29 | 2022.0.4
30 | pom
31 | import
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.springframework.cloud
39 | spring-cloud-starter-netflix-eureka-server
40 |
41 |
42 |
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-maven-plugin
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/eureka-server-config/src/main/java/com/roytuts/eureka/server/config/com.roytuts.eureka.server.config.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.eureka.server.config;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 |
7 | @EnableEurekaServer
8 | @SpringBootApplication
9 | public class EurekaServerConfigApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(EurekaServerConfigApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/eureka-server-config/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | application:
3 | name: eureka-server-config
4 | server:
5 | port: 8761
6 | eureka:
7 | client:
8 | register-with-eureka: false
9 | fetch-registry: false
10 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex-service/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | forex-service
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 19
15 | 19
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 3.1.5
22 |
23 |
24 |
25 |
26 |
27 | org.springframework.cloud
28 | spring-cloud-dependencies
29 | 2022.0.4
30 | pom
31 | import
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-web
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-data-jpa
45 |
46 |
47 |
48 | org.springframework.cloud
49 | spring-cloud-starter-netflix-eureka-client
50 |
51 |
52 |
53 | com.mysql
54 | mysql-connector-j
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.springframework.boot
62 | spring-boot-maven-plugin
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex-service/src/main/java/com/roytuts/com/roytuts/forex/ForexApplication.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.forex;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 |
7 | @EnableDiscoveryClient
8 | @SpringBootApplication
9 | public class ForexApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(ForexApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex-service/src/main/java/com/roytuts/com/roytuts/forex/entity/ForexValue.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.forex.entity;
2 |
3 | import jakarta.persistence.Column;
4 | import jakarta.persistence.Entity;
5 | import jakarta.persistence.GeneratedValue;
6 | import jakarta.persistence.GenerationType;
7 | import jakarta.persistence.Id;
8 |
9 | @Entity
10 | public class ForexValue {
11 |
12 | @Id
13 | @GeneratedValue(strategy = GenerationType.IDENTITY)
14 | private Integer id;
15 |
16 | @Column(name = "from_curr")
17 | private String from;
18 |
19 | @Column(name = "to_curr")
20 | private String to;
21 |
22 | private Double rate;
23 |
24 | public ForexValue() {
25 | }
26 |
27 | public ForexValue(String from, String to, Double rate) {
28 | this.from = from;
29 | this.to = to;
30 | this.rate = rate;
31 | }
32 |
33 | public Integer getId() {
34 | return id;
35 | }
36 |
37 | public void setId(Integer id) {
38 | this.id = id;
39 | }
40 |
41 | public String getFrom() {
42 | return from;
43 | }
44 |
45 | public void setFrom(String from) {
46 | this.from = from;
47 | }
48 |
49 | public String getTo() {
50 | return to;
51 | }
52 |
53 | public void setTo(String to) {
54 | this.to = to;
55 | }
56 |
57 | public Double getRate() {
58 | return rate;
59 | }
60 |
61 | public void setRate(Double rate) {
62 | this.rate = rate;
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex-service/src/main/java/com/roytuts/com/roytuts/forex/repository/ForexRepository.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.forex.repository;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 |
5 | import com.roytuts.forex.entity.ForexValue;
6 |
7 | public interface ForexRepository extends JpaRepository {
8 |
9 | ForexValue findByFromAndTo(String from, String to);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex-service/src/main/java/com/roytuts/com/roytuts/forex/rest/controller/ForexRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.forex.rest.controller;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.http.HttpStatus;
5 | import org.springframework.http.ResponseEntity;
6 | import org.springframework.web.bind.annotation.GetMapping;
7 | import org.springframework.web.bind.annotation.PathVariable;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | import com.roytuts.forex.entity.ForexValue;
11 | import com.roytuts.forex.repository.ForexRepository;
12 |
13 | @RestController
14 | public class ForexRestController {
15 |
16 | @Autowired
17 | private ForexRepository forexRepository;
18 |
19 | @GetMapping("/forex-exchange/from/{from}/to/{to}")
20 | public ResponseEntity getCurrencyExchange(@PathVariable String from, @PathVariable String to) {
21 | ForexValue forexValue = forexRepository.findByFromAndTo(from, to);
22 |
23 | return new ResponseEntity(forexValue, HttpStatus.OK);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex-service/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | application:
3 | name: forex-service
4 | datasource:
5 | driverClassName: com.mysql.cj.jdbc.Driver
6 | url: jdbc:mysql://localhost:3306/roytuts
7 | username: root
8 | password: root
9 | jpa:
10 | hibernate.ddl-auto: update
11 | generate-ddl: true
12 | show-sql: true
13 | server:
14 | port: 9000
15 | eureka:
16 | client:
17 | service-url:
18 | default-zone: http://localhost:8761/eureka
19 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/forex.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `forex_value` (
2 | `id` INT(10) NOT NULL AUTO_INCREMENT,
3 | `from_curr` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
4 | `rate` DOUBLE NULL DEFAULT NULL,
5 | `to_curr` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
6 | PRIMARY KEY (`id`) USING BTREE
7 | )
8 | COLLATE='utf8mb4_unicode_ci'
9 | ENGINE=InnoDB
10 | AUTO_INCREMENT=4;
11 |
12 | insert into forex_value(from_curr,to_curr,rate)
13 | values('USD','INR',71);
14 | insert into forex_value(from_curr,to_curr,rate)
15 | values('EUR','INR',85);
16 | insert into forex_value(from_curr,to_curr,rate)
17 | values('AUD','INR',39);
18 |
--------------------------------------------------------------------------------
/spring-cloud-load-balancer/readme.rst:
--------------------------------------------------------------------------------
1 | Please follow the tutorial https://roytuts.com/spring-cloud-loadbalancer-with-microservices/
2 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/readme.rst:
--------------------------------------------------------------------------------
1 | You can go through the tutorial https://roytuts.com/spring-cloud-microservices-discovery-with-eureka-on-random-ports/
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-alert/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-alert
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.5
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-netflix-eureka-client
33 |
34 |
35 |
36 |
37 |
38 |
39 | org.springframework.cloud
40 | spring-cloud-dependencies
41 | 2020.0.2
42 | pom
43 | import
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-maven-plugin
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-alert/src/main/java/com/roytuts/spring/boot/microservice/alert/AlertServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.alert;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @SpringBootApplication
13 | public class AlertServiceApp {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(AlertServiceApp.class, args);
17 | }
18 |
19 | @GetMapping("/alert/{msg}")
20 | public ResponseEntity echo(@PathVariable String msg) {
21 | return new ResponseEntity("Your alert message, " + msg, HttpStatus.OK);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-alert/src/main/java/com/roytuts/spring/boot/microservice/alert/ServerPortsRangeConfig.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.alert;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.boot.web.server.WebServerFactoryCustomizer;
5 | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.util.SocketUtils;
8 |
9 | @Configuration
10 | public class ServerPortsRangeConfig implements WebServerFactoryCustomizer {
11 |
12 | @Value("${port.number.min:7000}")
13 | private Integer minPort;
14 |
15 | @Value("${port.number.max:9000}")
16 | private Integer maxPort;
17 |
18 | @Override
19 | public void customize(ConfigurableServletWebServerFactory factory) {
20 | int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
21 | factory.setPort(port);
22 | System.getProperties().put("server.port", port);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-alert/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=alert
2 | server.port=0
3 | #server.port=7000
4 |
5 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-echo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-echo
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.5
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-netflix-eureka-client
33 |
34 |
35 |
36 |
37 |
38 |
39 | org.springframework.cloud
40 | spring-cloud-dependencies
41 | 2020.0.2
42 | pom
43 | import
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-maven-plugin
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-echo/src/main/java/com/roytuts/spring/boot/microservice/echo/EchoServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.echo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @SpringBootApplication
13 | public class EchoServiceApp {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(EchoServiceApp.class, args);
17 | }
18 |
19 | @GetMapping("/echo/{msg}")
20 | public ResponseEntity echo(@PathVariable String msg) {
21 | return new ResponseEntity("Your message, " + msg, HttpStatus.OK);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-echo/src/main/java/com/roytuts/spring/boot/microservice/echo/ServerPortsRangeConfig.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.echo;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.boot.web.server.WebServerFactoryCustomizer;
5 | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.util.SocketUtils;
8 |
9 | @Configuration
10 | public class ServerPortsRangeConfig implements WebServerFactoryCustomizer {
11 |
12 | @Value("${port.number.min:7000}")
13 | private Integer minPort;
14 |
15 | @Value("${port.number.max:9000}")
16 | private Integer maxPort;
17 |
18 | @Override
19 | public void customize(ConfigurableServletWebServerFactory factory) {
20 | int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
21 | factory.setPort(port);
22 | System.getProperties().put("server.port", port);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-echo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=echo
2 | server.port=0
3 | #server.port=9000
4 |
5 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-eureka/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-eureka
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.5
22 |
23 |
24 |
25 |
26 | org.springframework.cloud
27 | spring-cloud-starter-netflix-eureka-server
28 |
29 |
30 |
31 |
32 |
33 |
34 | org.springframework.cloud
35 | spring-cloud-dependencies
36 | 2020.0.2
37 | pom
38 | import
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-maven-plugin
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-eureka/src/main/java/com/roytuts/spring/boot/microservice/eureka/EurekaServerApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.eureka;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 |
7 | @EnableEurekaServer
8 | @SpringBootApplication
9 | public class EurekaServerApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(EurekaServerApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-eureka/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=eureka
2 | server.port=8761
3 |
4 | #eureka.instance.hostname=localhost
5 | eureka.client.register-with-eureka=false
6 | eureka.client.fetch-registry=false
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-hello/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-boot-microservice-hello
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.5
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-netflix-eureka-client
33 |
34 |
35 |
36 |
37 |
38 |
39 | org.springframework.cloud
40 | spring-cloud-dependencies
41 | 2020.0.2
42 | pom
43 | import
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-maven-plugin
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-hello/src/main/java/com/roytuts/spring/boot/microservice/hello/HelloServiceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.hello;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | @RestController
12 | @SpringBootApplication
13 | public class HelloServiceApp {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(HelloServiceApp.class, args);
17 | }
18 |
19 | @GetMapping("/hello/{name}")
20 | public ResponseEntity hello(@PathVariable String name) {
21 | return new ResponseEntity("Hello, " + name, HttpStatus.OK);
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-hello/src/main/java/com/roytuts/spring/boot/microservice/hello/ServerPortsRangeConfig.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.boot.microservice.hello;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.boot.web.server.WebServerFactoryCustomizer;
5 | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.util.SocketUtils;
8 |
9 | @Configuration
10 | public class ServerPortsRangeConfig implements WebServerFactoryCustomizer {
11 |
12 | @Value("${port.number.min:7000}")
13 | private Integer minPort;
14 |
15 | @Value("${port.number.max:9000}")
16 | private Integer maxPort;
17 |
18 | @Override
19 | public void customize(ConfigurableServletWebServerFactory factory) {
20 | int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
21 | factory.setPort(port);
22 | System.getProperties().put("server.port", port);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-random-ports/spring-boot-microservice-hello/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=hello
2 | server.port=0
3 | #server.port=8000
4 |
5 | eureka.client.service-url.default-zone=http://localhost:8761/eureka/
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/readme.rst:
--------------------------------------------------------------------------------
1 | Please follow the tutorial https://roytuts.com/how-to-trace-microservices-logs-with-spring-cloud-sleuth-zipkin/
2 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-celsius-to-fahrenheit-converter/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.3.3.RELEASE'
4 | }
5 |
6 | repositories {
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
12 | }
13 | }
14 |
15 | plugins {
16 | id 'java-library'
17 | id "io.spring.dependency-management" version "1.0.10.RELEASE"
18 | }
19 |
20 | sourceCompatibility = 12
21 | targetCompatibility = 12
22 |
23 | repositories {
24 | mavenCentral()
25 | }
26 |
27 | dependencies {
28 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
29 | implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
30 | implementation("org.springframework.cloud:spring-cloud-starter-zipkin")
31 | }
32 |
33 | dependencyManagement {
34 | imports {
35 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7'
36 | }
37 | }
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-celsius-to-fahrenheit-converter/src/main/java/com/roytuts/spring/celsius/to/fahrenheit/converter/CelsiusToFahrenheitConverterApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.celsius.to.fahrenheit.converter;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.http.HttpStatus;
9 | import org.springframework.http.ResponseEntity;
10 | import org.springframework.web.bind.annotation.GetMapping;
11 | import org.springframework.web.bind.annotation.PathVariable;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import brave.sampler.Sampler;
15 |
16 | @RestController
17 | @SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class })
18 | public class CelsiusToFahrenheitConverterApp {
19 |
20 | private static final Logger LOGGER = LoggerFactory.getLogger(CelsiusToFahrenheitConverterApp.class.getName());
21 |
22 | public static void main(String[] args) {
23 | SpringApplication.run(CelsiusToFahrenheitConverterApp.class, args);
24 | }
25 |
26 | @GetMapping("/celsius/{c}/to/fahrenheit/")
27 | public ResponseEntity convertCelsiusToFahrenheit(@PathVariable("c") float celsius) {
28 | float f = (float) (32 + 1.8 * celsius);
29 |
30 | LOGGER.info("Celsius to Fahrenheit: {} -> {}", celsius, f);
31 |
32 | return new ResponseEntity(f, HttpStatus.OK);
33 | }
34 |
35 | @Bean
36 | public Sampler defaultSampler() {
37 | return Sampler.ALWAYS_SAMPLE;
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-celsius-to-fahrenheit-converter/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=celsius-to-fahrenheit-converter
2 | server.port=8000
3 |
4 | eureka.client.service-url.default-zone=http://localhost:8761/eureka
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-eureka-server/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.3.3.RELEASE'
4 | }
5 |
6 | repositories {
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
12 | }
13 | }
14 |
15 | plugins {
16 | id 'java-library'
17 | id "io.spring.dependency-management" version "1.0.10.RELEASE"
18 | }
19 |
20 | sourceCompatibility = 12
21 | targetCompatibility = 12
22 |
23 | repositories {
24 | mavenCentral()
25 | }
26 |
27 | dependencies {
28 | implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
29 | }
30 |
31 | dependencyManagement {
32 | imports {
33 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7'
34 | }
35 | }
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-eureka-server/src/main/java/com/roytuts/spring/microservices/eureka/server/EurekaServerApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.microservices.eureka.server;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 |
7 | @EnableEurekaServer
8 | @SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class })
9 | public class EurekaServerApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(EurekaServerApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-eureka-server/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=eureka-server
2 | server.port=8761
3 | eureka.client.register-with-eureka=false
4 | eureka.client.fetch-registry=false
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-fahrenheit-to-celsius-converter/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.3.3.RELEASE'
4 | }
5 |
6 | repositories {
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
12 | }
13 | }
14 |
15 | plugins {
16 | id 'java-library'
17 | id "io.spring.dependency-management" version "1.0.10.RELEASE"
18 | }
19 |
20 | sourceCompatibility = 12
21 | targetCompatibility = 12
22 |
23 | repositories {
24 | mavenCentral()
25 | }
26 |
27 | dependencies {
28 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
29 | implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
30 | implementation("org.springframework.cloud:spring-cloud-starter-zipkin")
31 | }
32 |
33 | dependencyManagement {
34 | imports {
35 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7'
36 | }
37 | }
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-fahrenheit-to-celsius-converter/src/main/java/com/roytuts/spring/fahrenheit/to/celsius/converter/FahrenheitToCelsiusConverterApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.fahrenheit.to.celsius.converter;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.http.HttpStatus;
9 | import org.springframework.http.ResponseEntity;
10 | import org.springframework.web.bind.annotation.GetMapping;
11 | import org.springframework.web.bind.annotation.PathVariable;
12 | import org.springframework.web.bind.annotation.RestController;
13 | import org.springframework.web.client.RestTemplate;
14 |
15 | import brave.sampler.Sampler;
16 |
17 | @RestController
18 | @SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class })
19 | public class FahrenheitToCelsiusConverterApp {
20 |
21 | private static final Logger LOGGER = LoggerFactory.getLogger(FahrenheitToCelsiusConverterApp.class.getName());
22 |
23 | public static void main(String[] args) {
24 | SpringApplication.run(FahrenheitToCelsiusConverterApp.class, args);
25 | }
26 |
27 | @GetMapping("/fahrenheit/{f}/to/celsius/")
28 | public ResponseEntity convertFahrenheitToCelsius(@PathVariable("f") float fahrenheit) {
29 | float c = (float) ((fahrenheit - 32) / 1.8);
30 |
31 | LOGGER.info("Fahrenheit to Celsius: {} -> {}", fahrenheit, c);
32 |
33 | return new ResponseEntity(c, HttpStatus.OK);
34 | }
35 |
36 | @Bean
37 | public Sampler defaultSampler() {
38 | return Sampler.ALWAYS_SAMPLE;
39 | }
40 |
41 | @Bean
42 | public RestTemplate restTemplate() {
43 | return new RestTemplate();
44 | }
45 |
46 | @GetMapping("/call/celsius/{c}/to/fahrenheit/")
47 | public ResponseEntity callCelsiusToFahrenheit(@PathVariable("c") float celsius) {
48 | LOGGER.info("callCelsiusToFahrenheit()");
49 |
50 | return restTemplate().getForEntity("http://localhost:8000/celsius/" + celsius + "/to/fahrenheit/", Float.class);
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/spring-cloud-microservices-trace-logs-sleuth-zipkin/spring-microservices-fahrenheit-to-celsius-converter/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=fahrenheit-to-celsius-converter
2 | server.port=9000
3 |
4 | eureka.client.service-url.default-zone=http://localhost:8761/eureka
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/readme.rst:
--------------------------------------------------------------------------------
1 | Please follow the tutorial https://roytuts.com/event-driven-streaming-using-spring-cloud-stream-and-apache-kafka/
2 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.2.4.RELEASE'
4 | }
5 | repositories {
6 | mavenCentral()
7 | }
8 | dependencies {
9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10 | }
11 | }
12 |
13 | plugins {
14 | id 'java-library'
15 | id 'org.springframework.boot' version '2.2.4.RELEASE'
16 | }
17 |
18 | sourceCompatibility = 12
19 | targetCompatibility = 12
20 |
21 | repositories {
22 | mavenCentral()
23 | }
24 |
25 | dependencies {
26 | implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}")
27 | implementation("org.springframework.cloud:spring-cloud-starter-stream-kafka:3.0.2.RELEASE")
28 | }
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/src/main/java/com/roytuts/spring/cloud/stream/kafka/ordercheck/OrderCheckApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.kafka.ordercheck;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 | import org.springframework.cloud.stream.annotation.EnableBinding;
8 |
9 | import com.roytuts.spring.cloud.stream.kafka.ordercheck.processor.OrderProcessor;
10 |
11 | @SpringBootApplication
12 | @EnableBinding(OrderProcessor.class)
13 | public class OrderCheckApp {
14 |
15 | public static final Logger LOG = LoggerFactory.getLogger(OrderCheckApp.class);
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(OrderCheckApp.class, args);
19 |
20 | LOG.info("The Order Check Application has started...");
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/src/main/java/com/roytuts/spring/cloud/stream/kafka/ordercheck/checker/OrderChecker.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.kafka.ordercheck.checker;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.cloud.stream.annotation.StreamListener;
10 | import org.springframework.messaging.Message;
11 | import org.springframework.messaging.support.MessageBuilder;
12 | import org.springframework.stereotype.Component;
13 |
14 | import com.roytuts.spring.cloud.stream.kafka.ordercheck.enums.Status;
15 | import com.roytuts.spring.cloud.stream.kafka.ordercheck.model.Order;
16 | import com.roytuts.spring.cloud.stream.kafka.ordercheck.processor.OrderProcessor;
17 |
18 | @Component
19 | public class OrderChecker {
20 |
21 | public static final Logger LOG = LoggerFactory.getLogger(OrderChecker.class);
22 |
23 | private static final List DISCARDED_ITEMS = Arrays.asList("Watch", "Clock");
24 |
25 | @Autowired
26 | private OrderProcessor processor;
27 |
28 | @StreamListener(OrderProcessor.ORDERS_IN)
29 | public void checkAndSortOrders(Order order) {
30 | LOG.info("{} {} for {} for {}", order.getStatus(), order.getUuid(), order.getItem(), order.getName());
31 |
32 | if (DISCARDED_ITEMS.contains(order.getItem())) {
33 | order.setStatus(Status.UNDELIVERED.name());
34 | processor.undelivered().send(message(order));
35 | } else {
36 | order.setStatus(Status.DELIVERED.name());
37 | processor.delivered().send(message(order));
38 | }
39 |
40 | LOG.info("{} {} for {} for {}", order.getStatus(), order.getUuid(), order.getItem(), order.getName());
41 | }
42 |
43 | private static final Message message(T val) {
44 | return MessageBuilder.withPayload(val).build();
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/src/main/java/com/roytuts/spring/cloud/stream/kafka/ordercheck/enums/Status.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.kafka.ordercheck.enums;
2 |
3 | public enum Status {
4 |
5 | DELIVERED, UNDELIVERED, PENDING, REJECTED
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/src/main/java/com/roytuts/spring/cloud/stream/kafka/ordercheck/model/Order.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.kafka.ordercheck.model;
2 |
3 | import com.roytuts.spring.cloud.stream.kafka.ordercheck.enums.Status;
4 |
5 | public class Order {
6 |
7 | private String uuid, name, item, status;
8 |
9 | public Order() {
10 | this.setStatus(Status.PENDING.name());
11 | }
12 |
13 | public Order(String uuid, String name, String item) {
14 | this.uuid = uuid;
15 | this.name = name;
16 | this.item = item;
17 | this.setStatus(Status.PENDING.name());
18 | }
19 |
20 | public String getUuid() {
21 | return uuid;
22 | }
23 |
24 | public void setUuid(String uuid) {
25 | this.uuid = uuid;
26 | }
27 |
28 | public String getName() {
29 | return name;
30 | }
31 |
32 | public void setName(String name) {
33 | this.name = name;
34 | }
35 |
36 | public String getItem() {
37 | return item;
38 | }
39 |
40 | public void setItem(String item) {
41 | this.item = item;
42 | }
43 |
44 | public String getStatus() {
45 | return status;
46 | }
47 |
48 | public void setStatus(String status) {
49 | if (status.equals(Status.DELIVERED.name()) || status.equals(Status.UNDELIVERED.name())
50 | || status.equals(Status.PENDING.name()) || status.equals(Status.REJECTED.name())) {
51 | this.status = status;
52 | } else {
53 | throw new IllegalArgumentException("Cannot set the Order's status to " + status);
54 | }
55 | }
56 |
57 | @Override
58 | public String toString() {
59 | return "Order [uuid=" + uuid + ", name=" + name + ", item=" + item + ", status=" + status + "]";
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/src/main/java/com/roytuts/spring/cloud/stream/kafka/ordercheck/processor/OrderProcessor.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.kafka.ordercheck.processor;
2 |
3 | import org.springframework.cloud.stream.annotation.Input;
4 | import org.springframework.cloud.stream.annotation.Output;
5 | import org.springframework.messaging.MessageChannel;
6 | import org.springframework.messaging.SubscribableChannel;
7 | import org.springframework.stereotype.Component;
8 |
9 | @Component
10 | public interface OrderProcessor {
11 |
12 | String ORDERS_IN = "output";
13 | String DELIVERED_OUT = "delivered";
14 | String UNDELIVERED_OUT = "undelivered";
15 |
16 | @Input(ORDERS_IN)
17 | SubscribableChannel sourceOfOrders();
18 |
19 | @Output(DELIVERED_OUT)
20 | MessageChannel delivered();
21 |
22 | @Output(UNDELIVERED_OUT)
23 | MessageChannel undelivered();
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordercheck/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=8081
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordersource/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.2.4.RELEASE'
4 | }
5 | repositories {
6 | mavenCentral()
7 | }
8 | dependencies {
9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10 | }
11 | }
12 |
13 | plugins {
14 | id 'java-library'
15 | id 'org.springframework.boot' version '2.2.4.RELEASE'
16 | }
17 |
18 | sourceCompatibility = 12
19 | targetCompatibility = 12
20 |
21 | repositories {
22 | mavenCentral()
23 | }
24 |
25 | dependencies {
26 | implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}")
27 | implementation("org.springframework.cloud:spring-cloud-starter-stream-kafka:3.0.2.RELEASE")
28 | }
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordersource/src/main/java/com/roytuts/spring/cloud/stream/using/kafka/OrderSourceApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.using.kafka;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Random;
6 | import java.util.UUID;
7 | import java.util.function.Supplier;
8 |
9 | import org.slf4j.Logger;
10 | import org.slf4j.LoggerFactory;
11 | import org.springframework.boot.SpringApplication;
12 | import org.springframework.boot.autoconfigure.SpringBootApplication;
13 | import org.springframework.cloud.stream.annotation.EnableBinding;
14 | import org.springframework.cloud.stream.messaging.Source;
15 | import org.springframework.context.annotation.Bean;
16 | import org.springframework.integration.annotation.InboundChannelAdapter;
17 |
18 | import com.roytuts.spring.cloud.stream.using.kafka.model.Order;
19 |
20 | @SpringBootApplication
21 | @EnableBinding(Source.class)
22 | public class OrderSourceApp {
23 |
24 | private static final Logger LOG = LoggerFactory.getLogger(OrderSourceApp.class);
25 |
26 | private List names = Arrays.asList("Donald", "Theresa", "Vladimir", "Angela", "Emmanuel", "Shinzō",
27 | "Jacinda", "Kim");
28 |
29 | private List items = Arrays.asList("Mobile", "Tab", "Desktop", "Laptop", "Head Phone", "Adapter", "Charger",
30 | "USB Cable", "Watch", "Clock");
31 |
32 | public static void main(String[] args) {
33 | SpringApplication.run(OrderSourceApp.class, args/* "--spring.cloud.stream.function.definition=supply" */);
34 | }
35 |
36 | @Bean
37 | @InboundChannelAdapter(channel = Source.OUTPUT)
38 | public Supplier supply() {
39 | return () -> {
40 | String rName = names.get(new Random().nextInt(names.size()));
41 | String rItem = items.get(new Random().nextInt(items.size()));
42 | Order order = new Order(UUID.randomUUID().toString(), rName, rItem);
43 |
44 | LOG.info("{} {} for {} for {}", order.getStatus(), order.getUuid(), order.getItem(), order.getName());
45 |
46 | return order;
47 | };
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordersource/src/main/java/com/roytuts/spring/cloud/stream/using/kafka/enums/Status.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.using.kafka.enums;
2 |
3 | public enum Status {
4 |
5 | DELIVERED, UNDELIVERED, PENDING, REJECTED
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/spring-cloud-stream-kafka/spring-cloud-stream-kafka-ordersource/src/main/java/com/roytuts/spring/cloud/stream/using/kafka/model/Order.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.cloud.stream.using.kafka.model;
2 |
3 | import com.roytuts.spring.cloud.stream.using.kafka.enums.Status;
4 |
5 | public class Order {
6 |
7 | private String uuid, name, item, status;
8 |
9 | public Order() {
10 | this.setStatus(Status.PENDING.name());
11 | }
12 |
13 | public Order(String uuid, String name, String item) {
14 | this.uuid = uuid;
15 | this.name = name;
16 | this.item = item;
17 | this.setStatus(Status.PENDING.name());
18 | }
19 |
20 | public String getUuid() {
21 | return uuid;
22 | }
23 |
24 | public void setUuid(String uuid) {
25 | this.uuid = uuid;
26 | }
27 |
28 | public String getName() {
29 | return name;
30 | }
31 |
32 | public void setName(String name) {
33 | this.name = name;
34 | }
35 |
36 | public String getItem() {
37 | return item;
38 | }
39 |
40 | public void setItem(String item) {
41 | this.item = item;
42 | }
43 |
44 | public String getStatus() {
45 | return status;
46 | }
47 |
48 | public void setStatus(String status) {
49 | if (status.equals(Status.DELIVERED.name()) || status.equals(Status.UNDELIVERED.name())
50 | || status.equals(Status.PENDING.name()) || status.equals(Status.REJECTED.name())) {
51 | this.status = status;
52 | } else {
53 | throw new IllegalArgumentException("Cannot set the Order's status to " + status);
54 | }
55 | }
56 |
57 | @Override
58 | public String toString() {
59 | return "Order [uuid=" + uuid + ", name=" + name + ", item=" + item + ", status=" + status + "]";
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/config/external.properties:
--------------------------------------------------------------------------------
1 | msg=Hello Developer
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/readme.rst:
--------------------------------------------------------------------------------
1 | You can read tutorial https://roytuts.com/spring-centralized-runtime-properties-configuration-without-refreshing-the-client/
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-client/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.4.3'
4 | }
5 |
6 | repositories {
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
12 | }
13 | }
14 |
15 | plugins {
16 | id 'java-library'
17 | id 'org.springframework.boot' version "${springBootVersion}"
18 | id "io.spring.dependency-management" version "1.0.11.RELEASE"
19 | }
20 |
21 | sourceCompatibility = 12
22 | targetCompatibility = 12
23 |
24 | repositories {
25 | mavenCentral()
26 | }
27 |
28 | dependencies {
29 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
30 | implementation("org.springframework.cloud:spring-cloud-starter-config:3.0.2")
31 | implementation("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
32 |
33 | //required for jdk 9 or above
34 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359')
35 | }
36 |
37 | dependencyManagement {
38 | imports {
39 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.0'
40 | }
41 | }
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-dynamic-runtime-configurations-client
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.3
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 | org.springframework.cloud
32 | spring-cloud-starter-config
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-actuator
38 |
39 |
40 |
41 | javax.xml.bind
42 | jaxb-api
43 | runtime
44 |
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.cloud
51 | spring-cloud-dependencies
52 | 2020.0.0
53 | pom
54 | import
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | org.springframework.boot
63 | spring-boot-maven-plugin
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-client/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/client/SpringDynamicRuntimeConfigClientApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.client;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringDynamicRuntimeConfigClientApp {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringDynamicRuntimeConfigClientApp.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-client/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/client/rest/controller/SpringRestController.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.client.rest.controller;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.cloud.context.config.annotation.RefreshScope;
5 | import org.springframework.http.HttpStatus;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | @RefreshScope
11 | @RestController
12 | public class SpringRestController {
13 |
14 | @Value("${msg:Hey}")
15 | private String msg;
16 |
17 | @GetMapping("/msg")
18 | public ResponseEntity greeting() {
19 | return new ResponseEntity(msg, HttpStatus.OK);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-client/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=external
2 | management.endpoints.web.exposure.include=*
3 | spring.config.import=optional:configserver:http://localhost:8888/
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-server/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | springBootVersion = '2.4.3'
4 | }
5 |
6 | repositories {
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
12 | }
13 | }
14 |
15 | plugins {
16 | id 'java-library'
17 | id 'org.springframework.boot' version "${springBootVersion}"
18 | id "io.spring.dependency-management" version "1.0.11.RELEASE"
19 | }
20 |
21 | sourceCompatibility = 12
22 | targetCompatibility = 12
23 |
24 | repositories {
25 | mavenCentral()
26 | }
27 |
28 | dependencies {
29 | implementation("org.springframework.cloud:spring-cloud-config-server:3.0.2")
30 |
31 | //required for jdk 9 or above
32 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359')
33 | }
34 |
35 | dependencyManagement {
36 | imports {
37 | mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.0'
38 | }
39 | }
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.roytuts
9 | spring-dynamic-runtime-configurations-server
10 | 0.0.1-SNAPSHOT
11 |
12 |
13 | UTF-8
14 | 12
15 | 12
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-parent
21 | 2.4.3
22 |
23 |
24 |
25 |
26 | org.springframework.cloud
27 | spring-cloud-config-server
28 |
29 |
30 |
31 | javax.xml.bind
32 | jaxb-api
33 | runtime
34 |
35 |
36 |
37 |
38 |
39 |
40 | org.springframework.cloud
41 | spring-cloud-dependencies
42 | 2020.0.0
43 | pom
44 | import
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-maven-plugin
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-server/src/main/java/com/roytuts/spring/dynamic/runtime/configurations/server/SpringDynamicRuntimeConfigServerApp.java:
--------------------------------------------------------------------------------
1 | package com.roytuts.spring.dynamic.runtime.configurations.server;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.config.server.EnableConfigServer;
6 |
7 | @EnableConfigServer
8 | @SpringBootApplication
9 | public class SpringDynamicRuntimeConfigServerApp {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(SpringDynamicRuntimeConfigServerApp.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/spring-dynamic-configuration-runtime/spring-dynamic-runtime-configurations-server/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=config-server
2 | server.port=8888
3 | spring.profiles.active=native
4 | spring.cloud.config.server.native.search-locations=file:///C:/config
--------------------------------------------------------------------------------