Gitlab - Argos ALM by PALO IT

Commit 7b8275ba authored by Julian Pulido's avatar Julian Pulido

change gRPC consume for rest consume

parent 78f9f818
...@@ -19,7 +19,7 @@ public class MeasureConsumer implements Consumer<Measure> { ...@@ -19,7 +19,7 @@ public class MeasureConsumer implements Consumer<Measure> {
@Override @Override
public void accept(Measure measure) { public void accept(Measure measure) {
measureService.saveMeasureRedis(measure); measureService.saveMeasure(measure);
log.info("Catch event {}", measure); log.info("Catch event {}", measure);
} }
} }
...@@ -16,6 +16,7 @@ import com.paloit.pocmeasuregrpc.MeasureServiceGrpc; ...@@ -16,6 +16,7 @@ import com.paloit.pocmeasuregrpc.MeasureServiceGrpc;
import com.paloit.pocmeasuregrpc.MeasureServiceGrpc.MeasureServiceBlockingStub; import com.paloit.pocmeasuregrpc.MeasureServiceGrpc.MeasureServiceBlockingStub;
@Component @Component
@Deprecated(since = "01/08/2023")
public class GrpcAdapter { public class GrpcAdapter {
@Value("${grpc.adapter.host:localhost}") @Value("${grpc.adapter.host:localhost}")
...@@ -23,7 +24,7 @@ public class GrpcAdapter { ...@@ -23,7 +24,7 @@ public class GrpcAdapter {
@Value("${grpc.adapter.port:9099}") @Value("${grpc.adapter.port:9099}")
private int port; private int port;
public String createMeasure(Measure measure) { /*public String createMeasure(Measure measure) {
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port) ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext() .usePlaintext()
.build(); .build();
...@@ -35,5 +36,5 @@ public class GrpcAdapter { ...@@ -35,5 +36,5 @@ public class GrpcAdapter {
.build()); .build());
channel.isShutdown(); channel.isShutdown();
return measureResponse.getDeviceId(); return measureResponse.getDeviceId();
} }*/
} }
package com.cardiff.pocmeasurekafka.out.rest;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import com.cardiff.pocmeasurekafka.model.Measure;
@Component
public class RestAdapter {
private static final Logger log = LoggerFactory.getLogger(RestAdapter.class);
@Value("${m3.api.url:http://localhost:8080/measure}")
private String m3ApiUrl;
public String createMeasure(Measure measure){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<MeasureRest> requestEntity = new HttpEntity<>(this.mapper(measure), headers);
String absoluteUri = UriComponentsBuilder
.fromHttpUrl(m3ApiUrl)
.toUriString();
ResponseEntity<String> response = restTemplate.exchange(absoluteUri, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
}
private MeasureRest mapper(Measure measure){
return new MeasureRest(measure.deviceId(), measure.value(), measure.date().toInstant(ZoneOffset.UTC));
}
public record MeasureRest(String deviceId, String value, Instant date) implements Serializable {
}
}
...@@ -5,8 +5,8 @@ import org.slf4j.LoggerFactory; ...@@ -5,8 +5,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.cardiff.pocmeasurekafka.model.Measure; import com.cardiff.pocmeasurekafka.model.Measure;
import com.cardiff.pocmeasurekafka.out.grpc.GrpcAdapter;
import com.cardiff.pocmeasurekafka.out.redis.RedisAdapter; import com.cardiff.pocmeasurekafka.out.redis.RedisAdapter;
import com.cardiff.pocmeasurekafka.out.rest.RestAdapter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -16,15 +16,14 @@ public class MeasureService { ...@@ -16,15 +16,14 @@ public class MeasureService {
private static final Logger log = LoggerFactory.getLogger(MeasureService.class); private static final Logger log = LoggerFactory.getLogger(MeasureService.class);
private final RedisAdapter redisAdapter; private final RedisAdapter redisAdapter;
private final GrpcAdapter grpcAdapter; private final RestAdapter restAdapter;
public String saveMeasureRedis(Measure measure){ public void saveMeasure(Measure measure){
String deviceIdRedis = redisAdapter.saveMeasureRedis(measure).deviceId(); String deviceIdRedis = redisAdapter.saveMeasureRedis(measure).deviceId();
log.info("catch deviceID response redis {}", deviceIdRedis); log.info("catch deviceID save in redis {}", deviceIdRedis);
String deviceIdFromRedis = redisAdapter.getByDeviceId(measure.deviceId()).deviceId(); String deviceIdFromRedis = redisAdapter.getByDeviceId(measure.deviceId()).deviceId();
log.info("catch deviceID getByDeviceId from redis {}", deviceIdFromRedis); log.info("catch deviceID getByDeviceId from redis {}", deviceIdFromRedis);
String deviceIdGrpc = grpcAdapter.createMeasure(measure); String deviceIdFromRest = restAdapter.createMeasure(measure);
log.info("catch deviceID response Grpc {}", deviceIdRedis); log.info("catch deviceID from m3 rest service consume {}", deviceIdFromRest);
return deviceIdGrpc;
} }
} }
...@@ -51,3 +51,6 @@ spring.cache.redis.time-to-live=1m ...@@ -51,3 +51,6 @@ spring.cache.redis.time-to-live=1m
#GrpcEnpoint #GrpcEnpoint
grpc.adapter.host = localhost grpc.adapter.host = localhost
grpc.adapter.port = 9099 grpc.adapter.port = 9099
#RestEndpoint
m3.api.url = http://localhost:9099/measure
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment