微服务架构 基础(五)

微服务架构 基础(五)

持续更新…


继续前面的基础四工程进行扩展

分布式配置中心

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以需要一套集中式、动态的配置管理设施是必不可少的。
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

配置中心具有什么作用?

  • 集中管理配置文件
  • 不同环境不同配置,动态化配置更新,分环境部署比如dev/test/prod/bete/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一获取配置自己的信息
  • 但配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以Rest接口的形式暴露

大致流程:

在这里插入图片描述

服务端搭建

新建配置中心服务端子模块(config-center-10002):

在这里插入图片描述

添加主要依赖:

<!-- eureka客户端依赖 -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
 <!-- 配置中心服务端依赖 -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-server</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

添加application.yml文件:

server:
  port: 10002
spring:
  application:
    name: config-center-10002
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/yiyexingchen/spring-cloud-config.git
          default-label: master
          search-paths:
            - SpringCloud[这里是代码仓库目录]
          username: ***[这里是gitee账号]
          password: ***[这里是gitee密码]
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka
    register-with-eureka: true # 注册到Eureka中心
    fetch-registry: true # 可以获取注册中心列表信息

主启动类:

package cn.wu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterApplication10002 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication10002.class,args);
    }
}

首先启动Eureka服务器(端口分别为7001和7002)测试结果如下(此时可以直接访问到相应仓库下的特定文件,这里git基础不再过多赘述):

在这里插入图片描述

通过以上的搭建过程,可以渐渐理解随时通过git管理yml文件达到所谓的集中式、动态化的配置管理…

配置读取匹配规则

application:服务名,label:分支名,profile:环境名

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

客户端搭建

新建配置中心客户端子模块(config-client-11001):

在这里插入图片描述

添加核心依赖:

<!-- eureka客户端依赖 -->
<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-config</artifactId>
</dependency>

新建bootstrap.yml(application.yml是用户级的资源配配置项,而bootstrap.yml是系统级资源配置项,优先等级更高):

server:
  port: 11001
spring:
  application:
    name: config-client-11001
  cloud:
    config:
      label: master # 分支名称 会自动拼接
      name: config # 配置文件名称 会自动拼接
      profile: dev # 开发环境的后缀 会自动拼接
      uri: http://localhost:10002 # 配置中心地址

eureka:
  client:
    register-with-eureka: true # 注册
    fetch-registry: true # 获取注册列表信息
    service-url:
      defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka

主启动类:

package cn.wu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication11001 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication11001.class,args);
    }
}

控制层:

package cn.wu.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}

首先动两个Eureka服务注册中心(端口分别为7001/7002),然后启动服务配置中心服务端(端口为10002),最后启动配置中心客户端,结果测试:

在这里插入图片描述
成功实现了客户端11001访问配置中心的服务端10002通过Gitee获取配置信息…

但是,简单分析一下,可以发现客户端11001的配置信息是运行一次只加载一次的,所以再次修改Gitee上仓库中的文件信息,是不会动态刷新的…

客户端动态刷新

添加监控依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

bootstrap.yml文件添加:

# 暴露检控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

修改主启动类:

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication11001 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication11001.class,args);
    }
}

控制层添加重要注解@RefreshScope

@RefreshScope // 添加热加载注解
@RestController
public class ConfigController {

    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}

修改Gitee中的yml文件,再次访问相应URL…

在这里插入图片描述

测试结果为(要记得在这这之前应该利用POST请求激活一下该端口… ):

在这里插入图片描述

在这里插入图片描述

从以上还是能发现一些问题,最后都是需要POST激活才行,虽然有多个微服务需要刷新时通过脚本也是能实现,但是还是不够完善(没有做到真正意义上的动态刷新… ),因此,还需要进一步优化改善,这个问题也就是下一篇文章需要介绍的:Spring Cloud Bus 消息总线实现…


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>