Commit 6bc6042197cb8b963eee9a9da8a0139ec36f556e

Authored by 64850858
1 parent 77e3e087

更换云端录像的代理方式

src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java
... ... @@ -73,7 +73,7 @@ public class ProxyServletConfig {
73 73 if (ioException instanceof ConnectException) {
74 74 logger.error("zlm 连接失败");
75 75 } else if (ioException instanceof ClientAbortException) {
76   - logger.error("用户已中断连接,代理终止");
  76 + logger.error("zlm: 用户已中断连接,代理终止");
77 77 } else {
78 78 logger.error("zlm 代理失败: ", e);
79 79 }
... ... @@ -141,4 +141,102 @@ public class ProxyServletConfig {
141 141 }
142 142 }
143 143  
  144 +
  145 +
  146 + @Bean
  147 + public ServletRegistrationBean recordServletRegistrationBean(){
  148 + ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RecordProxySerlet(),"/record_proxy/*");
  149 + servletRegistrationBean.setName("record_proxy");
  150 + servletRegistrationBean.addInitParameter("targetUri", "http://127.0.0.1:18081");
  151 + servletRegistrationBean.addUrlMappings();
  152 + if (logger.isDebugEnabled()) {
  153 + servletRegistrationBean.addInitParameter("log", "true");
  154 + }
  155 + return servletRegistrationBean;
  156 + }
  157 +
  158 + class RecordProxySerlet extends ProxyServlet{
  159 +
  160 +
  161 + /**
  162 + * 异常处理
  163 + */
  164 + @Override
  165 + protected void handleRequestException(HttpRequest proxyRequest, HttpResponse proxyResonse, Exception e){
  166 + try {
  167 + super.handleRequestException(proxyRequest, proxyResonse, e);
  168 + } catch (ServletException servletException) {
  169 + logger.error("录像服务 代理失败: ", e);
  170 + } catch (IOException ioException) {
  171 + if (ioException instanceof ConnectException) {
  172 + logger.error("录像服务 连接失败");
  173 + } else if (ioException instanceof ClientAbortException) {
  174 + logger.error("录像服务:用户已中断连接,代理终止");
  175 + } else {
  176 + logger.error("录像服务 代理失败: ", e);
  177 + }
  178 + } catch (RuntimeException exception){
  179 + logger.error("录像服务 代理失败: ", e);
  180 + }
  181 + }
  182 +
  183 + /**
  184 + * 对于为按照格式请求的可以直接返回404
  185 + */
  186 + @Override
  187 + protected String getTargetUri(HttpServletRequest servletRequest) {
  188 + String requestURI = servletRequest.getRequestURI();
  189 + IMediaServerItem mediaInfo = getMediaInfoByUri(requestURI);
  190 +
  191 + String uri = null;
  192 + if (mediaInfo != null) {
  193 +// String realRequestURI = requestURI.substring(requestURI.indexOf(mediaInfo.getId())+ mediaInfo.getId().length());
  194 + uri = String.format("http://%s:%s", mediaInfo.getIp(), mediaInfo.getRecordAssistPort());
  195 + }else {
  196 + uri = "http://127.0.0.1:" + serverPort +"/index/hook/null"; // 只是一个能返回404的请求而已, 其他的也可以
  197 + }
  198 + return uri;
  199 + }
  200 +
  201 + /**
  202 + * 动态替换请求目标
  203 + */
  204 + @Override
  205 + protected HttpHost getTargetHost(HttpServletRequest servletRequest) {
  206 + String requestURI = servletRequest.getRequestURI();
  207 + IMediaServerItem mediaInfo = getMediaInfoByUri(requestURI);
  208 + HttpHost host;
  209 + if (mediaInfo != null) {
  210 + host = new HttpHost(mediaInfo.getIp(), mediaInfo.getRecordAssistPort());
  211 + }else {
  212 + host = new HttpHost("127.0.0.1", serverPort);
  213 + }
  214 + return host;
  215 +
  216 + }
  217 +
  218 + /**
  219 + * 根据uri获取流媒体信息
  220 + */
  221 + IMediaServerItem getMediaInfoByUri(String uri){
  222 + String[] split = uri.split("/");
  223 + String mediaServerId = split[2];
  224 + return mediaServerService.getOne(mediaServerId);
  225 + }
  226 +
  227 + /**
  228 + * 去掉url中的标志信息
  229 + */
  230 + @Override
  231 + protected String rewriteUrlFromRequest(HttpServletRequest servletRequest) {
  232 + String requestURI = servletRequest.getRequestURI();
  233 + IMediaServerItem mediaInfo = getMediaInfoByUri(requestURI);
  234 + String url = super.rewriteUrlFromRequest(servletRequest);
  235 + if (mediaInfo == null) {
  236 + return url;
  237 + }
  238 + return url.replace(mediaInfo.getId() + "/", "");
  239 + }
  240 + }
  241 +
