API网关:Zuul

继续上篇。这篇我们会类似地创建一个Zuul网关用于对所有API进行代理。

引入依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

同样地,采用动态配置的模式。下述配置加入配置仓库中:

server:
  port: 8888
 
spring:
  application:
    name: zuul-service
  security:
    user:
      name: admin
      password: 123456
  cloud:
    loadbalancer:
      retry:
        enabled: true
      cache:
        ttl: 30
 
zuul:
  routes: #router
    app:
      path: /api/**
      serviceId: test-service
    iot:
      path: /iot/**
      serviceId: iot-service
    user:
      path: /user/**
      serviceId: user-service
  strip-prefix: false
  host:
    connect-timeout-millis: 8000
    socket-timeout-millis: 4000
  ratelimit:
    enabled: false
    default-policy:
      limit: 1
      quota: 100
      refresh-interval: 100
  retryable: true
  add-host-header: true
 
ribbon:
  ReadTimeout: 1000
  ConnectTimeout: 500
 
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7000/eureka
  instance:
    prefer-ip-address: true
 
feign:
  hystrix:
    enabled: true
 
management:
  endpoints:
    web:
      exposure:
        include: 'routes'

本地bootstrap.yml保留如下配置:

spring:
  application:
    name: zuul-service
  cloud:
    config:
      profile: dev
      uri: http://localhost:7011/
      username: config-admin
      password: 123456

启动类添加如下注解:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient

Eureka中可以读取到注册信息:

 

posted @ 2020-10-15 14:55  猎喵Rachel  阅读(119)  评论(0)    收藏  举报