springcloud整合gateway实现网关的示例代码
程序员文章站
2023-12-29 14:33:52
目录1.项目目录:2.代码实现:3.实现效果:1.项目目录:创建项目gateway作为父类2.代码实现:父类依赖 org.sp...
1.项目目录:
创建项目gateway作为父类
2.代码实现:
父类依赖
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.6.2</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.cxh</groupid> <artifactid>gateway</artifactid> <version>0.0.1-snapshot</version> <name>gateway</name> <description>demo project for spring boot</description> <packaging>pom</packaging> <properties> <java.version>8</java.version> <spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version> <spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version> </properties> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-alibaba-dependencies</artifactid> <version>${spring-cloud-alibaba-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>
创建module项目gateway-client
添加依赖
<parent> <groupid>com.cxh</groupid> <artifactid>gateway</artifactid> <version>0.0.1-snapshot</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.cxh</groupid> <artifactid>gateway-client</artifactid> <version>0.0.1-snapshot</version> <name>gateway-client</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <!--服务注册--> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-nacos-discovery</artifactid> <version>0.2.1.release</version> </dependency> <!--服务调用--> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-openfeign</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies>
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
添加依赖
<parent> <groupid>com.cxh</groupid> <artifactid>gateway</artifactid> <version>0.0.1-snapshot</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.cxh</groupid> <artifactid>gateway-service</artifactid> <version>0.0.1-snapshot</version> <name>gateway-service</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-gateway</artifactid> <version>3.0.4</version> </dependency> <!--服务注册/发现中心依赖--> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-nacos-discovery</artifactid> </dependency> <!--服务的配置中心依赖--> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-nacos-config</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-feign</artifactid> <version>1.4.3.release</version> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-loadbalancer</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-hystrix</artifactid> <version>2.2.10.release</version> </dependency> </dependencies>
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
到此这篇关于springcloud整合gateway实现网关的示例代码的文章就介绍到这了,更多相关springcloud gateway网关内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
springcloud整合gateway实现网关的示例代码
-
spring boot整合Shiro实现单点登录的示例代码
-
spring boot整合Shiro实现单点登录的示例代码
-
ThinkPHP整合datatables实现服务端分页的示例代码
-
MyBatis整合Redis实现二级缓存的示例代码
-
Springboot整合Netty实现RPC服务器的示例代码
-
基于Go语言实现的简易api网关的示例代码
-
springboot整合mybatis-plus实现多表分页查询的示例代码
-
SpringBoot整合腾讯云COS对象存储实现文件上传的示例代码
-
Spring整合Quartz实现动态定时器的示例代码