全栈开发笔记之知识点精选(一)解决跨域问题+优雅地关闭系统

如何配置允许跨域访问

现在开发的项目一般都是前后端分离的项目,所以跨域访问会经常使用。

1、单个控制器方法CORS注解

@RestController
@RequestMapping("/system/test")
public class TestController {

    @CrossOrigin
    @GetMapping("/{id}")
    public AjaxResult getUser(@PathVariable Integer userId) {
        // ...
    }
	
	@DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId) {
        // ...
    }
}

2、整个控制器添加CORS注解

@CrossOrigin(origins = "http://localhost:8088/", maxAge = 3600)
@RestController
@RequestMapping("/system/test")
public class TestController {

    @GetMapping("/{id}")
    public AjaxResult getUser(@PathVariable Integer userId) {
        // ...
    }
	
	@DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId) {
        // ...
    }
}

3、全局CORS配置,编写MvcConfig.java

/**
 * @author yuxt
 * @date 2021/4/22
 */
@Configuration
public class MvcConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            /**
             * web跨域访问配置
             */
            @Override
            public void addCorsMappings(CorsRegistry registry)
            {
                // 设置允许跨域的路径
                registry.addMapping("/**")
                        // 设置允许跨域请求的域名
                        .allowedOrigins("*")
                        // 是否允许证书
                        .allowCredentials(true)
                        // 设置允许的方法
                        .allowedMethods("GET", "POST", "DELETE", "PUT")
                        // 设置允许的header属性
                        .allowedHeaders("*")
                        // 跨域允许时间
                        .maxAge(3600);
            }
        };
    }
}

如何优雅地关闭后台系统

一般关闭Java程序的方式:
ps -ef|grep java #获取进程pid
kill -9 pid #杀死进程
存在问题:正在执行的业务操作会被迫停止,可能出现错误的业务数据。

优雅地关闭后台系统,就是指对应用进程发送停止指令之后,能够保证正在执行的业务操作不受影响,可以继续完成已有请求的处理后关闭,同时停止处理新来的请求。

在 Spring Boot 2.3及以后版本中增加了新特性:优雅停止,目前 Spring Boot 内置的四个嵌入式 Web 服务器(Jetty、Reactor Netty、Tomcat 和 Undertow)以及反应式和基于Servlet的Web应用程序都支持优雅停止。

1,添加maven依赖

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
       <version>2.3.0.RELEASE</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
       <version>2.3.0.RELEASE</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
       <version>2.3.0.RELEASE</version>
   </dependency>

2,配置application.yml

management:
  endpoint:
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: shutdown

3、启动程序,访问http://localhost:8080/actuator,得到停止系统的请求路径

4、打开控制台或者使用postman,先发送一个耗时较长的请求,再发送 curl -X POST http://localhost:8080/actuator/shutdown,可以发现在之前的请求返回结果后再返回

{"message": "Shutting down, bye..."}

系统关闭!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜长思

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值