Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)

 

目录

服务网关Gateway实现用户鉴权_网关全局过滤器加入JWT 鉴权

分布式配置中心_Spring Cloud Config 

分布式配置中心_Config配置总控中心搭建

分布式配置中心_Config配置读取规则


 

服务网关Gateway实现用户鉴权_网关全局过滤器加入JWT 鉴权

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第1张图片

配置跳过验证路由 

org:
 my:
   jwt:
      #跳过认证的路由
     skipAuthUrls:
         - /user/login

创建LoginGlobalFilter全局过滤器

@Data
@Slf4j
@Component
@ConfigurationProperties("org.my.jwt")
public class LoginGlobalFilter  implements
GlobalFilter, Ordered {
    // 跳过路由数组
    private String[] skipAuthUrls;
    @Override
    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求url地址
        String url = exchange.getRequest().getURI().getPath();
        //跳过不需要验证的路径
        if (null != skipAuthUrls && isSkipUrl(url)) {
            return chain.filter(exchange);
       }
        //从请求头中取得token
        String token = exchange.getRequest().getHeaders().getFirst("Authorization");
        if (StringUtils.isEmpty(token)) {
            return createResponseObj(exchange,500,"token参数缺失");
       }
        //请求中的token是否有效
        boolean verifyResult = JWTUtil.verify(token);
        if (!verifyResult) {
            return createResponseObj(exchange,500,"token 失效");
       }
        //如果各种判断都通过,执行chain上的其他业务逻辑
        return chain.filter(exchange);
   }
    @Override
    public int getOrder() {
        return 0;
   }
    /**
     * 判断当前访问的url是否开头URI是在配置的忽略url列表中
     *
     * @param url
     * @return
     */
    public boolean isSkipUrl(String url) {
        for (String skipAuthUrl : skipAuthUrls) {
            if (url.startsWith(skipAuthUrl))
            {
                return true;
           }
       }
        return false;
   }
    // 组装返回数据
    private Mono createResponseObj(ServerWebExchange exchange,Integer code,String message){
        ServerHttpResponse response = exchange.getResponse();
        // 设置响应状态码200
        response.setStatusCode(HttpStatus.OK);
        // 设置响应头
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        // 创建响应对象
        Response res = new Response(code, message);
        // 把对象转成字符串
        byte[] responseByte = JSONObject.toJSONString(res).toString().getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = response.bufferFactory().wrap(responseByte);
        return response.writeWith(Flux.just(buffer));
   }
}

测试

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第2张图片

分布式配置中心_Spring Cloud Config 

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第3张图片

分布式系统面临问题 

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一 管理,实时更新,所以需要分布式配置中心组件。

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第4张图片

什么是Spring Cloud Config 

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第5张图片

 整个结构包括三个部分,客户端(各个微服务应用),服务端 (中介者),配置仓库(可以是本地文件系统或者远端仓库, 包括git,svn等)。

1、配置仓库中放置各个配置文件(.yml 或者.properties)

2、服务端指定配置文件存放的位置

3、客户端指定配置文件的名称

Config能干什么

1、提供服务端和客户端支持

2、集中管理各环境的配置文件

3、配置文件修改之后,可以快速的生效

4、可以进行版本管理

5、支持大的并发查询

6、支持各种语言 

对比主流配置中心 

开源的配置中心有很多,比如,360的QConf、淘宝的 nacos、携程的Apollo等。在Spring Cloud中,有分布式配置中心组件spring cloud config,它功能全面、强大,可以无缝地和Spring体系相结合,使用方便简单。

实时效果反馈

1.Spring Cloud Config项目是一个解决分布式系统的__问题。

A 服务注册发现

B 负载均衡

C 服务熔断

D 配置管理

2.Spring Cloud Config项目包含了____和____两个部分。

A client,service

B client,server

C master,slave

D 以上都错误 

分布式配置中心_Config配置总控中心搭建

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第6张图片

服务端开发 

服务端开发最主要的任务是配置从哪里读取对应的配置文件,我们将配置从Git仓库读取配置文件。

在码云新建一个名为springcloud-config的新的仓库

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第7张图片 

项目开源 

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第8张图片

新建模块cloud-config-server3344 

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第9张图片

仓库中新建3个文件 

config-dev.yml

config:
 info: "master branch,config-dev.yml
version=1"

config-test.yml

config:
 info: "master branch,config-test.yml
version=1"

config-prod.yml

config:
 info: "master branch,config-prod.yml
version=1"

POM文件引入依赖

   
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
                  
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            1.18.22
        
    

编写YML文件

新增application.yml

server:
 port: 3344
spring:
 application:
   name: cloud-config-center
 cloud:
   config:
     server:
       git:
         uri: https://gitee.com/WCCRegistered/cloud-config.git
         search-paths:
           - cloud-config
     label: master
eureka:
 client:
    # 表示是否将自己注册到Eureka Server
   register-with-eureka: true
    # 示是否从Eureka Server获取注册的服务信息
   fetch-registry: true
    # Eureka Server地址
   service-url:
     defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
 instance:
   instance-id: cloud-config-center
   prefer-ip-address: true

编写主启动类

package com.tong;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* 主启动类
*/
@Slf4j
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
      SpringApplication.run(ConfigCenterMain3344.class,args);
        log.info("*********** 配置中心服务启动成功 *************");
   }
}

测试通过config微服务是否可以从码云上获取配置

http://locahost:3344/master/config-dev.yml

实时效果反馈

1.Spring Cloud Config项目配置具体存放在____。

A redis

B mysql

C git

D svn 

分布式配置中心_Config配置读取规则

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第10张图片

Config支持的请求的参数规则

1、/{application}/{profile}[/{label}]

2、/{application}-{profile}.yml

3、/{label}/{application}-{profile}.yml

4、/{application}-{profile}.properties

5、/{label}/{application}-{profile}.properties 

Spring Cloud【分布式配置中心(Spring Cloud Config 、Config配置总控中心搭建、Config配置读取规则)】(九)_第11张图片 

注意:

1、{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面 创建的配置文件。

2、{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、 application-test.yml、application-prod.yml。

3、{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可 以通过不同的 label 来控制访问不同的配置文件了。 

最推荐使用方式

/ { 分支名 } / { 应用名 } - { 环境名 }.yml 

实时效果反馈

1.Spring Cloud Config客户端在指定配置文件时profile表示__含 义。

A 应用名称

B 配置文件的版本

C git 分支

D 以上都是错误

 

你可能感兴趣的:(Spring全家桶,服务器,spring,cloud,Config,后端,java)