Article Directory
SpringCloud Config Introduction
Spring Cloud Config is a new project Spring Cloud team created for distributed systems infrastructure and application services to provide centralized micro external configuration support, which is divided into server and client in two parts. Wherein the service center side is also referred to a distributed configuration, it is an independent micro-service applications, warehouse and configured to connect the client to provide access to configuration information, the encryption / decryption information access interface and the like; and the client is a micro-service architecture the individual micro-service applications or infrastructure, they manage application resources and business-related content through the configuration specified configuration center, and obtaining and loading configuration information from a configuration center at boot time. Spring Cloud Config achieve the abstract mapping of server and client environment variables and configuration properties, so it is applicable not only to build Spring applications outside, also can be used in any application to run in other languages. Since the Spring Cloud Config achieve configuration center default using Git to store configuration information, use the configuration server Spring Cloud Config constructed natural to support version management for micro-service application configuration information, and can be easily by Git client tools of management configuration and access content. Of course, it also provides support for other storage methods, such as: GIT repository, SVN repository, localization file system.
Config Server-side primary and Git / SVN server
Popular point, is unified management, including facilitating handover environment configuration, and modify the configuration without moving the code, worry and effort;
If you spend SpringCloud Bus, can be achieved without having to reboot, auto-sensing configuration changes and apply the new configuration;
Config Server using basic
According to the previous SpringCloud Chart, The first step, to put forward a configServer to link remote GIT repository to read remote configuration;
GIT warehouse here, we generally use GitHub https://github.com/, or code cloud https://gitee.com/
We demonstrate here with GitHub
Build a warehouse microservice-config and then download Git locally;
To upload a configuration file git repository, application.yml remember to utf-8 encoding, or garbled, resolve problems;
for I am here to facilitate the use of the configuration file is later to be used:
---
spring:
profiles:
active: dev
---
spring:
profiles: dev
port: 111
---
spring:
profiles: test
port: 222
New module: microservice-config-server-
4001 import dependent pom:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Complete pom-dependent:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.swx</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-server-4001</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Add comment @EnableConfigServer startup class ConfigServerApplication_4001
package com.swx.microserviceconfigserver4001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class MicroserviceConfigServer4001Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConfigServer4001Application.class, args);
}
}
Our warehouse git configuration request to yml address configuration file:
server:
port: 4001
spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: https://github.com/Lentter/microservice-config.git
Local Hosts add a local domain name mapping:
127.0.0.1 configserver.swx.com
Start at 4001:
http://configserver.swx.com:4001/application-xxx.yml
access address:
return the contents of the configuration file that we upload;
as for the request path, the provisions of matching rules:
The HTTP service has resources in the form:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
Config Client Basic use
According to the previous config diagram, we need to build server-side Client-side calls, and ultimately the client side gets the remote git configuration information;
To facilitate the presentation later, we submitted three configuration files to a remote git repository;
crm-dev.yml
port:
777
CRM-test.yml
port:
888
application.yml:
---
spring:
profiles:
active: dev
---
spring:
profiles: dev
port: 111
---
spring:
profiles: test
port: 222
Then we create a new module microservice-config-client-5001
plus dependent:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我们项目启动的时候,就要调用server config端,获取配置信息,所以这里要bootstrap.yml配置文件,优先级最高:
spring:
application:
name: application-dev
cloud:
config:
name: crm
uri: http://configserver.swx.com:4001
profile: test
label: master
fail-fast: true
application.yml:
server:
port: 5001
context-path: /
然后再配置一个我们测试用的controller测试显示端口
ConfigClientController
package com.swx.microserviceconfigclient5001.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 ConfigClientController {
@Value("${port}")
private String port;
@GetMapping("/getPort")
public String getPort() {
return "测试你访问的yml文件的端口是:【"+port+"】";
}
}
最后 本地hosts我们加给配置:
127.0.0.1 client-config.swx.com
启动项目,访问测试地址
http://client-config.swx.com:5001/getPort
可以看到,我们访问的888端口,这是因为在bootstrap.yml中配置的是test,所以读取出来的是888,改成dev就读取出777了
我们还可以访问以下几个地址:
http://configserver.swx.com:4001/application-dev.yml
http://configserver.swx.com:4001/application-test.yml
Config整合Eureka
eureka整合config以及服务器提供者整合config
我们先上传配置文件到Github
eureka_config.yml
spring:
profiles:
active:
- dev
---
server:
port: 2004
context-path: /
spring:
profiles: dev
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
port: 2005
context-path: /
spring:
profiles: test
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
新建项目:microservice-eureka-server-config
导入pom依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
配置两个配置文件
application.yml
spring:
application:
name: microservice-eureka-server-config
bootstrap.yml
spring:
application:
name: microservice-eureka-server-config
cloud:
config:
name: eureka_config
uri: http://http://configserver.swx.com:4001 # 配置configserver地址
profile: dev # 级别
label: master # 分支 git中 默认master
在启动类加上Eureka的注解:@EnableEurekaServer
package com.swx.microserviceeurekaserverconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerConfigApplication.class, args);
}
}
测试一下:
先启动4001 然后再启动新建的eureka
访问:
http://localhost:2004/
ok
然后再搭建一个服务者注册进这个注册中心:microservice-book-provider-config
一样的首先上传一个配置文件
provider_config.yml
spring:
profiles:
active: dev
---
server:
port: 1007
context-path: /
# 数据源配置
spring:
profiles: dev
application:
name: microservice-book
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost
appname: microservice-book
instance-id: microservice-book:1007
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka
info:
groupId: com.swx.springcloud
artifactId: microservice-book-provider-config-1007
version: 1.0-SNAPSHOT
userName: http://swx.com
phone: 130**********
---
server:
port: 1008
context-path: /
# 数据源配置
spring:
profiles: test
application:
name: microservice-book
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost
appname: microservice-book
instance-id: microservice-book:1008
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka
info:
groupId: com.swx.springcloud
artifactId: microservice-book-provider-config-1008
version: 1.0-SNAPSHOT
userName: http://swx.com
phone: 130**********
新建microservice-book-provider-config
pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.swx</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-book-provider-config</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.swx</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加注册中心Eureka相关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- actuator监控引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Hystrix相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.yml:
spring:
application:
name: microservice-student-provider-config
cloud:
config:
name: provider_config
uri: http://configserver.swx.com:4001 # 配置configserver地址
profile: dev # 级别
label: master # 分支 git中 默认master
application.yml
spring:
application:
name: microservice-book-provider-config
启动类:
package com.swx.microservicebookproviderconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EntityScan("com.swx.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceBookProviderConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceBookProviderConfigApplication.class, args);
}
}
其他类文件从 原先的服务提供者里直接复制一份即可,这里不贴了;
启动下这个项目;
说明成功注册到服务注册中心了;
Config配置搜索路径
前面我们所有的GIT远程端配置文件都是跟目录的,所有请求默认都是根目录,但是有时候,项目很多,配置文件需要根据子目录来划分,这时候,就需要来配置搜索路径了;比如aaa项目的配置文件放aaa目录下,bbb项目的配置文件放bbb目录下,不配置的话 是找不到的那些配置文件的,我们需要配置search-paths属性实现;
microservice-config-server-4001 configserver端 加个配置
server:
port: 4001
spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: https://github.com/Lentter/microservice-config.git
search-paths: a,c
然后准备3个文件夹a、b、c
分别对应nns.yml
spring:
profiles:
active: dev
---
spring:
profiles: dev
name: aaadev
---
spring:
profiles: test
name: aaatest
nns2.yml
spring:
profiles:
active: dev
---
spring:
profiles: dev
name: bbbdev
---
spring:
profiles: test
name: bbbtest
nns3.yml
spring:
profiles:
active: dev
---
spring:
profiles: dev
name: cccdev
---
spring:
profiles: test
name: ccctest
After all we have to submit to Github test the
we configured the a and c can access normal, only we did not configure b only have access to the default, which is not the name
if we do not access the configuration file, you I will not name:
more ......