springcloud2 gateway 之二:转发调用eureka客户端服务

gateway不仅能转发api接口请求,也能转发页面请求,返回页面。

springcloud2 gateway 之一:简单样例
https://blog.csdn.net/haveqing/article/details/88424598

springcloud eureka注册中心搭建
https://blog.csdn.net/haveqing/article/details/86479813

eureka 客户端springcloud service
https://blog.csdn.net/haveqing/article/details/88427366

文件结构

springcloud2 gateway 之二:转发调用eureka客户端服务_第1张图片

一、pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
         
    
    com.urthink.upfs
    springcloud-gateway
    0.0.1-SNAPSHOT
    springcloud-gateway
    springcloud-gateway project for Spring Boot

    
        1.8
        Finchley.SR1
    

    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    


二、SpringcloudGatewayApplication.java

package com.urthink.upfs.springcloudgateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * http://localhost:8081/demo/test1
 * http://localhost:8080/app1/demo/test1
 * http://localhost:8082/demo/test1
 * http://localhost:8080/app2/demo/test1
 */
@SpringBootApplication
public class SpringcloudGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudGatewayApplication.class, args);
    }

}

三、application.yml

server:
  port: 8080

eureka:
  instance:
    prefer-ip-address: true
    #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则),默认30
    lease-renewal-interval-in-seconds: 10
    #Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己),默认90
    lease-expiration-duration-in-seconds: 12
  client:
    registry-fetch-interval-seconds: 10 #eureka client刷新本地缓存时间,默认30
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        #netty 路由过滤器,http或https开头
      - id: app1-route
        uri: http://localhost:8081
        predicates:
        - Path=/app1/**
        filters:
          #转发请求时去掉1级前缀
        - StripPrefix=1
        #LoadBalancerClient负载均衡过滤器(整合eureka注册中心),lb开头
      - id: app2-route
        uri: lb://springcloud-service
        predicates:
        - Path=/app2/**
        filters:
        #转发请求时去掉1级前缀
        - StripPrefix=1

四、访问

http://localhost:8082/demo/test1
http://localhost:8080/app2/demo/test1

 

你可能感兴趣的:(springcloud2 gateway 之二:转发调用eureka客户端服务)