springcloud整合gateway实现网关的示例代码
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网关内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot2.0集成Swagger2访问404的解决操作
这篇文章主要介绍了SpringBoot2.0集成Swagger2访问404的解决操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-09-09java中元素排序Comparable和Comparator的区别
本文主要介绍了java中元素排序Comparable和Comparator的区别,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-12-12Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法
这篇文章主要给大家介绍了关于Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们来一起看看吧2018-05-05Java 使用JdbcTemplate 中的queryForList发生错误解决办法
这篇文章主要介绍了Java 使用JdbcTemplate 中的queryForList发生错误解决办法的相关资料,需要的朋友可以参考下2017-07-07
最新评论