栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微服务架构 基础(五)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微服务架构 基础(五)

微服务架构 基础(五) 持续更新…

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

分布式配置中心

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

配置中心具有什么作用?

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

大致流程:

服务端搭建

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

添加主要依赖:

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

添加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):

添加核心依赖:


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client



    org.springframework.cloud
    spring-cloud-starter-config

新建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上仓库中的文件信息,是不会动态刷新的…

客户端动态刷新

添加监控依赖:


   org.springframework.boot
   spring-boot-starter-actuator

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 消息总线实现…



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/458431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号