目录
- zuul
- 系统架构和zuul的作用
- zuul网关实现
- 配置映射路径
- 过滤器
- Config
- 概述
- 云端管理
- 本地配置
zuul
zuul是分布式和集群后前端统一访问入口
系统架构和zuul的作用
zuul把自己注册进eureka,然后可通过前端传来的服务名发现和访问对应的服务集群
为了预防zuul单点故障,zuul也要做集群,所以要在前面再加个nginx反向代理,nginx本身也有负载均衡的作用
为了预防nginx单点故障,由keepalived管理,配置主备切换,配置相同的虚拟ip给前端访问
正常开发只需要负责到zuul层,keepalived和nginx由运维搭
zuul网关实现
创建新模块zuul-gateway-2010
导包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency>
application.yml
server:port: 2010
spring:application:name: zuul-gateway
eureka:client:service-url:defaultZone: http://eureka1:1010/eureka,http://eureka2:1020/eureka,http://eureka3:1030/eurekainstance:instance-id: zuul:2010prefer-ip-address: false
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy //zuul网关的注解
public class ZuulApp {public static void main(String[] args) {SpringApplication.run(ZuulApp.class,args);}
}
启动eureka集群,生产者和网关,浏览器访问测试:http://localhost:2010/user-provider/user/provider/3
配置映射路径
可隐藏服务名,加前缀
浏览器访问测试:http://localhost:2010/services/myUser/user/provider/100
过滤器
核心方法,执行流程和使用场景见文档3.4.1,3.4.2,3.4.3
代码实现
@Component
public class LoginFilter extends ZuulFilter {//过滤器类型 pre 前置 routing 路由 post 后置 error 错误@Overridepublic String filterType() {return "pre";}//执行的顺序 数字越小 越先执行@Overridepublic int filterOrder() {return 0;}//判断该过滤器是否需要执行@Overridepublic boolean shouldFilter() {return true;}//业务逻辑 例如登录的token验证@Overridepublic Object run() throws ZuulException {//获取当前请求上下文RequestContext context = RequestContext.getCurrentContext();//从上下文里面获取请求对象HttpServletRequest request = context.getRequest();String token = request.getHeader("token");if (token==null||"".equals(token.trim())){//返回错误的结果context.setSendZuulResponse(false);//设置响应码context.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());}//不为空,去redis里面查询return null;}
}
浏览器不带token访问和postman带token访问测试:http://localhost:2010/services/myUser/user/provider/101
zuul自带负载均衡和断路器,配置即可
tips:这里的负载均衡和断路器作用于前端通过zuul访问服务集群,不同于之前的消费者访问生产者集群
zuul:retryable: true
ribbon:ConnectTimeout: 250 # 连接超时时间(ms)ReadTimeout: 2000 # 通信超时时间(ms)OkToRetryOnAllOperations: true # 是否对所有操作重试MaxAutoRetriesNextServer: 2 # 同一服务不同实例的重试次数MaxAutoRetries: 1 # 同一实例的重试次数
hystrix:command:default:execution:isolation:thread:timeoutInMillisecond: 3000 # 熔断超时时长:3000ms
Config
概述
见文档4.2.1,4.2.2
云端管理
1.Gitee创建仓库
添加文件application-zuul-test.yml,复制配置文件内容到其中,端口2010
添加文件application-zuul-dev.yml,复制配置文件内容到其中,可改动端口为2020做区分
2.新建项目config-server-3010,
导包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--配置中心支持--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>
配置
server:port: 3010
spring:application:name: config-servercloud:config:server:git:uri: https://gitee.com/ming-feng/config-center.gitusername: 513585429@qq.compassword: ***********
启动类
@SpringBootApplication
@EnableConfigServer //启用配置服务端
public class ConfigServerApp {public static void main(String[] args) {SpringApplication.run(ConfigServerApp.class);}
}
启动项目,浏览器访问测试:http://localhost:3010/application-zuul-dev.yml或
http://localhost:3010/application-zuul/test
3.以zuul项目为例,修改配置文件名让原来的配置文件失效
导包
<!--configclient端--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency>
添加配置文件bootstrap.yml
spring:cloud:config:name: application-zuul #gitee上面名称profile: dev #环境label: master #分支uri: http://127.0.0.1:3010 #配置服务器
启动项目,浏览器可访问测试:http://localhost:2020/services/myUser/user/provider/104
本地配置
克隆gitee项目到本地电脑
configserver项目添加本地文件路径配置
server:port: 3010
spring:application:name: config-server
# cloud:
# config:
# server:
# git:
# uri: https://gitee.com/ming-feng/config-center.git
# username: 513585429@qq.com
# password: gt513585429cloud:config:server:native: #在本地目录搜索search-locations: file:D:\\code\\config-centerprofiles:active: native
重启configserver
浏览器访问测试:http://localhost:3010/application-zuul-dev.yml