跨域调用处理
什么是跨域
客户端发起异步请求一个服务器,该服务器再异步请求另一服务器
解决
- 使用jsonp(使用访问回调函数,可以随机生成回调函数名,保证不被劫持,只能通过get请求方式传参)
- 使用cors协议(不能处理劫持-在跨域异步调用时劫持请求,包装再转发-可以使用VPN专线连接保证不被劫持)
cross-origin resource sharing(跨域资源共享)
前后端分离
前端项目与后端项目都可单独运行
使用cors协议
- 使用@Configuration注解(全局)
- 使用@CrossOrigin注解(局部,可用于类或方法)
@CrossOrigin(origins={“http://localhost/”})建议使用域名而非localhost,因为localhost可以被解析为:127.0.0.1 /IP /主机名 /000:00:000:1 /localhost
监控
Actuator插件
用来监控应用的状态
- 引入依赖
<?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> <artifactId>spring-boot-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.1.10.RELEASE</version> </parent> <groupId>cn.khue</groupId> <artifactId>springboot-actuator-test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--actuator依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
- 创建启动类
@SpringBootApplication public class SpringBootApp { public static void main(String[] args) { SpringApplication.run(SpringBootApp.class,args); } }
- 启动,通过http://localhost:8080/actuator路径查看详情
Spring Boot Admin
图形化管理Spring Boot
Spring Boot Admin Server
- 引入依赖
<?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">