springcloud+zuul+eureka
网关的设计用两个目的:
第一:一个有效的请求可以获取到正确的资源,不正常的请求可以被拦截,合法性验证。
第二:可以转发这个请求,给下层的服务。下层的服务只需要关注自己的业务处理,对于请求是否有效,不用过多的考虑。
这个https://blog.csdn.net/liuc0317/article/details/80360594 是传统的网关设计。
接下来,我们看一下zuul的使用。
项目图:
主程序代码:
package com.XXX.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableZuulProxy
@ComponentScan(basePackages = {"com.XXX.gateway"})
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
@SpringBootApplication 注解是springBoot 项目
@EnableZuulProxy注解是启用springcloud 的网关服务。
@ComponentScan 要扫描 后边包下的所有Bean ,并且让spring 的上下文进行管理.