SpringCloud-unified configuration center (1)

5c6ca5fb0001032e19201080.jpg (1920×1080)

Why do we need a unified configuration center?

  1. Inconvenient to maintain: multiple people modify the same configuration at the same time, and conflicts continue
  2. Configuration content security and permissions: Online configuration is not disclosed to developers, because it is sensitive, it is only disclosed to operation and maintenance
  3. Key points (restart required to update configuration items): The unified configuration center can realize dynamic configuration without restarting the project (including online configuration updates)

5d3113410001bec719201080.jpg (1920×1080)

  • Initially place the configuration on the remote git. config-server pulls it down and puts it on the local git. If there is a problem with the remote git, pull the configuration from the local git. Then get the client's components from the server part of the other client.

 

Start

5e9147c0000148e119201080.jpg (1920×1080)

5e914b770001815019201080.jpg (1920×1080)

  1. Eureka's client-side dependencies, Config Server's dependencies
  2. @EnableDiscoveryClient、@EnableConfigServer
  3. Center configuration yml, remote warehouse configuration

5e914f540001577b19201080.jpg (1920×1080)

  • order.yml (regardless of which environment is selected, this will be loaded and accessed, so you can put the public configuration here, if you don't need it, comment it out) order-dev.yml order-test.yml, this rule is agreed internally . As shown in the figure: Although order-test.yml is accessed here, order.yml and it will be merged.

5c06778c00019aa219201080.jpg (1920×1080)

Configure naming conventions

  1. /order-dev.yml (/ file name-environment name. file suffix)
  2. /dev/order-dev.yml (/ branch name / file name-environment name. file suffix)
  • /{name}-{profiles}.yml
  • /{label}/{name}-{profiles}.yml
  • name service name
  • profile environment
  • label branch (the default is the "master" master branch)

 

Config Server

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.imooc</groupId>
	<artifactId>config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.BUILD-SNAPSHOT</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>
package com.imooc.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigApplication.class, args);
	}
}
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/SpringCloud_Sell/config-repo
          username: [email protected]
          password: T#27h*E$cg@%}j
          basedir: /Users/admin/code/myProjects/java/imooc/SpringCloud_Sell/config/basedir
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

5d0c6d5600013dfb19201080.jpg (1920×1080)

  • Ps: Then configure the central server can also be highly available. It's just that unlike eureka, you need to register with each other, as long as you change the port and start as much as you want, then the client access to the service of the configuration center is also load-balanced. As shown in the figure, this is the configuration and logs in the client. The CONFIG comes with high availability, which is the Config Server above. However, in the log, the cursor position starts a few times to find that the service address will be different (that is, load balancing).

 

Config Client(e.g. Order)

order server

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
# bootstrap.yml

spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: test
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

(1) Loading sequence

  1. bootstrap.yml loads first
  2. load after application.yml
  • When using spring cloud config, you need to get the configuration parameters from the cloud and load them into the ApplicationContext, otherwise the startup will report an error, so you need to configure spring cloud config in bootstrap.yml

(2) Use spring cloud config

  • First configure spring.application.name, spring.cloud.config.server.git.uri in bootstrap.yml (here is through config client to git.uri in config server)

5e1f23620001612019201080.jpg (1920×1080)

  • Q: Why is the highlighted part written here, shouldn't it be written in the git configuration?
  • Answer: Sequence: first register to the service center, discover config through the service center, and discover the configuration file. If there is no matching service center to find 8761 by default, if you can find it, you can find it. If not, you will get back the problem. If the service center is not 8761, it should be written in bootstrap.

5e914ec30001a61019201080.jpg (1920×1080)

package com.imooc.order.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/env")
public class EnvController {

    @Value("${env}")
    private String env;

    @GetMapping("/print")
    public String print() {
        return env;
    }
}
  • Here is used for testing, to see if it can be opened!

5e914f300001a7b219201080.jpg (1920×1080)

Published 952 original articles · praised 1820 · 890,000 views

Guess you like

Origin blog.csdn.net/Dream_Weave/article/details/105450418