搭建过程如common模块
[](()3.2、修改配置pom.xml
com.study
common_util
org.springframework.cloud
spring-cloud-starter-gateway
2.2.1.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.1.RELEASE
[](()3.3、修改配置文件application.properties
服务端口
server.port=80
服务名spring.application.name=service-gateway
nacos服务地址spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true
#设置路由id
spring.cloud.gateway.routes[0].id=service-hosp
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-hosp
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=cmn/**
[](()3.4、创建启动类
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class ServerGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ServerGatewayApplication.class, args);
}
}
[](()3.5、修改前端配置
使用网关替代nginx
[](()3.6、跨域处理
=======================================================================
跨域:浏览器对于javascript的同源策略的限制 。
以下情况都属于跨域:
如果域名和端口都相同,但是请求路径不同,不属于跨域,如:
www.jd.com/item
www.jd.com/goods
http和https也属于跨域
而我们刚才是从localhost:1000去访问localhost:8888,这属于端口不同,跨域了。
[](()3.6.1、跨域处理为什么有跨域问题?
跨域不一定都会有跨域问题。
因为跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是与当前页域名相同的路径,这能有效的阻止跨站攻击。
因此:跨域问题 是针对ajax的一种限制。



