Gitlab - Argos ALM by PALO IT
Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
poc-measure-kafka
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Luis Eduardo Oviedo Granados
poc-measure-kafka
Commits
89715d24
Commit
89715d24
authored
Aug 24, 2023
by
Lucas Eduardo Mori
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: use rabbitmq
parent
8b4183c4
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
99 additions
and
32 deletions
+99
-32
docker-compose.yaml
docker-compose.yaml
+8
-0
pom.xml
pom.xml
+11
-2
src/main/java/com/cardiff/pocmeasurekafka/config/RabbitMQConfig.java
...va/com/cardiff/pocmeasurekafka/config/RabbitMQConfig.java
+45
-0
src/main/java/com/cardiff/pocmeasurekafka/in/rest/MeasureController.java
...om/cardiff/pocmeasurekafka/in/rest/MeasureController.java
+3
-9
src/main/java/com/cardiff/pocmeasurekafka/in/stream/MeasureConsumer.java
...om/cardiff/pocmeasurekafka/in/stream/MeasureConsumer.java
+14
-7
src/main/java/com/cardiff/pocmeasurekafka/out/rest/RestAdapter.java
...ava/com/cardiff/pocmeasurekafka/out/rest/RestAdapter.java
+1
-4
src/main/resources/application-local.properties
src/main/resources/application-local.properties
+12
-5
src/main/resources/application.properties
src/main/resources/application.properties
+5
-5
No files found.
docker-compose.yaml
View file @
89715d24
...
@@ -25,3 +25,11 @@ services:
...
@@ -25,3 +25,11 @@ services:
environment
:
environment
:
MONGO_INITDB_ROOT_USERNAME
:
user
MONGO_INITDB_ROOT_USERNAME
:
user
MONGO_INITDB_ROOT_PASSWORD
:
pa55word
MONGO_INITDB_ROOT_PASSWORD
:
pa55word
rabbitmq
:
image
:
rabbitmq:3.12.0-management
ports
:
-
"
5672:5672"
-
"
15672:15672"
environment
:
RABBITMQ_DEFAULT_USER
:
"
user"
RABBITMQ_DEFAULT_PASS
:
"
p@ssword"
\ No newline at end of file
pom.xml
View file @
89715d24
...
@@ -31,9 +31,18 @@
...
@@ -31,9 +31,18 @@
<groupId>
io.micrometer
</groupId>
<groupId>
io.micrometer
</groupId>
<artifactId>
micrometer-tracing-bridge-brave
</artifactId>
<artifactId>
micrometer-tracing-bridge-brave
</artifactId>
</dependency>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-stream-kafka</artifactId>-->
<!-- </dependency>-->
<dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-cloud-starter-stream-kafka
</artifactId>
<artifactId>
spring-boot-starter-amqp
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.datatype
</groupId>
<artifactId>
jackson-datatype-jsr310
</artifactId>
<version>
2.15.2
</version>
<!-- Use the latest version -->
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
...
...
src/main/java/com/cardiff/pocmeasurekafka/config/RabbitMQConfig.java
0 → 100644
View file @
89715d24
package
com.cardiff.pocmeasurekafka.config
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
;
import
org.springframework.amqp.core.AmqpTemplate
;
import
org.springframework.amqp.core.Queue
;
import
org.springframework.amqp.rabbit.connection.ConnectionFactory
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
import
org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
;
import
org.springframework.amqp.support.converter.MessageConverter
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
RabbitMQConfig
{
@Value
(
"${spring.rabbitmq.queue}"
)
private
String
queue
;
@Bean
public
Queue
queue
()
{
return
new
Queue
(
queue
,
false
);
}
@Bean
public
MessageConverter
messageConverter
(
ObjectMapper
objectMapper
)
{
return
new
Jackson2JsonMessageConverter
(
objectMapper
);
}
@Bean
public
ObjectMapper
objectMapper
()
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
objectMapper
.
registerModule
(
new
JavaTimeModule
());
return
objectMapper
;
}
@Bean
public
AmqpTemplate
template
(
ConnectionFactory
connectionFactory
,
MessageConverter
messageConverter
)
{
RabbitTemplate
template
=
new
RabbitTemplate
(
connectionFactory
);
template
.
setMessageConverter
(
messageConverter
);
return
template
;
}
}
src/main/java/com/cardiff/pocmeasurekafka/in/rest/MeasureController.java
View file @
89715d24
package
com.cardiff.pocmeasurekafka.in.rest
;
package
com.cardiff.pocmeasurekafka.in.rest
;
import
java.io.Serializable
;
import
com.cardiff.pocmeasurekafka.out.redis.RedisAdapter
;
import
java.time.LocalDateTime
;
import
com.cardiff.pocmeasurekafka.out.redis.RedisAdapter.MeasureRedis
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.cardiff.pocmeasurekafka.model.Measure
;
import
com.cardiff.pocmeasurekafka.out.redis.RedisAdapter
;
import
com.cardiff.pocmeasurekafka.out.redis.RedisAdapter.MeasureRedis
;
import
lombok.RequiredArgsConstructor
;
@RestController
@RestController
@RequestMapping
(
"/measure"
)
@RequestMapping
(
"/measure"
)
@RequiredArgsConstructor
@RequiredArgsConstructor
...
...
src/main/java/com/cardiff/pocmeasurekafka/in/stream/MeasureConsumer.java
View file @
89715d24
package
com.cardiff.pocmeasurekafka.in.stream
;
package
com.cardiff.pocmeasurekafka.in.stream
;
import
java.util.function.Consumer
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.amqp.rabbit.annotation.RabbitListener
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
com.cardiff.pocmeasurekafka.model.Measure
;
import
com.cardiff.pocmeasurekafka.model.Measure
;
...
@@ -13,13 +12,21 @@ import lombok.RequiredArgsConstructor;
...
@@ -13,13 +12,21 @@ import lombok.RequiredArgsConstructor;
@Component
(
"measureConsumer"
)
@Component
(
"measureConsumer"
)
@RequiredArgsConstructor
@RequiredArgsConstructor
public
class
MeasureConsumer
implements
Consumer
<
Measure
>
{
public
class
MeasureConsumer
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
MeasureConsumer
.
class
);
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
MeasureConsumer
.
class
);
private
final
MeasureService
measureService
;
private
final
MeasureService
measureService
;
@Override
// @Override
public
void
accept
(
Measure
measure
)
{
// public void accept(Measure measure) {
measureService
.
saveMeasure
(
measure
);
// measureService.saveMeasure(measure);
log
.
info
(
"Catch event {}"
,
measure
);
// log.info("Catch event {}", measure);
// }
@RabbitListener
(
queues
=
"${spring.rabbitmq.queue}"
)
public
void
receiveMessage
(
Measure
message
)
{
measureService
.
saveMeasure
(
message
);
log
.
info
(
"Received message: [{}]"
,
message
);
}
}
}
}
src/main/java/com/cardiff/pocmeasurekafka/out/rest/RestAdapter.java
View file @
89715d24
package
com.cardiff.pocmeasurekafka.out.rest
;
package
com.cardiff.pocmeasurekafka.out.rest
;
import
com.cardiff.pocmeasurekafka.model.Measure
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.time.Instant
;
import
java.time.Instant
;
import
java.time.LocalDateTime
;
import
java.time.ZoneOffset
;
import
java.time.ZoneOffset
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
...
@@ -17,8 +16,6 @@ import org.springframework.stereotype.Component;
...
@@ -17,8 +16,6 @@ import org.springframework.stereotype.Component;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
com.cardiff.pocmeasurekafka.model.Measure
;
@Component
@Component
public
class
RestAdapter
{
public
class
RestAdapter
{
...
...
src/main/resources/application-local.properties
View file @
89715d24
...
@@ -27,17 +27,24 @@ spring.jackson.visibility.setter=none
...
@@ -27,17 +27,24 @@ spring.jackson.visibility.setter=none
spring.jackson.visibility.is-getter
=
none
spring.jackson.visibility.is-getter
=
none
# Stream
# Stream
spring.cloud.stream.kafka.binder.brokers
=
localhost:9092
#spring.cloud.stream.kafka.binder.brokers=localhost:9092
spring.cloud.function.definition
=
measureConsumer
#spring.cloud.function.definition=measureConsumer
spring.cloud.stream.kafka.binder.configuration.key.serializer
=
org.apache.kafka.common.serialization.StringSerializer
#spring.cloud.stream.kafka.binder.configuration.key.serializer=org.apache.kafka.common.serialization.StringSerializer
# RabbitMQ
spring.rabbitmq.host
=
localhost
spring.rabbitmq.queue
=
measureConsumer
spring.rabbitmq.port
=
5672
spring.rabbitmq.username
=
user
spring.rabbitmq.password
=
p@ssword
#Producer
#Producer
#spring.cloud.stream.bindings.measureProducer.destination=measure
#spring.cloud.stream.bindings.measureProducer.destination=measure
## Consumer
## Consumer
spring.cloud.stream.bindings.measureConsumer-in-0.destination
=
measure
#
spring.cloud.stream.bindings.measureConsumer-in-0.destination=measure
spring.cloud.stream.bindings.measureConsumer-in-0.group
=
measureGroup
#
spring.cloud.stream.bindings.measureConsumer-in-0.group=measureGroup
# Redis
# Redis
...
...
src/main/resources/application.properties
View file @
89715d24
...
@@ -28,13 +28,13 @@ spring.jackson.visibility.is-getter=none
...
@@ -28,13 +28,13 @@ spring.jackson.visibility.is-getter=none
spring.cloud.stream.bindings.measureProducer.destination
=
measure
spring.cloud.stream.bindings.measureProducer.destination
=
measure
# Stream
# Stream
spring.cloud.stream.kafka.binder.brokers
=
${SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKERS}
#
spring.cloud.stream.kafka.binder.brokers=${SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKERS}
spring.cloud.function.definition
=
measureConsumer
#
spring.cloud.function.definition=measureConsumer
spring.cloud.stream.kafka.binder.configuration.key.serializer
=
org.apache.kafka.common.serialization.StringSerializer
#
spring.cloud.stream.kafka.binder.configuration.key.serializer=org.apache.kafka.common.serialization.StringSerializer
## Consumer
## Consumer
spring.cloud.stream.bindings.measureConsumer-in-0.destination
=
measure
#
spring.cloud.stream.bindings.measureConsumer-in-0.destination=measure
spring.cloud.stream.bindings.measureConsumer-in-0.group
=
measureGroup
#
spring.cloud.stream.bindings.measureConsumer-in-0.group=measureGroup
# Redis
# Redis
#spring.data.redis.host=localhost
#spring.data.redis.host=localhost
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment