Bladeren bron

修改邮件

杨杰 6 jaren geleden
bovenliggende
commit
c864a7efa9

+ 4 - 0
pom.xml

@@ -29,6 +29,10 @@
 			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 		</dependency>
 		<dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-web</artifactId>
 		</dependency>

+ 2 - 0
src/main/java/com/llisoft/pay/Application.java

@@ -5,7 +5,9 @@ import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
 
+@EnableFeignClients // 启用feign
 @EnableEurekaClient // 注册中心客户端 同类注解 @EnableDiscoveryClient
 @SpringBootApplication
 public class Application {

+ 27 - 3
src/main/java/com/llisoft/pay/config/ExceptionConfig.java

@@ -1,7 +1,11 @@
 package com.llisoft.pay.config;
 
+import java.nio.file.AccessDeniedException;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -10,7 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import com.llisoft.common.constant.CodeEnum;
 import com.llisoft.common.exception.MtaException;
+import com.llisoft.common.vo.MailSendVo;
 import com.llisoft.common.vo.ResponseVo;
+import com.llisoft.pay.service.feign.FeignMailService;
 
 /**
  * 统一处理业务异常 controller
@@ -21,6 +27,12 @@ public class ExceptionConfig {
 	
 	private Logger logger = LoggerFactory.getLogger(ExceptionConfig.class);
 	
+	@Value("${spring.application.name}")
+	private String application;
+	
+	@Autowired
+	private FeignMailService feignMailService;
+	
 
     /**
      * 入参空
@@ -29,7 +41,7 @@ public class ExceptionConfig {
      */
     @ExceptionHandler(HttpMessageNotReadableException.class)
     public @ResponseBody ResponseVo<String> exception(HttpMessageNotReadableException exception){
-    	logger.warn("入参异常", exception);
+    	logger.warn("入参异常: {}", exception.getMessage());
         return ResponseVo.build(CodeEnum.C400);
     }
     
@@ -40,18 +52,29 @@ public class ExceptionConfig {
      */
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public @ResponseBody ResponseVo<String> exception(MethodArgumentNotValidException exception){
-    	logger.warn("入参异常", exception);
+    	logger.warn("入参异常: {}", exception.getMessage());
     	return ResponseVo.build(CodeEnum.C400.getCode(), exception.getBindingResult().getFieldError().getDefaultMessage());
     }
     
     /**
+     * 无权访问
+     * @param exception
+     * @return
+     */
+    @ExceptionHandler(AccessDeniedException.class)
+    public @ResponseBody ResponseVo<String> exception(AccessDeniedException exception){
+    	logger.warn("无权访问: {}", exception.getMessage());
+    	return ResponseVo.build(CodeEnum.C403);
+    }
+	
+    /**
      * 业务异常
      * @param exception
      * @return
      */
     @ExceptionHandler(MtaException.class)
     public @ResponseBody ResponseVo<String> exception(MtaException exception){
-    	logger.warn("业务异常", exception);
+    	logger.warn("业务异常: {}", exception.getMessage());
     	return ResponseVo.build(CodeEnum.FAIL.getCode(), exception.getMessage());
     }
     
@@ -63,6 +86,7 @@ public class ExceptionConfig {
     @ExceptionHandler(Exception.class)
     public @ResponseBody ResponseVo<String> exception(Exception exception){
     	logger.error("服务异常", exception);
+    	feignMailService.send(MailSendVo.build("【" + application + "】服务异常", exception));
     	return ResponseVo.build(CodeEnum.C500);
     }
     

+ 15 - 0
src/main/java/com/llisoft/pay/service/feign/FeignMailService.java

@@ -0,0 +1,15 @@
+package com.llisoft.pay.service.feign;
+
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+
+import com.llisoft.common.vo.MailSendVo;
+
+// 通过@FeignClient注解来指定服务名进而绑定服务
+@FeignClient(value="mta-service-sms", fallback=FeignMailServiceHystric.class)
+public interface FeignMailService {
+
+	@PostMapping("/feign/mail/send")
+	public void send(MailSendVo mailSendVo);
+	
+}

+ 13 - 0
src/main/java/com/llisoft/pay/service/feign/FeignMailServiceHystric.java

@@ -0,0 +1,13 @@
+package com.llisoft.pay.service.feign;
+
+import org.springframework.stereotype.Component;
+
+import com.llisoft.common.vo.MailSendVo;
+
+@Component // 配置断路器
+public class FeignMailServiceHystric implements FeignMailService{
+
+	@Override
+	public void send(MailSendVo mailSendVo) {}
+	
+}