first commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>maint-dev</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>tocc-system-maint</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<description>养护系统服务</description>
|
||||
<modules>
|
||||
<module>tocc-system-maint-api</module>
|
||||
<module>tocc-system-maint-biz</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>tocc-system-maint</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>tocc-system-maint-api</artifactId>
|
||||
<description>养护系统服务接口</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>maint-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.swagger.core.v3</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>2.2.8</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.wanji.software.tocc.system.maint.constants;
|
||||
|
||||
/**
|
||||
* 服务名需要和 spring.application.name 保持一致。
|
||||
*/
|
||||
public final class ApiConstants {
|
||||
|
||||
public static final String NAME = "maint-system";
|
||||
|
||||
public static final String PREFIX = "/rpc-api/maint-system";
|
||||
|
||||
public static final String VERSION = "1.0.0";
|
||||
|
||||
private ApiConstants() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>tocc-system-maint</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>tocc-system-maint-biz</artifactId>
|
||||
<description>养护系统服务,给前端提供接口,通过 Feign 调用基础服务</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>tocc-system-maint-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>tocc-infra-maint-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.wanji.software</groupId>
|
||||
<artifactId>maint-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.wanji.software.tocc.system.maint;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* 养护系统服务。页面接口放在本进程,数据库访问调用基础服务。
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {
|
||||
"com.wanji.software.tocc.system.maint",
|
||||
"com.wanji.software.tocc.maint.support"
|
||||
})
|
||||
@EnableFeignClients(basePackages = "com.wanji.software.tocc.infra.maint.api")
|
||||
public class MaintSystemApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MaintSystemApplication.class, args);
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.wanji.software.tocc.system.maint.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.wanji.software.tocc.common.exception.BusinessException;
|
||||
import com.wanji.software.tocc.common.model.Result;
|
||||
import feign.FeignException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 统一异常处理。业务异常回短文案;调用基础服务失败或系统异常记日志后返回固定提示。
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class ExceptionAdvice {
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public Result<Void> handleBusiness(BusinessException exception) {
|
||||
log.warn("业务异常: {}", exception.getMessage());
|
||||
return Result.fail(exception.getCode() == null ? 1 : exception.getCode(),
|
||||
safeMsg(exception.getMessage(), "请求失败"));
|
||||
}
|
||||
|
||||
@ExceptionHandler(FeignException.class)
|
||||
public Result<Void> handleFeign(FeignException exception) {
|
||||
log.error("调用基础服务失败", exception);
|
||||
String remoteMsg = readRemoteMsg(exception);
|
||||
return Result.fail(safeMsg(remoteMsg, "基础服务暂时不可用"));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Result<Void> handle(Exception exception) {
|
||||
log.error("系统异常", exception);
|
||||
return Result.fail("系统繁忙,请稍后再试");
|
||||
}
|
||||
|
||||
private String readRemoteMsg(FeignException exception) {
|
||||
try {
|
||||
byte[] body = exception.content();
|
||||
if (body == null || body.length == 0) {
|
||||
return null;
|
||||
}
|
||||
Result<?> result = objectMapper.readValue(body, Result.class);
|
||||
return result == null ? null : result.getMsg();
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String safeMsg(String message, String fallback) {
|
||||
return message == null || message.trim().isEmpty() ? fallback : message;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.wanji.software.tocc.system.maint.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.wanji.software.tocc.common.exception.BusinessException;
|
||||
import com.wanji.software.tocc.common.model.Result;
|
||||
import com.wanji.software.tocc.maint.support.CurrentUser;
|
||||
import com.wanji.software.tocc.maint.support.CurrentUserHolder;
|
||||
import com.wanji.software.tocc.maint.support.LocalDevAuthFilter;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
import feign.Util;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* 调用基础服务时带上当前用户;非 2xx 时尽量还原远端 Result 文案。
|
||||
*/
|
||||
@Configuration
|
||||
public class MaintFeignConfig {
|
||||
|
||||
@Bean
|
||||
public RequestInterceptor currentUserInterceptor() {
|
||||
return template -> {
|
||||
CurrentUser current = CurrentUserHolder.get();
|
||||
template.header(LocalDevAuthFilter.USER_ID_HEADER, current.getUserId());
|
||||
template.header(LocalDevAuthFilter.USER_NAME_HEADER, encode(current.getUserName()));
|
||||
template.header(LocalDevAuthFilter.ORG_ID_HEADER, current.getOrgId());
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorDecoder maintErrorDecoder(ObjectMapper objectMapper) {
|
||||
return (methodKey, response) -> {
|
||||
String remoteMsg = readRemoteMsg(objectMapper, response);
|
||||
if (remoteMsg != null) {
|
||||
return new BusinessException(remoteMsg);
|
||||
}
|
||||
return new BusinessException("基础服务暂时不可用");
|
||||
};
|
||||
}
|
||||
|
||||
private String readRemoteMsg(ObjectMapper objectMapper, Response response) {
|
||||
if (response == null || response.body() == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
String body = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
|
||||
Result<?> result = objectMapper.readValue(body, Result.class);
|
||||
if (result == null || result.getMsg() == null || result.getMsg().trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return result.getMsg();
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String encode(String value) {
|
||||
try {
|
||||
return URLEncoder.encode(value, "UTF-8");
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.wanji.software.tocc.system.maint.controller;
|
||||
|
||||
import com.wanji.software.tocc.common.model.Result;
|
||||
import com.wanji.software.tocc.infra.maint.api.BridgeApi;
|
||||
import com.wanji.software.tocc.infra.maint.api.MaintHealthApi;
|
||||
import com.wanji.software.tocc.infra.maint.model.dto.BridgeResp;
|
||||
import com.wanji.software.tocc.maint.support.CurrentUserHolder;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 给前端的接口放在这个包。需要查库时注入基础服务的 Feign 接口,不要在本服务写 Mapper。
|
||||
*/
|
||||
@Tag(name = "道路养护管理")
|
||||
@RestController
|
||||
@RequestMapping("/maint")
|
||||
public class MaintController {
|
||||
|
||||
@Resource
|
||||
private MaintHealthApi maintHealthApi;
|
||||
|
||||
@Resource
|
||||
private BridgeApi bridgeApi;
|
||||
|
||||
@GetMapping("/health")
|
||||
@Operation(summary = "检查基础服务是否可用")
|
||||
public Result<String> health() {
|
||||
return maintHealthApi.health();
|
||||
}
|
||||
|
||||
@GetMapping("/current-user")
|
||||
@Operation(summary = "查看本地开发使用的当前用户")
|
||||
public Result<String> currentUser() {
|
||||
return Result.ok(CurrentUserHolder.get().getUserName());
|
||||
}
|
||||
|
||||
@GetMapping("/bridge")
|
||||
@Operation(summary = "查看本地开发使用的当前用户")
|
||||
public Result<BridgeResp> getBridgeById(@RequestParam("id") String id) {
|
||||
return Result.ok(bridgeApi.getById(id).getCheckedData());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
server:
|
||||
port: 8525
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: maint-system
|
||||
autoconfigure:
|
||||
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
||||
|
||||
maint:
|
||||
infra:
|
||||
url: http://127.0.0.1:7424
|
||||
|
||||
springdoc:
|
||||
api-docs:
|
||||
enabled: true
|
||||
path: /v3/api-docs
|
||||
swagger-ui:
|
||||
enabled: true
|
||||
path: /swagger-ui.html
|
||||
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
language: zh_cn
|
||||
Reference in New Issue
Block a user