1-- [SpringCloud] --13 distributed configuration center SpringCloud Config

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 configyou 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

Here Insert Picture Description

Why should design one ConfigServer?

ConfigServerObtaining gitenvironmental profile information.

The purpose is to cache gitthe above profile information.

3 companies distinguish environmental project

Build gitenvironment Objective: persistent store profile information (code using cloud).

git Environment on a folder to distinguish between what?

gitEnvironment on the folder to 项目differentiate.

  • member_config: Member Profiles
  • order_config: Order service profile

Company project environment how to distinguish?

  • dev: Development Environment
  • sit:test environment
  • pre: Pre-release environment
  • prd: Quasi-production environment

Four yards to build a cloud environment

Use Code cloud environment to build gitserver-side:

Here Insert Picture Description

New testconfigFolder

Here Insert Picture Description

5 create server-side

Create a new project:

Here Insert Picture Description

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:
Here Insert Picture Description

Production Environment:

Here Insert Picture Description

Test the server configuration file can get

Start cloud-configServices:
browser access: http://127.0.0.1:8888/member-prd.properties

Here Insert Picture Description

Here Insert Picture Description

7 client reads the cloud-configconfiguration 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

Here Insert Picture Description

8 Dynamic refresh data

By default, the configuration file is not updated in real time information.

In SpringCloudthe manual refresh configuration files and real-time refresh configuration file in two ways.

  • Manually using actuatorendpoint refresh data
  • Refreshed in real time using SpringCloud Busa 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 actuatorendpoint 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 Beanthe Add @RefreshScopenotes

Here Insert Picture Description

When the configuration change, labeled with @RefreshScopethe Beanwill 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

Published 675 original articles · won praise 214 · Views 140,000 +

Guess you like

Origin blog.csdn.net/weixin_42112635/article/details/104702387