diff --git a/auth-service/pom.xml b/auth-service/pom.xml
index 7aa52ff..f9ebaa5 100644
--- a/auth-service/pom.xml
+++ b/auth-service/pom.xml
@@ -108,6 +108,15 @@
jaxb-runtime
2.3.2
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-stream-kafka
+
diff --git a/auth-service/src/main/java/com/javatab/authservice/AuthServiceApplication.java b/auth-service/src/main/java/com/javatab/authservice/AuthServiceApplication.java
index 5ffe6e3..9a388fb 100644
--- a/auth-service/src/main/java/com/javatab/authservice/AuthServiceApplication.java
+++ b/auth-service/src/main/java/com/javatab/authservice/AuthServiceApplication.java
@@ -5,9 +5,17 @@
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.web.client.RestTemplate;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
@SpringBootApplication
@EnableEurekaClient
public class AuthServiceApplication {
@@ -21,5 +29,4 @@ public static void main(String[] args) {
public RestTemplate restTemplate() {
return new RestTemplate();
}
-
-}
+}
\ No newline at end of file
diff --git a/auth-service/src/main/java/com/javatab/authservice/controllers/AuthController.java b/auth-service/src/main/java/com/javatab/authservice/controllers/AuthController.java
index a13174d..783f939 100644
--- a/auth-service/src/main/java/com/javatab/authservice/controllers/AuthController.java
+++ b/auth-service/src/main/java/com/javatab/authservice/controllers/AuthController.java
@@ -3,9 +3,11 @@
import com.javatab.authservice.domain.AuthRequest;
import com.javatab.authservice.domain.AuthResponse;
import com.javatab.authservice.domain.User;
+import com.javatab.authservice.domain.UserEvent;
import com.javatab.authservice.security.JwtUserDetailService;
import com.javatab.authservice.services.UserService;
import lombok.RequiredArgsConstructor;
+import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.web.bind.annotation.*;
@RestController
@@ -14,6 +16,9 @@ public class AuthController {
private final JwtUserDetailService userDetailService;
private final UserService userService;
+ private final StreamBridge streamBridge;
+
+ static final String USER_CREATED_OUTPUT = "userCreatedOutput";
@PostMapping("/login")
public AuthResponse login(@RequestBody AuthRequest request) throws Exception {
@@ -22,6 +27,10 @@ public AuthResponse login(@RequestBody AuthRequest request) throws Exception {
@PostMapping("/register")
public User registerNewUser(@RequestBody User user) {
- return userService.createNewUser(user);
+ User newUser = userService.createNewUser(user);
+ UserEvent userEvent = UserEvent.builder().username(newUser.getUsername()).email(newUser.getEmail()).build();
+ boolean sent = streamBridge.send(USER_CREATED_OUTPUT, userEvent);
+ System.out.println("Message sent " + sent);
+ return newUser;
}
}
diff --git a/auth-service/src/main/java/com/javatab/authservice/domain/UserEvent.java b/auth-service/src/main/java/com/javatab/authservice/domain/UserEvent.java
new file mode 100644
index 0000000..8a0dfae
--- /dev/null
+++ b/auth-service/src/main/java/com/javatab/authservice/domain/UserEvent.java
@@ -0,0 +1,11 @@
+package com.javatab.authservice.domain;
+
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class UserEvent {
+ private String username;
+ private String email;
+}
diff --git a/auth-service/src/main/java/com/javatab/authservice/security/WebSecurityConfiguration.java b/auth-service/src/main/java/com/javatab/authservice/security/WebSecurityConfiguration.java
index eac2da9..14ea49c 100644
--- a/auth-service/src/main/java/com/javatab/authservice/security/WebSecurityConfiguration.java
+++ b/auth-service/src/main/java/com/javatab/authservice/security/WebSecurityConfiguration.java
@@ -4,6 +4,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -34,7 +35,7 @@ protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/register", "/login").permitAll()
- //.antMatchers(HttpHeaders.ALLOW).permitAll()
+ .antMatchers(HttpHeaders.ALLOW).permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntityPoint)
diff --git a/auth-service/src/main/resources/application.yml b/auth-service/src/main/resources/application.yml
index aa02778..cb8be23 100644
--- a/auth-service/src/main/resources/application.yml
+++ b/auth-service/src/main/resources/application.yml
@@ -9,6 +9,22 @@ spring:
enabled: true
service-id: config-server
+ #function:
+ # defination: userCreatedOutput
+ stream:
+ #kafka:
+ # binder:
+ # brokers: 192.168.1.8
+ # defaultBrokerPort: 9092
+ bindings:
+ userCreatedOutput-out-0:
+ destination: userCreatedOutput
+ #binder: kafka
+ #group: group
+ #consumer:
+ # concurrency: 10
+ # max-attempts: 3
+
datasource:
url: jdbc:postgresql://localhost:5432/testdb
username: postgres
diff --git a/docker/docker-compose-kafka.yml b/docker/docker-compose-kafka.yml
new file mode 100644
index 0000000..8d1b49b
--- /dev/null
+++ b/docker/docker-compose-kafka.yml
@@ -0,0 +1,39 @@
+version: '3.8'
+services:
+ zookeeper:
+ image: wurstmeister/zookeeper:latest
+ ports:
+ - "2181:2181"
+ networks:
+ backend:
+ aliases:
+ - "zookeeper"
+ kafkaserver:
+ image: wurstmeister/kafka:latest
+ ports:
+ - "9092:9092"
+ environment:
+ - KAFKA_ADVERTISED_HOST_NAME=192.168.1.8
+ - KAFKA_ADVERTISED_PORT=9092
+ - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
+ - KAFKA_CREATE_TOPICS=users:1:1,students:1:1,userCreatedOutput:1:1
+ volumes:
+ - "/var/run/docker.sock:/var/run/docker.sock"
+ depends_on:
+ - zookeeper
+ networks:
+ backend:
+ aliases:
+ - "kafka"
+
+networks:
+ backend:
+ driver: bridge
+ # redisserver:
+ # image: redis:alpine
+ # ports:
+ # - 6379:6379
+ # networks:
+ # backend:
+ # aliases:
+ # - "redis"
\ No newline at end of file
diff --git a/docker/docker-compose-redis.yml b/docker/docker-compose-redis.yml
new file mode 100644
index 0000000..c637274
--- /dev/null
+++ b/docker/docker-compose-redis.yml
@@ -0,0 +1,14 @@
+version: "3.8"
+services:
+ redisserver:
+ image: redis:alpine
+ ports:
+ - "6379:6379"
+ networks:
+ backend:
+ aliases:
+ - "redis"
+
+networks:
+ backend:
+ driver: bridge
\ No newline at end of file
diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index 6b04538..88b73ff 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -1,4 +1,4 @@
-version: '3.8' # Migrate to version 3
+version: '3.8'
services:
discoveryservice:
image: javatab/discovery-service:1.0.0
diff --git a/student-service/pom.xml b/student-service/pom.xml
index f97bbca..787701d 100644
--- a/student-service/pom.xml
+++ b/student-service/pom.xml
@@ -64,6 +64,15 @@
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-stream-kafka
+
diff --git a/student-service/src/main/java/com/javatab/student/MessageFunctionConfiguration.java b/student-service/src/main/java/com/javatab/student/MessageFunctionConfiguration.java
new file mode 100644
index 0000000..7f34164
--- /dev/null
+++ b/student-service/src/main/java/com/javatab/student/MessageFunctionConfiguration.java
@@ -0,0 +1,15 @@
+package com.javatab.student;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.function.Consumer;
+
+@Configuration
+public class MessageFunctionConfiguration {
+
+ @Bean
+ Consumer getNewlyCreatedUser() {
+ return userEvent -> System.out.println("New created user " + userEvent);
+ }
+}
diff --git a/student-service/src/main/java/com/javatab/student/Student.java b/student-service/src/main/java/com/javatab/student/Student.java
index b68bc1e..8ba0050 100644
--- a/student-service/src/main/java/com/javatab/student/Student.java
+++ b/student-service/src/main/java/com/javatab/student/Student.java
@@ -15,13 +15,9 @@
@Data
@Document(collection = "users")
public class Student {
-
-
@JsonSerialize(using = ToStringSerializer.class)
private ObjectId id;
private String username;
private String email;
private String password;
- private String role;
-
}
diff --git a/student-service/src/main/java/com/javatab/student/StudentServiceApplication.java b/student-service/src/main/java/com/javatab/student/StudentServiceApplication.java
index e9b235a..1d993d5 100644
--- a/student-service/src/main/java/com/javatab/student/StudentServiceApplication.java
+++ b/student-service/src/main/java/com/javatab/student/StudentServiceApplication.java
@@ -6,6 +6,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
@SpringBootApplication
public class StudentServiceApplication {
diff --git a/student-service/src/main/java/com/javatab/student/UserEvent.java b/student-service/src/main/java/com/javatab/student/UserEvent.java
new file mode 100644
index 0000000..bbddca7
--- /dev/null
+++ b/student-service/src/main/java/com/javatab/student/UserEvent.java
@@ -0,0 +1,11 @@
+package com.javatab.student;
+
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class UserEvent {
+ private String username;
+ private String email;
+}
diff --git a/student-service/src/main/resources/application.yml b/student-service/src/main/resources/application.yml
index 2641e6d..e28b0a8 100644
--- a/student-service/src/main/resources/application.yml
+++ b/student-service/src/main/resources/application.yml
@@ -8,6 +8,18 @@ spring:
discovery:
enabled: true
service-id: config-server
+
+ stream:
+ # kafka:
+ # binder:
+ # brokers: 192.168.1.8
+ #function:
+ # defination: getNewlyCreatedUser
+ bindings:
+ getNewlyCreatedUser-in-0:
+ destination: userCreatedOutput
+ #binder: kafka
+ group: mygroup
data:
mongodb:
host: localhost