博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud 2.x学习笔记:5、Config(Greenwich版本)
阅读量:2388 次
发布时间:2019-05-10

本文共 6454 字,大约阅读时间需要 21 分钟。

说明:参考

1、Config介绍

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

在spring cloud config 组件中,分两个角色,一是config server,二是config client。

Spring Cloud Config Server最常见是将配置文件存放方式有3种:

(1)本地
(2)远程Git仓库
(3)局域网数据库

本文介绍局域网数据库方式存储配置文件

2、样例

2.1 数据库

(1) 创建数据库实例

MariaDB [(none)]> create database config;Query OK, 1 row affected (0.03 sec)MariaDB [(none)]> use config;Database changedMariaDB [config]>

(2) 创建数据表

MariaDB [config]> CREATE TABLE `config_properties` (    ->   `id` bigint(20) NOT NULL AUTO_INCREMENT,    ->   `key1` varchar(50) COLLATE utf8_bin NOT NULL,    ->   `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,    ->   `application` varchar(50) COLLATE utf8_bin NOT NULL,    ->   `profile` varchar(50) COLLATE utf8_bin NOT NULL,    ->   `label` varchar(50) COLLATE utf8_bin DEFAULT NULL,    ->   PRIMARY KEY (`id`)    -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;Query OK, 0 rows affected (0.00 sec)MariaDB [config]>

(3) 插入数据

MariaDB [config]> insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','server.port','8083','config-client','dev','master');'2','foo','bar-jdbc','config-client','dev','master');Query OK, 1 row affected (0.00 sec)MariaDB [config]> insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('2','foo','bar-jdbc','config-client','dev','master');Query OK, 1 row affected (0.00 sec)MariaDB [config]>

2.2 父级项目

在这里插入图片描述

4.0.0
com.cntaiping.tpa
config
1.0-SNAPSHOT
pom
config
Demo project for Spring Boot
UTF-8
UTF-8
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
config-server
config-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

2.3 Config Server模块

(1)pom文件

4.0.0
com.cntaiping.tpa
config-server
0.0.1-SNAPSHOT
jar
config-server
Demo project for Spring Boot
com.cntaiping.tpa
config
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-config-server
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc

(2) application.yml

spring:  profiles:    active: jdbc  application:    name: config-server  datasource:    url: jdbc:mysql://10.17.12.160:3306/config?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver  cloud:    config:      label: master      server:        jdbc: trueserver:  port: 8300spring.cloud.config.server.jdbc.sql: SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?

(3) Application类

添加@EnableConfigServer

package com.cntaiping.tpa.configserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigServerApplication.class, args);    }}

2.4 Config Client模块

(1) pom.xml

4.0.0
com.cntaiping.tpa
config-client
0.0.1-SNAPSHOT
jar
config-client
Demo project for Spring Boot
com.cntaiping.tpa
config
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-config

(2) bootstrap.properties

新建文件bootstrap.properties

在这里插入图片描述

spring.application.name=config-clientspring.cloud.config.uri= http://localhost:8300/spring.cloud.config.fail-fast=truespring.profiles.active=dev

默认端口号8083

(3) Application类

package com.cntaiping.tpa.configclient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigClientApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigClientApplication.class, args);    }}

(4)控制器

package com.cntaiping.tpa.configclient.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConfigController {    @Value("${server.port}")    String port;    @Value("${foo}")    String foo;    @RequestMapping(value = "/get")    public String get(String key){        if("foo".equals(key)){            return foo;        }        if("server.port".equals(key)){            return port;        }        return "";    }}

2.5 运行效果

运行server

在这里插入图片描述
运行client
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

转载地址:http://uwtab.baihongyu.com/

你可能感兴趣的文章
Flash Builder 4字体设置
查看>>
Actionscript 3.0 笔记一
查看>>
图像处理库OpenCV参考网址
查看>>
dllimport与dllexport作用与区别
查看>>
OpenGL坐标系
查看>>
C++用new和不用new创建类对象区别
查看>>
C++ C# JAVA 创建对象
查看>>
齐次坐标的理解
查看>>
QT配置文件
查看>>
QT .pro配置文件2
查看>>
Qt 模态与非模态对话框
查看>>
Qt C++中的关键字explicit .
查看>>
qtcreator中常用快捷键
查看>>
PowerDesigner 简介
查看>>
VS2008快捷键大全
查看>>
Access 操作或事件已被禁用模式阻止
查看>>
C# 控件置于最顶层、最底层
查看>>
几个常见的压缩算法
查看>>
浮点数的存储
查看>>
点到线段的距离
查看>>