剔除 httpExchange 更换为 openfeign 实现

This commit is contained in:
shikong 2023-08-20 00:48:06 +08:00
parent 8d21cfeb75
commit e792607241
11 changed files with 113 additions and 45 deletions

View File

@ -34,6 +34,7 @@
<java.version>17</java.version>
<springboot.version>3.1.2</springboot.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
<!--Java Bean-->
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
@ -135,6 +136,14 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -34,10 +34,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- For reactive support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>

View File

@ -1,11 +1,17 @@
package cn.skcks.docking.gb28181.media.dto.config;
import feign.Param;
import lombok.Data;
@Data
public class ApiConfig {
@Param("api.apiDebug")
private Integer apiDebug;
@Param("api.secret")
private String secret;
@Param("api.defaultSnap")
private String defaultSnap;
@Param("api.snapRoot")
private String snapRoot;
}

View File

@ -4,6 +4,7 @@ package cn.skcks.docking.gb28181.media.dto.config;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.Data;
@Data
public class ServerConfig {
@JsonUnwrapped(prefix = "api.")

View File

@ -3,6 +3,7 @@ package cn.skcks.docking.gb28181.media.dto.response;
import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.common.json.JsonUtils;
import cn.skcks.docking.gb28181.media.dto.status.ResponseStatus;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -26,6 +27,7 @@ public class ZlmResponse<T> {
return JsonUtils.toJson(this);
}
@JsonIgnore
public JsonResponse<T> getJsonResponse(){
return ZlmResponseConvertor.INSTANCE.toJsonResponse(this);
}

View File

@ -0,0 +1,29 @@
package cn.skcks.docking.gb28181.media.proxy;
import cn.skcks.docking.gb28181.media.dto.config.ServerConfig;
import cn.skcks.docking.gb28181.media.dto.response.ZlmResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.HttpExchange;
import java.util.List;
import java.util.Map;
@FeignClient(name="zlmMediaServerProxy",url = "${media.url}")
@HttpExchange
public interface ZlmMediaHttpClient {
@GetMapping("/index/api/getApiList")
ZlmResponse<List<String>> getApiList(@RequestParam String secret);
@GetMapping("/index/api/getServerConfig")
ZlmResponse<List<ServerConfig>> getServerConfig(@RequestParam String secret);
@GetMapping("/index/api/getServerConfig")
ResponseEntity<String> getServerConfigResponseEntity(@RequestParam String secret);
@GetMapping("/index/api/setServerConfig")
String setServerConfig(@RequestParam String secret, @SpringQueryMap Map<String,Object> config);
}

View File

@ -1,20 +1,36 @@
package cn.skcks.docking.gb28181.media.proxy;
import cn.skcks.docking.gb28181.common.json.JsonUtils;
import cn.skcks.docking.gb28181.media.dto.config.ServerConfig;
import cn.skcks.docking.gb28181.media.dto.response.ZlmResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Builder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import java.util.List;
import java.util.Map;
@HttpExchange
public interface ZlmMediaHttpService {
@Builder
public class ZlmMediaHttpService {
private String secret;
private ZlmMediaHttpClient exchange;
@GetExchange("/index/api/getServerConfig")
ZlmResponse<List<ServerConfig>> getServerConfig(@RequestParam String secret);
public ZlmResponse<List<String>> getApiList() {
return exchange.getApiList(secret);
}
@GetExchange("/index/api/getServerConfig")
ResponseEntity<String> getServerConfigResponseEntity(@RequestParam String secret);
public ZlmResponse<List<ServerConfig>> getServerConfig() {
return exchange.getServerConfig(secret);
}
public ResponseEntity<String> getServerConfigResponseEntity() {
return exchange.getServerConfigResponseEntity(secret);
}
public String setServerConfig(ServerConfig config){
Map<String,Object> map = JsonUtils.mapper.convertValue(config, new TypeReference<>() {});
return exchange.setServerConfig(secret, map);
}
}

View File

@ -2,30 +2,20 @@ package cn.skcks.docking.gb28181.media.proxy;
import cn.skcks.docking.gb28181.media.config.ZlmMediaConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@RequiredArgsConstructor
@Component
public class ZlmProxy {
private final ZlmMediaConfig mediaConfig;
private final ZlmMediaHttpClient exchange;
@Bean
public WebClient zlmClient() {
return WebClient.builder()
.baseUrl(mediaConfig.getUrl())
public ZlmMediaHttpService zlMediaHttpService(){
return ZlmMediaHttpService.builder()
.secret(mediaConfig.getSecret())
.exchange(exchange)
.build();
}
@Bean
public ZlmMediaHttpService zlMediaHttpService(@Qualifier("zlmClient") WebClient zlmClient){
HttpServiceProxyFactory proxyFactory =
HttpServiceProxyFactory.builder(WebClientAdapter.forClient(zlmClient))
.build();
return proxyFactory.createClient(ZlmMediaHttpService.class);
}
}

View File

@ -3,5 +3,6 @@ project:
media:
url: 'http://10.10.10.200:5080'
# url: 'http://10.10.10.200:12580/anything/'
id: amrWMKmbKqoBjRQ9
secret: 4155cca6-2f9f-11ee-85e6-8de4ce2e7333

View File

@ -3,7 +3,9 @@ package cn.skcks.docking.gb28181;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class MediaTestApplication {
public static void main(String[] args) {

View File

@ -4,11 +4,15 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.date.ZoneUtil;
import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.common.json.JsonUtils;
import cn.skcks.docking.gb28181.media.config.ZlmMediaConfig;
import cn.skcks.docking.gb28181.media.dto.config.ApiConfig;
import cn.skcks.docking.gb28181.media.dto.config.ServerConfig;
import cn.skcks.docking.gb28181.media.dto.response.ZlmResponse;
import cn.skcks.docking.gb28181.media.dto.response.ZlmResponseConvertor;
import cn.skcks.docking.gb28181.media.proxy.ZlmMediaHttpService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -30,27 +34,9 @@ import java.util.*;
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ExtendWith(SpringExtension.class)
public class MediaServiceTest {
@Autowired
private ZlmMediaConfig config;
@Autowired
private ZlmMediaHttpService zlMediaHttpService;
@Test
void context(){
String secret = config.getSecret();
ResponseEntity<String> entity = zlMediaHttpService.getServerConfigResponseEntity(secret);
log.info("{}", entity.getBody());
ZlmResponse<List<ServerConfig>> test = zlMediaHttpService.getServerConfig(secret);
JsonResponse<List<ServerConfig>> jsonResponse = test.getJsonResponse();
log.info("{}", jsonResponse);
ZlmResponse<List<ServerConfig>> zlmResponse = ZlmResponseConvertor.INSTANCE.toZlmResponse(jsonResponse);
log.info("{}", zlmResponse);
log.info("{}", zlmResponse.getCode().getMsg());
}
@Test
void test(){
long unix = 1692346604L;
@ -64,4 +50,30 @@ public class MediaServiceTest {
log.info("{}", LocalDateTimeUtil.of(instant, TimeZone.getTimeZone("GMT+8")));
log.info("{}", LocalDateTime.ofEpochSecond(unix, 0, ZoneOffset.ofHours(8)));
}
@Test
void context(){
ResponseEntity<String> entity = zlMediaHttpService.getServerConfigResponseEntity();
log.info("{}", entity.getBody());
ZlmResponse<List<ServerConfig>> test = zlMediaHttpService.getServerConfig();
JsonResponse<List<ServerConfig>> jsonResponse = test.getJsonResponse();
log.info("{}", jsonResponse);
ZlmResponse<List<ServerConfig>> zlmResponse = ZlmResponseConvertor.INSTANCE.toZlmResponse(jsonResponse);
log.info("{}", zlmResponse);
log.info("{}", zlmResponse.getCode().getMsg());
}
@Test
void testApi(){
log.info("{}",zlMediaHttpService.getApiList());
}
@Test
void httpBinTest(){
ZlmResponse<List<ServerConfig>> resp = zlMediaHttpService.getServerConfig();
ServerConfig config = resp.getData().get(0);
log.info("{}", zlMediaHttpService.setServerConfig(config));
}
}