一起艳学天气微服务(四)——API网关

一起艳学天气微服务(四)——API网关

这里写图片描述
api网关,用一套单一且统一的API入口点,可用于黑白名单,日志,协议适配,身份认证,计流限流,路由等。
目前常见API网关常用有nginx,zuul,Kong。今天,就说说zuul,将以/city/* 城市api和 /weather/* 天气api为例,基于Zuul来实现API网关。

项目已经成功运行了,但遇到的坑,难以忘记。。。

启动Zuul

启动很简单,就加个注解@EnableZuulProxy
配置文件:

server.port=8089

spring.application.name: micro-weather-eureka-client-zuul

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

zuul.routes.city.path: /city/**
zuul.routes.city.serviceId: yh-weather1-eureka-client
zuul.routes.weather.path: /weather/**
zuul.routes.weather.serviceId: yh-weather-eureka-client

hystrix.command.default.execution.timeout.enabled: false

要先开启eureka-server服务,再逐个开启微服务,最后才开启网关。
开启网关的pom

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
@FeignClient("micro-weather-eureka-client-zuul")
public interface DataClient {

    @RequestMapping(value="/city/getCity", method=RequestMethod.GET)
    Result listCity() throws Exception;

    @RequestMapping(value="/weather/cityName/{cityName}", method=RequestMethod.GET)
    ScenicResponse getDataByCityId(@PathVariable("cityName") String cityName);
}

这里的@FeignClient要指向zuul项目的spring.application.name

需要源码学习可加QQ490647751回复‘开通VIP-一起艳学天气微服务(四)——API网关’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值