Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2395f81

Browse files
memcached and rabbitmq is fixed
1 parent 61d3ec9 commit 2395f81

File tree

5 files changed

+111
-12
lines changed

5 files changed

+111
-12
lines changed

‎docker-compose.yml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,58 @@ services:
55
build:
66
context: .
77
dockerfile: Dockerfile
8-
image: devops-app:latest
8+
image: spring-app
99
container_name: devops-app
1010
ports:
11-
- "8080:8080"
11+
- "8082:8080"
1212
environment:
1313
SPRING_PROFILES_ACTIVE: docker
1414
depends_on:
15-
- mongo
16-
- rabbitmq
17-
- memcached
15+
mongo:
16+
condition: service_healthy
17+
rabbitmq:
18+
condition: service_healthy
19+
memcached:
20+
condition: service_healthy
1821

1922
mongo:
2023
image: mongo:4.4
2124
container_name: mongo
2225
ports:
23-
- "27018:27017"
26+
- "27017:27017"
2427
volumes:
2528
- mongo-data:/data/db
29+
healthcheck:
30+
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
31+
interval: 10s
32+
timeout: 5s
33+
retries: 5
2634

2735
rabbitmq:
2836
image: rabbitmq:3-management
2937
container_name: rabbitmq
3038
ports:
31-
- "5673:5672"
32-
- "15673:15672" # Management UI
39+
- "5674:5672"
40+
- "15674:15672" # Management UI
3341
environment:
3442
RABBITMQ_DEFAULT_USER: guest
3543
RABBITMQ_DEFAULT_PASS: guest
44+
healthcheck:
45+
test: ["CMD", "rabbitmqctl", "status"]
46+
interval: 10s
47+
timeout: 5s
48+
retries: 5
3649

3750
memcached:
3851
image: memcached:alpine
3952
container_name: memcached
4053
ports:
41-
- "11212:11211"
54+
- "11211:11211"
55+
healthcheck:
56+
test: ["CMD", "echo", "version", "|", "nc", "localhost", "11211"]
57+
interval: 10s
58+
timeout: 5s
59+
retries: 5
4260

4361
volumes:
4462
mongo-data:

‎pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
<groupId>org.springframework.boot</groupId>
3636
<artifactId>spring-boot-starter-thymeleaf</artifactId>
3737
</dependency>
38+
39+
<dependency>
40+
<groupId>com.googlecode.xmemcached</groupId>
41+
<artifactId>xmemcached</artifactId>
42+
<version>2.4.7</version>
43+
</dependency>
44+
3845

3946
<!-- Spring Boot Security -->
4047
<dependency>
@@ -75,6 +82,12 @@
7582
<scope>test</scope>
7683
</dependency>
7784

85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-starter-amqp</artifactId>
88+
</dependency>
89+
90+
7891

7992

8093
</dependencies>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.devopsapp.config;
2+
3+
import net.rubyeye.xmemcached.XMemcachedClient;
4+
import net.rubyeye.xmemcached.MemcachedClient;
5+
import org.springframework.cache.CacheManager;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
import org.springframework.cache.support.SimpleCacheManager;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
import java.io.IOException;
12+
13+
@Configuration
14+
@EnableCaching
15+
public class MemcachedConfig {
16+
17+
@Bean
18+
public MemcachedClient memcachedClient() throws IOException {
19+
return new XMemcachedClient("memcached", 11211);
20+
}
21+
22+
@Bean
23+
public CacheManager cacheManager() {
24+
return new SimpleCacheManager();
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.devopsapp.config;
2+
3+
import org.springframework.amqp.core.Queue;
4+
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
5+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
6+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
7+
import org.springframework.amqp.rabbit.core.RabbitAdmin;
8+
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
@Configuration
13+
@EnableRabbit
14+
public class RabbitMQConfig {
15+
16+
public static final String USER_QUEUE = "user.registration.queue";
17+
18+
@Bean
19+
public Queue userQueue() {
20+
return new Queue(USER_QUEUE, true); // durable queue
21+
}
22+
23+
@Bean
24+
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
25+
return new RabbitAdmin(connectionFactory);
26+
}
27+
28+
@Bean
29+
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
30+
return new RabbitTemplate(connectionFactory);
31+
}
32+
}

‎src/main/java/com/example/devopsapp/service/UserService.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.example.devopsapp.model.User;
44
import com.example.devopsapp.repository.UserRepository;
5+
import com.example.devopsapp.config.RabbitMQConfig;
6+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
79
import org.springframework.stereotype.Service;
@@ -13,13 +15,15 @@
1315
public class UserService {
1416

1517
private final UserRepository userRepository;
16-
1718
private final BCryptPasswordEncoder passwordEncoder;
1819

20+
private final RabbitTemplate rabbitTemplate;
21+
1922
@Autowired
20-
public UserService(UserRepository userRepository) {
23+
public UserService(UserRepository userRepository, RabbitTemplaterabbitTemplate) {
2124
this.userRepository = userRepository;
2225
this.passwordEncoder = new BCryptPasswordEncoder();
26+
this.rabbitTemplate = rabbitTemplate;
2327
}
2428

2529
public Optional<User> findByEmail(String email) {
@@ -33,7 +37,13 @@ public Optional<User> findByUsername(String username) {
3337
public User saveUser(User user) {
3438
// Encrypt password before saving
3539
user.setPassword(passwordEncoder.encode(user.getPassword()));
36-
return userRepository.save(user);
40+
User savedUser = userRepository.save(user);
41+
42+
// Send a message to RabbitMQ queue
43+
rabbitTemplate.convertAndSend(RabbitMQConfig.USER_QUEUE,
44+
"New user registered: " + savedUser.getUsername());
45+
46+
return savedUser;
3747
}
3848

3949
public boolean checkPassword(User user, String rawPassword) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /