Skip to content

Commit 9190af5

Browse files
authored
Merge pull request #7 from Nasruddin/feature/cloud-gateway
Kafka integrated and Redis is supported
2 parents a6e1e79 + 0f522ff commit 9190af5

File tree

15 files changed

+161
-9
lines changed

15 files changed

+161
-9
lines changed

auth-service/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@
108108
<artifactId>jaxb-runtime</artifactId>
109109
<version>2.3.2</version>
110110
</dependency>
111+
<!--<dependency>
112+
<groupId>org.springframework.cloud</groupId>
113+
<artifactId>spring-cloud-stream</artifactId>
114+
</dependency>-->
115+
116+
<dependency>
117+
<groupId>org.springframework.cloud</groupId>
118+
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
119+
</dependency>
111120
</dependencies>
112121

113122
<dependencyManagement>

auth-service/src/main/java/com/javatab/authservice/AuthServiceApplication.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
66
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
77
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
8+
import org.springframework.cloud.stream.annotation.EnableBinding;
89
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.EnableMBeanExport;
911
import org.springframework.web.client.RestTemplate;
1012

13+
import java.util.Random;
14+
import java.util.concurrent.atomic.AtomicInteger;
15+
import java.util.function.Consumer;
16+
import java.util.function.Function;
17+
import java.util.function.Supplier;
18+
1119
@SpringBootApplication
1220
@EnableEurekaClient
1321
public class AuthServiceApplication {
@@ -21,5 +29,4 @@ public static void main(String[] args) {
2129
public RestTemplate restTemplate() {
2230
return new RestTemplate();
2331
}
24-
25-
}
32+
}

auth-service/src/main/java/com/javatab/authservice/controllers/AuthController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.javatab.authservice.domain.AuthRequest;
44
import com.javatab.authservice.domain.AuthResponse;
55
import com.javatab.authservice.domain.User;
6+
import com.javatab.authservice.domain.UserEvent;
67
import com.javatab.authservice.security.JwtUserDetailService;
78
import com.javatab.authservice.services.UserService;
89
import lombok.RequiredArgsConstructor;
10+
import org.springframework.cloud.stream.function.StreamBridge;
911
import org.springframework.web.bind.annotation.*;
1012

1113
@RestController
@@ -14,6 +16,9 @@ public class AuthController {
1416

1517
private final JwtUserDetailService userDetailService;
1618
private final UserService userService;
19+
private final StreamBridge streamBridge;
20+
21+
static final String USER_CREATED_OUTPUT = "userCreatedOutput";
1722

1823
@PostMapping("/login")
1924
public AuthResponse login(@RequestBody AuthRequest request) throws Exception {
@@ -22,6 +27,10 @@ public AuthResponse login(@RequestBody AuthRequest request) throws Exception {
2227

2328
@PostMapping("/register")
2429
public User registerNewUser(@RequestBody User user) {
25-
return userService.createNewUser(user);
30+
User newUser = userService.createNewUser(user);
31+
UserEvent userEvent = UserEvent.builder().username(newUser.getUsername()).email(newUser.getEmail()).build();
32+
boolean sent = streamBridge.send(USER_CREATED_OUTPUT, userEvent);
33+
System.out.println("Message sent " + sent);
34+
return newUser;
2635
}
2736
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.javatab.authservice.domain;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
@Data
7+
@Builder
8+
public class UserEvent {
9+
private String username;
10+
private String email;
11+
}

auth-service/src/main/java/com/javatab/authservice/security/WebSecurityConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.http.HttpHeaders;
78
import org.springframework.security.authentication.AuthenticationManager;
89
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
910
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -34,7 +35,7 @@ protected void configure(HttpSecurity http) throws Exception {
3435
http.csrf().disable()
3536
.authorizeRequests()
3637
.antMatchers("/register", "/login").permitAll()
37-
//.antMatchers(HttpHeaders.ALLOW).permitAll()
38+
.antMatchers(HttpHeaders.ALLOW).permitAll()
3839
.anyRequest().authenticated()
3940
.and()
4041
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntityPoint)

auth-service/src/main/resources/application.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ spring:
99
enabled: true
1010
service-id: config-server
1111

12+
#function:
13+
# defination: userCreatedOutput
14+
stream:
15+
#kafka:
16+
# binder:
17+
# brokers: 192.168.1.8
18+
# defaultBrokerPort: 9092
19+
bindings:
20+
userCreatedOutput-out-0:
21+
destination: userCreatedOutput
22+
#binder: kafka
23+
#group: group
24+
#consumer:
25+
# concurrency: 10
26+
# max-attempts: 3
27+
1228
datasource:
1329
url: jdbc:postgresql://localhost:5432/testdb
1430
username: postgres

docker/docker-compose-kafka.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: '3.8'
2+
services:
3+
zookeeper:
4+
image: wurstmeister/zookeeper:latest
5+
ports:
6+
- "2181:2181"
7+
networks:
8+
backend:
9+
aliases:
10+
- "zookeeper"
11+
kafkaserver:
12+
image: wurstmeister/kafka:latest
13+
ports:
14+
- "9092:9092"
15+
environment:
16+
- KAFKA_ADVERTISED_HOST_NAME=192.168.1.8
17+
- KAFKA_ADVERTISED_PORT=9092
18+
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
19+
- KAFKA_CREATE_TOPICS=users:1:1,students:1:1,userCreatedOutput:1:1
20+
volumes:
21+
- "/var/run/docker.sock:/var/run/docker.sock"
22+
depends_on:
23+
- zookeeper
24+
networks:
25+
backend:
26+
aliases:
27+
- "kafka"
28+
29+
networks:
30+
backend:
31+
driver: bridge
32+
# redisserver:
33+
# image: redis:alpine
34+
# ports:
35+
# - 6379:6379
36+
# networks:
37+
# backend:
38+
# aliases:
39+
# - "redis"

docker/docker-compose-redis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.8"
2+
services:
3+
redisserver:
4+
image: redis:alpine
5+
ports:
6+
- "6379:6379"
7+
networks:
8+
backend:
9+
aliases:
10+
- "redis"
11+
12+
networks:
13+
backend:
14+
driver: bridge

docker/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3.8' # Migrate to version 3
1+
version: '3.8'
22
services:
33
discoveryservice:
44
image: javatab/discovery-service:1.0.0

student-service/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
</exclusion>
6565
</exclusions>
6666
</dependency>
67+
<!--<dependency>
68+
<groupId>org.springframework.cloud</groupId>
69+
<artifactId>spring-cloud-stream</artifactId>
70+
</dependency>-->
71+
72+
<dependency>
73+
<groupId>org.springframework.cloud</groupId>
74+
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
75+
</dependency>
6776
</dependencies>
6877

6978
<dependencyManagement>

0 commit comments

Comments
 (0)