zoukankan      html  css  js  c++  java
  • SpringCloud--gateway路由配置

    第一步: 依赖

    1         <dependency>
    2             <groupId>org.springframework.cloud</groupId>
    3             <artifactId>spring-cloud-starter-gateway</artifactId>
    4         </dependency>
    5         <dependency>
    6             <groupId>org.springframework.cloud</groupId>
    7             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    8         </dependency>

    第二步: 启动类

    • 将网关服务注册到注册中心
    1 @SpringBootApplication
    2 @EnableDiscoveryClient
    3 public class GateWayApplication {
    4 
    5     public static void main(String[] args) {
    6         SpringApplication.run(GateWayApplication.class,args);
    7     }
    8 }

     第三步: 基础配置文件

     1  1 server:
     2  2   port: 9999
     3  3 spring:
     4  4   application:
     5  5     name: gateway
     6  6 # 如果的使用动态路由 网关本身必须要连接到Eureka上面
     7  7 eureka:
     8  8   client:
     9  9     service-url:
    10 10       # eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
    11 11       defaultZone: http://127.0.0.1:10086/eureka
    12 12     # 是否注册到服务端
    13 13     register-with-eureka: true
    14 14     # 是否拉取服务列表
    15 15     fetch-registry: true
    16 16   instance:
    17 17     ip-address: 127.0.0.1 # 服务的ip地址
    18 18     prefer-ip-address: true # 启用ip地址访问

    第四步: 配置路由到配置文件

    • 第一种: 静态路由
     1 server:
     2   port: 9999
     3 spring:
     4   application:
     5     name: gateway
     6   cloud:
     7     gateway:
     8       routes:
     9         # 路由id,可以随意写
    10         - id: user-service-route
    11         # 代理的服务地址
    12           uri: http://127.0.0.1:8082
    13         # 路由断言,可以配置映射路径
    14           predicates:
    15             - Path=/user/**
    • 第二种: 动态路由(更好)
      •   在静态路由的基础上修改路由配置 的uri 为应用名称(前提:网关要先注册到注册中心)
     1 server:
     2   port: 9999
     3 spring:
     4   application:
     5     name: gateway
     6   cloud:
     7     gateway:
     8       routes:
     9         # 路由id,可以随意写
    10         - id: user-service-route
    11         # 代理的服务地址
    12         # lb: 自动实现负载均衡
    13         # user-server为要访问的服务名
    14           uri: lb://user-server
    15         # 路由断言,可以配置映射路径
    16           predicates:
    17             - Path=/user/**
  • 相关阅读:
    kubernetes(七)二进制安装-worker节点安装
    kubernetes(六)二进制安装-master节点安装
    B树
    Nginx反向代理无法达到效果原因
    AVL树
    平衡二叉搜索树
    二叉搜索树
    二叉树
    js-day06-jQuery事件和DOM操作-练习题
    js-day05-JSON-jQuery初体验
  • 原文地址:https://www.cnblogs.com/bin563597293/p/14350591.html
Copyright © 2011-2022 走看看