Spring Cloud之旅(五) -- API网关

前言

微服务架构通过API网关将内部的服务提供给外部进行调用,这里通过Zuul实现。

开始创建

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    
    com.asiainfo.aigov
    api-gateway
    0.0.1-SNAPSHOT
    jar
    api-gateway
    http://maven.apache.org

    
        UTF-8
        1.8
        Finchley.RELEASE
    

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

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
    

application.yml

Zuul自身要向注册中心注册,通过路由对请求进行转发

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: api-gateway
zuul:
  routes:
    eureka-client:
      path: /eureka-client/**
      serviceId: eureka-client
    consumer-feign:
      path: /consumer-feign/**
      serviceId: consumer-feign

ApiGatewayApplication

package com.asiainfo.aigov.api_gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ApiGatewayApplication {

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

实际效果

http://localhost:8765/eureka-client/hello?name=pany
http://localhost:8765/consumer-feign/hello2?name=pany

你可能感兴趣的:(Spring Cloud之旅(五) -- API网关)