144 242 }
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/record/RecoderProxyController.java deleted 100644 → 0
1   -package com.genersoft.iot.vmp.vmanager.record;
2   -
3   -import com.genersoft.iot.vmp.conf.MediaConfig;
4   -import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig;
5   -import com.genersoft.iot.vmp.media.zlm.dto.IMediaServerItem;
6   -import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
7   -import com.genersoft.iot.vmp.service.IMediaServerService;
8   -import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
9   -import org.springframework.beans.factory.annotation.Autowired;
10   -import org.springframework.http.HttpStatus;
11   -import org.springframework.util.StringUtils;
12   -import org.springframework.web.bind.annotation.RequestMapping;
13   -import org.springframework.web.bind.annotation.ResponseBody;
14   -import org.springframework.web.bind.annotation.RestController;
15   -import org.springframework.web.client.HttpClientErrorException;
16   -import org.springframework.web.client.RestTemplate;
17   -
18   -import javax.servlet.http.HttpServletRequest;
19   -import javax.servlet.http.HttpServletResponse;
20   -
21   -import java.io.UnsupportedEncodingException;
22   -import java.net.URLDecoder;
23   -
24   -@RestController
25   -@RequestMapping("/record_proxy")
26   -public class RecoderProxyController {
27   -
28   -
29   - // private final static Logger logger = LoggerFactory.getLogger(ZLMHTTPProxyController.class);
30   -
31   - @Autowired
32   - private IRedisCatchStorage redisCatchStorage;
33   - @Autowired
34   - private IMediaServerService mediaServerService;
35   -
36   - @Autowired
37   - private MediaConfig mediaConfig;
38   -
39   - @ResponseBody
40   - @RequestMapping(value = "/**/**/**", produces = "application/json;charset=UTF-8")
41   - public Object proxy(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
42   -
43   -
44   - String baseRequestURI = request.getRequestURI();
45   - String[] split = baseRequestURI.split("/");
46   - if (split.length <= 2) {
47   - response.setStatus(HttpStatus.NOT_FOUND.value());
48   - return null;
49   - }
50   - String mediaId = split[2];
51   - if (StringUtils.isEmpty(mediaId)){
52   - response.setStatus(HttpStatus.BAD_REQUEST.value());
53   - return null;
54   - }
55   - // 后续改为根据Id获取对应的ZLM
56   - IMediaServerItem mediaInfo = mediaServerService.getOne(mediaId);
57   - if (mediaInfo == null) {
58   - response.setStatus(HttpStatus.NOT_FOUND.value());
59   - return null;
60   - }
61   - String requestURI = String.format("http://%s:%s%s?%s",
62   - mediaInfo.getSdpIp(),
63   - mediaConfig.getRecordAssistPort(),
64   - baseRequestURI.substring(baseRequestURI.indexOf(mediaId) + mediaId.length()),
65   - URLDecoder.decode(request.getQueryString(), "UTF-8")
66   - );
67   - // 发送请求
68   - RestTemplate restTemplate = new RestTemplate();
69   - //将指定的url返回的参数自动封装到自定义好的对应类对象中
70   - Object result = null;
71   - try {
72   - result = restTemplate.getForObject(requestURI,Object.class);
73   -
74   - }catch (HttpClientErrorException httpClientErrorException) {
75   - response.setStatus(httpClientErrorException.getStatusCode().value());
76   - }
77   - return result;
78   - }
79   -}
web_src/src/components/CloudRecord.vue
... ... @@ -8,7 +8,8 @@
8 8 <div style="background-color: #FFFFFF; margin-bottom: 1rem; position: relative; padding: 0.5rem; text-align: left;">
9 9 <span style="font-size: 1rem; font-weight: bold;">云端录像</span>
10 10 <div style="position: absolute; right: 5rem; top: 0.3rem;">
11   - 节点选择: <el-select size="mini" @change="chooseMediaChange" style="width: 16rem; margin-right: 1rem;" v-model="mediaServerId" placeholder="请选择">
  11 + 节点选择:
  12 + <el-select size="mini" @change="chooseMediaChange" style="width: 16rem; margin-right: 1rem;" v-model="mediaServerId" placeholder="请选择" :disabled="recordDetail">
12 13 <el-option
13 14 v-for="item in mediaServerList"
14 15 :key="item.id"
... ...