Spring Cloud 系列三《微服务网关(Spring Cloud Gateway)》

0 前言

Zuul,在Spring Cloud 2020.0.3以后,不在包含。多数示例已经不能使用。选型后,排除Soul,Nginx+Lua,选择了Spring Cloud GateWay。
在《Spring Cloud 二》基础上,
对每个示例的服务示例的端口做了调整:

- 8000 springcloud-eureka-server
- 8001 springcound-config-server
- 8002 springcloud-eureka-servicesupport
- 8003 springcloud-eureka-ribbonconsumer 
- 8004 springcloud-eureka-feignconsumer
- 8005 spring-cloud-config-client
- 8006 spring-cloud-hystrix-dashboard
- 8007 com.yun.gateway

1 代码截图

在这里插入图片描述

2 pom

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

3 bootstrap

server:
  port: 8007
spring:
  application:
    name: gateway-service
  cloud:
    config:
      name: ${spring.application.name} #对应config server Url中的{application}
      profile: dev
      discovery:
        enabled: true #启用服务发现
        service-id: spring-cloud-config-server #指定要从eureka获取的config server的服务ID(即:configserverr的applicationName)
    gateway:
      filter:
        remove-non-proxy-headers:
          headers:
            - dummy
      routes:
        - id:  apiuser #配置了一个路由apiuser
          # 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
          uri: lb://eureka-support
          predicates:
            # 重点!转发该路径!,/userapi/**,
            - Path=/provider/**
          # http://localhost:6601/userapi/user/users/2, 必须加上StripPrefix=1,否则访问服务时会带上userapi
          #而不是我们期望的去掉userapi,只保留**部分
          filters:
            - StripPrefix=1
        - id:  api2user
          uri: lb://user-service
          predicates:
            - Path=/userapi2/**
          filters:
            - StripPrefix=1
management:
  endpoints:
    web:
      exposure:
        include: "*" #暴露所有端口,也可以指定某一个环境(先management.endpoint.{profile}.enabled=true,然后这里指定这个{profile},多个用,分隔)
eureka:
  client:
    service-url:
      defaultZone: "http://${eureka.instance.hostname}:8000/eureka/"
  instance:
    hostname: localhost

4、GatewayApplication

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class,args);
    }
}

5、测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值