mirror of
https://gitee.com/san-bing/JChargePointProtocol
synced 2026-05-06 19:09:57 +08:00
grpc 实现
This commit is contained in:
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.app.service.impl;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import sanbing.jcpp.app.data.PileSession;
|
||||
import sanbing.jcpp.app.service.DownlinkCallService;
|
||||
import sanbing.jcpp.app.service.cache.session.PileSessionCacheKey;
|
||||
import sanbing.jcpp.infrastructure.cache.CacheValueWrapper;
|
||||
import sanbing.jcpp.infrastructure.cache.TransactionalCache;
|
||||
import sanbing.jcpp.infrastructure.queue.discovery.ServiceInfoProvider;
|
||||
import sanbing.jcpp.infrastructure.util.trace.TracerContextUtil;
|
||||
import sanbing.jcpp.proto.gen.ProtocolProto.DownlinkRequestMessage;
|
||||
import sanbing.jcpp.protocol.adapter.DownlinkController;
|
||||
|
||||
import static sanbing.jcpp.infrastructure.util.trace.TracerContextUtil.*;
|
||||
|
||||
/**
|
||||
* @author baigod
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DefaultDownlinkCallService implements DownlinkCallService {
|
||||
|
||||
@Resource
|
||||
RestTemplate downlinkRestTemplate;
|
||||
|
||||
@Resource
|
||||
ServiceInfoProvider serviceInfoProvider;
|
||||
|
||||
@Resource
|
||||
DownlinkController downlinkController;
|
||||
|
||||
@Resource
|
||||
TransactionalCache<PileSessionCacheKey, PileSession> pileSessionCache;
|
||||
|
||||
@Value("${cache.type}")
|
||||
private String cacheType;
|
||||
|
||||
@Override
|
||||
public void sendDownlinkMessage(DownlinkRequestMessage.Builder downlinkMessageBuilder, String pileCode) {
|
||||
if (serviceInfoProvider.isMonolith() && "caffeine".equalsIgnoreCase(cacheType)) {
|
||||
|
||||
downlinkController.onDownlink(downlinkMessageBuilder.build())
|
||||
.setResultHandler(result -> log.debug("下行消息发送完成"));
|
||||
|
||||
} else {
|
||||
try {
|
||||
CacheValueWrapper<PileSession> pileSessionCacheValueWrapper = pileSessionCache.get(new PileSessionCacheKey(pileCode));
|
||||
|
||||
if (pileSessionCacheValueWrapper == null) {
|
||||
log.warn("充电桩会话不存在 {}", pileCode);
|
||||
return;
|
||||
}
|
||||
|
||||
PileSession pileSession = pileSessionCacheValueWrapper.get();
|
||||
|
||||
invokeDownlinkRestApi(downlinkMessageBuilder.build(), pileSession.getNodeWebapiIpPort());
|
||||
|
||||
|
||||
} catch (RestClientException e) {
|
||||
log.error("下行消息发送异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeDownlinkRestApi(DownlinkRequestMessage DownlinkRequestMessage, String nodeWebapiIpPort) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(JCPP_TRACER_ID, TracerContextUtil.getCurrentTracer().getTraceId());
|
||||
headers.add(JCPP_TRACER_ORIGIN, TracerContextUtil.getCurrentTracer().getOrigin());
|
||||
headers.add(JCPP_TRACER_TS, String.valueOf(TracerContextUtil.getCurrentTracer().getTracerTs()));
|
||||
headers.setContentType(MediaType.parseMediaType("application/x-protobuf"));
|
||||
|
||||
HttpEntity<DownlinkRequestMessage> entity = new HttpEntity<>(DownlinkRequestMessage, headers);
|
||||
|
||||
try {
|
||||
ResponseEntity<?> response = downlinkRestTemplate.postForEntity("http://" + nodeWebapiIpPort + "/api/onDownlink",
|
||||
entity, ResponseEntity.class);
|
||||
log.debug("下行消息发送成功 {}", response);
|
||||
} catch (RestClientException e) {
|
||||
log.error("下行消息发送失败 {}", DownlinkRequestMessage, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,9 @@ public class DefaultPileProtocolService implements PileProtocolService {
|
||||
cacheSession(uplinkQueueMessage, pile,
|
||||
loginRequest.getRemoteAddress(),
|
||||
loginRequest.getNodeId(),
|
||||
loginRequest.getNodeWebapiIpPort());
|
||||
loginRequest.getNodeHostAddress(),
|
||||
loginRequest.getNodeRestPort(),
|
||||
loginRequest.getNodeGrpcPort());
|
||||
|
||||
downlinkMessageBuilder.setLoginResponse(LoginResponse.newBuilder()
|
||||
.setSuccess(true)
|
||||
@@ -98,16 +100,26 @@ public class DefaultPileProtocolService implements PileProtocolService {
|
||||
cacheSession(uplinkQueueMessage, pile,
|
||||
heartBeatRequest.getRemoteAddress(),
|
||||
heartBeatRequest.getNodeId(),
|
||||
heartBeatRequest.getNodeWebapiIpPort());
|
||||
heartBeatRequest.getNodeHostAddress(),
|
||||
heartBeatRequest.getNodeRestPort(),
|
||||
heartBeatRequest.getNodeGrpcPort());
|
||||
}
|
||||
}
|
||||
|
||||
private void cacheSession(UplinkQueueMessage uplinkQueueMessage, Pile pile, String remoteAddress, String nodeId, String nodeWebapiIpPort) {
|
||||
private void cacheSession(UplinkQueueMessage uplinkQueueMessage,
|
||||
Pile pile,
|
||||
String remoteAddress,
|
||||
String nodeId,
|
||||
String nodeIp,
|
||||
int restPort,
|
||||
int grpcPort) {
|
||||
PileSession pileSession = new PileSession(pile.getId(), pile.getPileCode(), uplinkQueueMessage.getProtocolName());
|
||||
pileSession.setProtocolSessionId(new UUID(uplinkQueueMessage.getSessionIdMSB(), uplinkQueueMessage.getSessionIdLSB()));
|
||||
pileSession.setRemoteAddress(remoteAddress);
|
||||
pileSession.setNodeId(nodeId);
|
||||
pileSession.setNodeWebapiIpPort(nodeWebapiIpPort);
|
||||
pileSession.setNodeIp(nodeIp);
|
||||
pileSession.setNodeRestPort(restPort);
|
||||
pileSession.setNodeGrpcPort(grpcPort);
|
||||
pileSessionCache.put(new PileSessionCacheKey(pile.getPileCode()), pileSession);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.app.service.impl;
|
||||
|
||||
import com.google.common.net.HostAndPort;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.stereotype.Service;
|
||||
import sanbing.jcpp.app.data.PileSession;
|
||||
import sanbing.jcpp.app.service.DownlinkCallService;
|
||||
import sanbing.jcpp.app.service.grpc.DownlinkGrpcClient;
|
||||
import sanbing.jcpp.proto.gen.ProtocolProto.DownlinkRequestMessage;
|
||||
|
||||
/**
|
||||
* @author baigod
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@ConditionalOnExpression("'${downlink.rpc.type:null}'=='grpc'")
|
||||
public class GrpcDownlinkCallService extends DownlinkCallService {
|
||||
|
||||
@Resource
|
||||
DownlinkGrpcClient downlinkGrpcClient;
|
||||
|
||||
@Override
|
||||
protected void _sendDownlinkMessage(DownlinkRequestMessage downlinkMessage, PileSession pileSession) {
|
||||
try {
|
||||
|
||||
downlinkGrpcClient.sendDownlinkRequest(HostAndPort.fromParts(pileSession.getNodeIp(), pileSession.getNodeGrpcPort()),
|
||||
downlinkMessage);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("下行消息发送异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.app.service.impl;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import sanbing.jcpp.app.data.PileSession;
|
||||
import sanbing.jcpp.app.service.DownlinkCallService;
|
||||
import sanbing.jcpp.infrastructure.util.trace.TracerContextUtil;
|
||||
import sanbing.jcpp.proto.gen.ProtocolProto.DownlinkRequestMessage;
|
||||
|
||||
import static sanbing.jcpp.infrastructure.util.trace.TracerContextUtil.*;
|
||||
|
||||
/**
|
||||
* @author baigod
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@ConditionalOnExpression("'${downlink.rpc.type:null}'=='rest'")
|
||||
public class RestDownlinkCallService extends DownlinkCallService {
|
||||
|
||||
@Resource
|
||||
RestTemplate downlinkRestTemplate;
|
||||
|
||||
@Override
|
||||
protected void _sendDownlinkMessage(DownlinkRequestMessage downlinkMessage, PileSession pileSession) {
|
||||
try {
|
||||
|
||||
invokeDownlinkRestApi(downlinkMessage, pileSession.getNodeIp(), pileSession.getNodeRestPort());
|
||||
|
||||
} catch (RestClientException e) {
|
||||
log.error("下行消息发送异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeDownlinkRestApi(DownlinkRequestMessage downlinkRequestMessage, String nodeWebapiIpPort, int nodeRestPort) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(JCPP_TRACER_ID, TracerContextUtil.getCurrentTracer().getTraceId());
|
||||
headers.add(JCPP_TRACER_ORIGIN, TracerContextUtil.getCurrentTracer().getOrigin());
|
||||
headers.add(JCPP_TRACER_TS, String.valueOf(TracerContextUtil.getCurrentTracer().getTracerTs()));
|
||||
headers.setContentType(MediaType.parseMediaType("application/x-protobuf"));
|
||||
|
||||
HttpEntity<DownlinkRequestMessage> entity = new HttpEntity<>(downlinkRequestMessage, headers);
|
||||
|
||||
try {
|
||||
ResponseEntity<?> response = downlinkRestTemplate.postForEntity("http://" + nodeWebapiIpPort + ":" + nodeRestPort + "/api/onDownlink",
|
||||
entity, ResponseEntity.class);
|
||||
log.debug("下行消息发送成功 {}", response);
|
||||
} catch (RestClientException e) {
|
||||
log.error("下行消息发送失败 {}", downlinkRequestMessage, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user