1 Why use a distributed configuration center?
When a system configuration file is changed, we need to restart the service in order to make the new configuration file to take effect, spring cloud config
you can achieve unified management profile micro-services in all systems, but can also be achieved when the profile occurred changes, the system will automatically update to acquire new configuration.
The difference between hot deployment?
Hot deployment environment for developers, because still restart the server.
Hot deployment is not suitable for the production environment.
2 distributed configuration center frame design principles
Why should design one
ConfigServer
?
ConfigServer
Obtaining git
environmental profile information.
The purpose is to cache git
the above profile information.
3 companies distinguish environmental project
Build
git
environment Objective: persistent store profile information (code using cloud).
git
Environment on a folder to distinguish between what?
git
Environment on the folder to 项目
differentiate.
member_config
: Member Profilesorder_config
: Order service profile
Company project environment how to distinguish?
dev
: Development Environmentsit
:test environmentpre
: Pre-release environmentprd
: Quasi-production environment
Four yards to build a cloud environment
Use Code cloud environment to build
git
server-side:
New
testconfig
Folder
5 create server-side
Create a new project:
The introduction of dependence:
<dependencies>
<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
application.yml
### 服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
spring:
application:
#### 注册中心应用名称
name: config-server
cloud:
config:
server:
git:
### git环境地址
uri: https://gitee.com/ylx20180828/config.git
#### 搜索目录
search-paths:
- testconfig
#### 读取分支
label: master
#### 端口号
server:
port: 8888
Startup Items
package com.snow.config;
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;
@EnableConfigServer // 开启configserver服务端
@EnableEurekaClient
@SpringBootApplication
public class ConfigApp {
public static void main(String[] args) {
SpringApplication.run(ConfigApp.class, args);
}
}
@EnableConfigServer
: Open distributed configuration center server
Create a profile on six yards Cloud
Create a profile in git above naming convention:
服务名称-环境.properties
For example: Member Service
member-dev.properties
Create a file
test environment:
Production Environment:
Test the server configuration file can get
Start cloud-config
Services:
browser access: http://127.0.0.1:8888/member-prd.properties
7 client reads the cloud-config
configuration file
cloud-member
Add dependent
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
Modify the configuration file:
bootstrap.yml
### 服务启动端口号
server:
port: 8000
### 服务名称(服务注册到eureka名称)
spring:
application:
### 服务名称需要与git创建的配置文件的名称前缀相同,member-sit.properties
name: member
cloud:
config:
#### 读取后缀
profile: sit
#### 读取config-server注册地址
discovery:
service-id: config-server
enabled: true
### 服务注册到eureka地址
eureka:
client:
service-url: # EurekaServer地址
defaultZone: http://127.0.0.1:8100/eureka
Create a test controller
package com.snow.member.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${myName}")
private String name;
@RequestMapping("/name")
private String name() {
return name;
}
}
Create a Startup Items
package com.snow.member;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MemberApp {
public static void main(String[] args) {
SpringApplication.run(MemberApp.class);
}
}
test
Start the service, browser access: http://127.0.0.1:8000/name
8 Dynamic refresh data
By default, the configuration file is not updated in real time information.
In
SpringCloud
the manual refresh configuration files and real-time refresh configuration file in two ways.
- Manually using
actuator
endpoint refresh data - Refreshed in real time using
SpringCloud Bus
a message bus
Manual and automatic refresh refresh do not need to restart the server. General recommends using a manual refresh, because the automatic refresh of poor performance.
8.1 actuator
endpoint refresh data
Add dependent
<!-- actuator监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml
New
### 开启监控端点
management:
endpoints:
web:
exposure:
include: "*"
The entry into force premise: the need to refresh
Bean
the Add@RefreshScope
notes
When the configuration change, labeled with @RefreshScope
the Bean
will to give special treatment to validate the configuration.
Manually refresh Interface
Post request a manual refresh
http://127.0.0.1:8000/actuator/refresh start the refresh read from cofnig server