欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > Spring Boot 中如何解决跨域问题、Spring Cloud 5大组件、微服务的优缺点是什么?

Spring Boot 中如何解决跨域问题、Spring Cloud 5大组件、微服务的优缺点是什么?

2024/10/25 12:18:05 来源:https://blog.csdn.net/2302_79020600/article/details/139658639  浏览:    关键词:Spring Boot 中如何解决跨域问题、Spring Cloud 5大组件、微服务的优缺点是什么?

Spring Boot 中如何解决跨域问题 ?

SpringMVC项目中使用@CrossOrigin注解来解决跨域问题 , 本质是CORS

 @RequestMapping("/hello")@CrossOrigin(origins = "*")//@CrossOrigin(value = "http://localhost:8081") //指定具体ip允许跨域public String hello() {return "hello world";}

SpringBoot项目采用自动配置的方式来配置CORS , 可以通过实现 WebMvcConfigurer接口然后重写addCorsMappings方法解决跨域问题。

 @Configurationpublic class CorsConfig implements WebMvcConfigurer {​@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**")//是否发送Cookie.allowCredentials(true)//放行哪些原始域.allowedOrigins("*").allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"}).allowedHeaders("*").exposedHeaders("*");}}

在SpringCloud项目中一般都会有网关 , 在网关中可以配置CORS跨域, 这样所有通过网关的请求都解决了跨域问题

 spring:cloud:gateway:globalcors:cors-configurations:'[/**]': # 匹配所有请求allowedOrigins: "*" #跨域处理 允许所有的域allowedMethods: # 支持的方法- GET- POST- PUT- DELETE

09- 你们项目中使用的SpringBoot是哪个版本 ?

  • SpringBoot : 2.3.4.RELEASE

  • SpringCloud : Hoxton.SR10

  • SpringCloudAlibaba : 2.2.5.RELEASE

10- Spring Cloud 5大组件有哪些?

早期我们一般认为的Spring Cloud五大组件是

  • Eureka : 注册中心

  • Ribbon : 负载均衡

  • Feign : 远程调用

  • Hystrix : 服务熔断

  • Zuul/Gateway : 网关

随着SpringCloudAlibba在国内兴起 , 我们项目中使用了一些阿里巴巴的组件

  • 注册中心/配置中心 Nacos

  • 负载均衡 Ribbon

  • 服务调用 Feign

  • 服务保护 sentinel

  • 服务网关 Gateway

11- 什么是微服务?微服务的优缺点是什么?

微服务就是一个独立的职责单一的服务应用程序,一个模块

1.优点:松耦合,聚焦单一业务功能,无关开发语言,团队规模降低 , 扩展性好, 天然支持分库 2.缺点:随着服务数量增加,管理复杂,部署复杂,服务器需要增多,服务通信和调用压力增大

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com