一起艳学天气微服务(四)——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网关’