软件系统开发定制SpringCloud——GateWay网关(详解+案例)

目录

 


 

一、相关概念

1、概念

Gateway是在spring软件系统开发定制生态系统之上构建的API网关服务,软件系统开发定制网关是外网进入内网的入口,软件系统开发定制对内网服务起保护作用。

2、网关作用

(1)反向代理:软件系统开发定制为保护内网服务的安全,软件系统开发定制通常不会暴露内网服务ip,而是通过暴露网关ip,通过网关ip代理内网服务ip。

(2)流量控制:当系统处于高峰期时,为防止系统应访问量过大而崩溃,网关可以限制访问数量。

(3)熔断:当系统中的服务出现故障,网关可以将服务降级,有请求访问时,直接访问事先准备好的降级方法,等到服务修复后,用户即可继续访问;

(4)负载均衡:可以按照不同负载均衡策略,将请求分发到不同服务上。

3、网关图

 

 

4、网关三大核心

(1)路由:路由是构建网关的基本模块,它由Id,目标url,一系列断言和过滤器组成,如果断言为true,则可以匹配该路由。

(2)断言:指定路径url,只有请求路径与这个指定的url匹配,才可以进行路由。

(3)过滤器:使用过滤器,可以在请求被路由前或者之后对请求进行修改。

二、案例

1、案例说明

(1)一个Gateway网关9527,一个服务提供者9001,一个注册中心Eureka7001;

(2)网关设置断言url为:hello/**,即请求url只有为hello开头才可以进行访问;

(3)客户端请求访问网关IP,访问到服务提供者9001。

 2、搭建GateWay网关9527服务

(1)创建工程

 (2)导入依赖

导入网关、熔断器,依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-gateway</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  13. </dependency>
  14. </dependencies>

 (3)配置application.yml文件

①配置网关端口号为9527;

②配置网关断言,只有请求url为/hello开头的请求才可以访问,断言id必须唯一,url为提供服务的路由地址

③配置Eureka注册中心

  1. server:
  2. port: 9527
  3. spring:
  4. application:
  5. name: gate9527
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: provider9001 #路由的ID,没有固定规则但要求唯一,建议配合服务名
  10. uri: http://localhost:9001 #匹配后提供服务的路由地址
  11. predicates:
  12. - Path=/hello/** #断言,路径相匹配的进行路由
  13. eureka:
  14. instance:
  15. hostname: cloud-gate9527
  16. client:
  17. register-with-eureka: true
  18. fetch-registry: true
  19. service-url:
  20. defaultZone: http://localhost:7001/eureka

(4)创建主启动类

开启注册中心客户端

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class GateWay9527{
  4. public static void main(String[] args) {
  5. SpringApplication.run(GateWay9527.class,args);
  6. }
  7. }

3、搭建服务提供者Provider9001

 (1)创建maven工程

 (2)导入依赖

导入eureka、web、服务监控依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. </dependencies>

(3)配置application.yml文件

①配置端口号为9001;

②配置服务注册中心地址。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: Provider9001
  6. eureka:
  7. client:
  8. register-with-eureka: true
  9. fetchRegistry: true
  10. service-url:
  11. defaultZone: http://localhost:7001/eureka

(4)创建主启动类

开启Eureka注册中心

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class Provider9001 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Provider9001.class,args);
  6. }
  7. }

(5)创建controller

创建测试请求,/hello/hi和/find

  1. @RestController
  2. public class HelloController {
  3. @RequestMapping("/hello/hi")
  4. public String hello(){
  5. return "路由/hello/hi";
  6. }
  7. @RequestMapping("/find")
  8. public String find(){
  9. return "路由/find";
  10. }
  11. }

4、搭建Eureka7001

(1)创建maven工程

 (2)导入依赖

导入Eureka服务端、web模块依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. </dependencies>

(3)配置application.yml

①服务端口为7001;

②Eureka服务端主机名;

③Eureka客户端:

register-with-eureka:是否在服务中心注册

fetchRegistry:是否可以在注册中心被发现

service-url:服务中心url地址

  1. server:
  2. port: 7001
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. register-with-eureka: false
  8. fetchRegistry: false
  9. service-url:
  10. defaultZone: http://localhost:7001/eureka

(4)创建主启动类

@EnableEurekaServer:Eureka服务端注解

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class Eureka7001 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Eureka7001.class,args);
  6. }
  7. }

(5)启动Eureka注册中心

访问http://localhost:7001

5、测试

(1)依次启动Eureka7001,Provider9001,GateWay9527

(2)访问:http://localhost:9527/hello/hi

(3)访问:http://localhost:9527/find

 

 

 路由/hello/hi可以正常访问,而路由/find无法访问,测试成功。

三、欢迎回访我的springCloud专栏 

 

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发