本篇文章小编给大家分享一下springcloud整合gateway实现网关代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.项目目录:
创建项目gateway作为父类
2.代码实现:
父类依赖
org.springframework.boot spring-boot-starter-parent 2.6.2 com.cxh gateway 0.0.1-SNAPSHOT gateway Demo project for Spring Boot pom 8 2021.1 2021.0.0 org.springframework.cloud spring-cloud-dependencies ${spring-cloud-dependencies.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba-dependencies.version} pom import
创建module项目gateway-client
添加依赖
com.cxh gateway 0.0.1-SNAPSHOT com.cxh gateway-client 0.0.1-SNAPSHOT gateway-client Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.2.1.RELEASE org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-web
yml配置
server: port: 8002 spring: application: name: gateway-client #服务名 profiles: active: dev #环境设置 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 #nacos服务注册
控制层
@RestController public class ClientController { @Value("${server.port}") private String port; @RequestMapping("/index") public String index(){ return "gateway-client端口:" + port; } }
启动类添加注解
@SpringBootApplication @EnableDiscoveryClient public class GatewayClientApplication { public static void main(String[] args) { SpringApplication.run(GatewayClientApplication.class, args); } }
创建module项目gateway-service
添加依赖
com.cxh gateway 0.0.1-SNAPSHOT com.cxh gateway-service 0.0.1-SNAPSHOT gateway-service Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-gateway 3.0.4 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.cloud spring-cloud-starter-feign 1.4.3.RELEASE org.springframework.cloud spring-cloud-loadbalancer org.springframework.cloud spring-cloud-starter-netflix-hystrix 2.2.10.RELEASE
yml配置
server: port: 8001 spring: application: name: gateway-service #服务名 profiles: active: dev #环境设置 cloud: gateway: routes: # 透传服务 - id: gateway-client #设置路由id uri: lb://gateway-client #设置路由的url lb://nacos服务注册名称 predicates: - Path=/client/** #路径匹配规则 filters: - StripPrefix=1
跨域配置
@Configuration public class CorsConfig { @Bean public CorsWebFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedMethod("*"); config.addAllowedOrigin("*"); config.addAllowedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); source.registerCorsConfiguration("/**", config); return new CorsWebFilter(source); } }
3.实现效果:
启动nacos后,再启动gateway-client, gateway-service项目,打开浏览器http://localhost:8001/client/index
返回信息:gateway-client端口:8002
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级