springcloud之zuul和config-server

1.zuul相关介绍

zuul是springcloud的组件之一,用来作为请求的网关:

所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序。作为一个边界性质的应用程序,Zuul提供了动态路由、监控、弹性负载和安全功能。Zuul底层利用各种filter实现如下功能:

  • 认证和安全 识别每个需要认证的资源,拒绝不符合要求的请求。
  • 性能监测 在服务边界追踪并统计数据,提供精确的生产视图。
  • 动态路由 根据需要将请求动态路由到后端集群。
  • 压力测试 逐渐增加对集群的流量以了解其性能。
  • 负载卸载 预先为每种类型的请求分配容量,当请求超过容量时自动丢弃。
  • 静态资源处理 直接在边界返回某些响应。

zuul的底层过滤器有前置过滤器pre,后置过滤器post,错误过滤器 errorFilter,还可以之定义过滤器

1.实现zuul,搭建一个网关

1.导入zuul的依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 </dependency>

 2.在主配置类上面开启网关反向代理

@EnableZuulProxy

3.配置配置文件yml,需要将网关模块注册到注册中心eureka

eureka:
client:
serviceUrl:
defaultZone: http://localhost:1010/eureka/
instance:
prefer-ip-address: true #定义ip到注册中心注册
instance-id: zuul-client:1020 #使用id的方式注册
server:
port: 1020
spring:
application:
name: zuul-client #给网关客户端在erueka服务器取个名字
zuul:
ignored-services: "*"
routes:
systemmanage-server: "/system/**"

4.自定义网关,写一个自定义类,继承ZuulFilter,覆写四个方法,一个是顺序,一个是类型,一个是返回boolean结果,一个是根据boolean结果的值判断是否执行run()方法
false,不执行run方法,true执行run方法
@Component
public class LoginCheckFilter extends ZuulFilter {
@Override
public String filterType() {
return PRE_TYPE;
}

@Override
public int filterOrder() {
return 3;
}

@Override
public boolean shouldFilter() {
//获取上下文对象
RequestContext currentContext = RequestContext.getCurrentContext();
//获取请求对象
HttpServletRequest request = currentContext.getRequest();
//通过请求对象获取资源路径
String requestURI = request.getRequestURI();
//判断请求路径是否包含login
if(requestURI.toUpperCase().contains("LOGIN")){
return false;
}
return true;//返回true执行下面的run方法
}

@Override
public Object run() throws ZuulException {
//获取上下文对象
RequestContext currentContext = RequestContext.getCurrentContext();
//获取请求对象
HttpServletRequest request = currentContext.getRequest();
    //获取响应对象
HttpServletResponse response = currentContext.getResponse();
    //设置返回类型和编码
response.setContentType("application/json;charset = utf-8");
Map<String,Object>jsonMap = new HashMap<>();
jsonMap.put("success", false);
jsonMap.put("msg", "请先登录。。。。");
//获取请求头
String header = request.getHeader("x-token");
if(StringUtils.isBlank(header)){
//
try {
response.getWriter().print(JSON.toJSONString(jsonMap));//这里使用了阿里巴巴的fastjson包,帮我们处理返回json,将对象转为json字符串
} catch (IOException e) {
e.printStackTrace();
}
currentContext.setSendZuulResponse(false);//如果是false,直接返回响应结果,下面的代码再不执行
}
return null;
}
}

2.使用配置中心config-server

介绍:配置中心的作用是从远程仓库中拉取到指向配置中心的配置文件,而配置中心又是指向远程仓库的,所以微服务根据,在远程厂库的配置文件的名字,就能够通过配置中心这个桥梁拉取到自己的配置。

1.要使用远程仓库,需要使用第三方的仓库如github,或者码云,如何在码云上面创建仓库就不再叙述了

2.在项目里面新建一个配置中心模块 config-server

3.导入依赖

<dependencies>
<!-- Config-Server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

4.主配置类开启配置中心
@EnableConfigServer
5.配置yml文件,指向远程仓库
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: config-server #指定服务的id
server:
port: 5000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/huang__ming/springcloud-config.git #指向码云仓库,这个就是远程仓库的地址
username: [email protected]
password: asd19930256

2.搭建配置中心的客户端

1.导入依赖

<!-- Config-Client 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

2.修改application.yml文件文件名为bootstrap.yml

3.将客户端的配置,配置到码云的仓库中(在仓库中创建一个文件zuul-client-dev.yml)

eureka:
client:
serviceUrl:
defaultZone: http://localhost:1010/eureka/
instance:
prefer-ip-address: true #定义ip到注册中心注册
instance-id: zuul-client:1020
server:
port: 1020
spring:
application:
name: zuul-client
zuul:
ignored-services: "*"
routes:
systemmanage-server: "/system/**"

4.配置修改文件名后的yml文件,路径指向配置文件的地址

spring:
cloud:
config:
uri: http://localhost:5000
name: zuul-client
profile: dev



5.测试,启动注册中心服务,启动配置中心微服务,启动网关微服务,浏览器输入localhost:5000/zuul-client.dev.yml,如果能够读取到远程仓库的
文件内容说明配置中心集成成功



猜你喜欢

转载自www.cnblogs.com/19930909a/p/12121373.html