微服务迁移记(三):配置中心SpringCloud Config搭建

springboot推荐使用注解方式,减少了大量的xml配置。系统的基本配置文件我选择用yml格式,相对于properties,代码更简洁(不用重复写属性),结构化更清晰一点,读取速度也应该能略快一点吧。配置文件名bootstrap.yml优先于application.yml。

分布式配置中心,主要是将配置信息保存在配置中心的本地文件或数据库或远程版本控制中心(svn、git)中。研究了一段时间阿波罗,不知道为啥虚拟机能telnet宿主mysql,但阿波罗始终提示数据库连接不上,遂放弃。进一步研究config,并将配置数据成功保存至mysql数据库中。

 

一、数据库创建

建立一张tb_config_server。为防止字段与关键字重复,前面加了字母a。

akey:配置名称

avalue:配置值

application:对应注册中心注册应用的服务别名

aprofile:dev 开发环境,prd:生产环境

label:标签

DROP TABLE IF EXISTS `tb_config_server`;
CREATE TABLE `tb_config_server` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`akey` varchar(255) DEFAULT NULL,
`avalue` varchar(255) DEFAULT NULL,
`application` varchar(255) DEFAULT NULL,
`aprofile` varchar(255) DEFAULT NULL,
`label` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

 

增加一条测试数据:

insert into tb_config_server(akey,avalue,application,aprofile,label) values('site_url','http://www.baidu.com','xxproject-api-service-sys','dev');

其中:xxproject-api-service-sys:代表是api项目中系统维护模块服务,这个名字是这个项目注册到注册中心的别名。

二、config配置中心搭建

1. 新建项目:xxproject-config-server

2. 导包,主要是config和数据库,同时需要将config注册到注册中心,所以consul的包也导入

复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version> 2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!-- 使用数据库配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version> 5.1.48</version>
        </dependency>
    </dependencies>
复制代码

 

3. 编写配置文件

spring.application.name:注册到注册中心的别名,客户端通过这个别名连接配置中心。

spring.cloud.config.server.jdbc:主要是一条SQL语句,提取配置列表。

复制代码
spring:
  application:
    name: config-server
  cloud:
    consul:
      host: 192.168.0.7
      port: 8500
      discovery:
        hostname: 192.168.0.6
    config:
      server:
        jdbc:
          sql: SELECT akey,avalue FROM tb_config_server WHERE application=? AND aprofile=? AND label=?
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/zyconfig??useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
  profiles:
    active: jdbc
server:
  port: 9000
复制代码

 

4. 编写启动入口

@EnableDiscoveryClient:注册中心
@EnableConfigServer:配置中心
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class AppConfigServer {
    public  static  void main(String[] args){
        SpringApplication.run(AppConfigServer.class,args);
    }
}

 

启动程序后,就可以在注册中心看到配置中心的服务,配置中心搭建完毕

 

三、客户端读取测试

1. config包导入

<!-- config client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version> 2.1.4.RELEASE</version>
        </dependency>

2. 配置文件(使用bootstrap.yml)

复制代码
spring:
  application:
    ## 系统管理服务,注册中心别名
    name: xxproject-api-service-sys
  cloud:
    config:
      discovery:
        ## 配置中心在注册中心的别名
        service-id: config-server
        enabled: true
      profile: dev
      label: dev
    consul:
      host: 192.168.0.7
      discovery:
        hostname: 192.168.0.6
      port: 8500
server:
  port: 9001
复制代码

3. 启动入口

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

4. 编写简单的测试控制器

复制代码
@RestController
public class testController{
    @Value("{site_url}")
    private String site_url;

    @RequestMapping("/index")
    public String index(){
        return site_rul;
    }
}
复制代码

访问:http://localhost:9001/index

可以成功打印出:http://www.baidu.com

posted on   zhouyu  阅读(385)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示