Together to learn Spring Cloud | Chapter 7: Distributed Configuration Center (Spring Cloud Config)

The previous section, we explain the service gateways zuul, in this section we explain distributed configuration center springcloud of -Spring Cloud Config mode from local git and two storage configuration information.

A, Spring Cloud Config Description:

Spring Cloud Config project is a configuration management solution to address the distributed system. It contains two parts Client and Server, server to provide storage profile, in the form of an interface configuration file to provide the contents out, client data acquired through the interface, and based on this data to initialize their own applications.

Second, the new module springcloud-config-server:

1. Reference: together to learn Spring Cloud | Chapter One: How to build a multi-module springcloud project to create a basic modular structure

2. Modify the pom.xml introduced 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.haly</groupId>
		<artifactId>springcloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<groupId>com.haly</groupId>
	<artifactId>springcloud-config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Server-config-springcloud </ name> 
	<Dependencies>
	<Description> config server to create a new project </ Description>

		<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-web</artifactId>
        </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>

3. Start the new entry based SpringcloudConfigServerApplication

@EnableConfigServer, indicate on Config Server

package com.haly;

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;

@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudConfigServerApplication {
	  public static void main(String[] args) {
			SpringApplication.run(SpringcloudConfigServerApplication.class, args);
	    }

}

4. Modify application.properties file (Git Method storage configuration)

On Github create a project and add a profile above config-client.properties , the configuration file to add a property config=NewConfig !.

In application.propertiesthe Provisioning Services Information and git information

spring.application.name=springcloud-config-server
server.port=7001
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

spring.cloud.config.server.git.uri=https://github.com/FunriLy/springcloud-study/
spring.cloud.config.server.git.searchPaths=config-repo
spring.cloud.config.server.git.username=Username
spring.cloud.config.server.git.password=Password

Start engineering Config Server, enter the browser: http: // localhost: 7001 / config-client / default / master, to obtain the following results

{
    "name": "config-client",
    "profiles": [
        "default"
    ],
    "label": null,
    "version": "52b88000fc46a8b1d72a2979f4721d45a3d1f429",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/FunriLy/springcloud-study//config-repo/config-client.properties",
            "source": {
                "configword": "NewConfig !"
            }
        }
    ]
}

Third, the new springcloud-config module (but do not):

In fact, I like to work to create a springcloud-config module, no business code, only pom.xml file and directory resources, resources put about public profile, you can refer to other modules

For example configuration:

spring-data-mysql.xml file

spring-data-redis-single.xml file

spring-jdbc-mysql.xml file

bootstrap.yml profile

1. Add pom.xml file

<?xml version="1.0"?>
<project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sinaif</groupId>
        <artifactId>sinaif-weibo-opt</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.sinaif</groupId>
    <artifactId>sinaif-config</artifactId>
    <name>${project.artifactId}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

2. New bootstrap.yml file (can be unified configuration config, eureka, hystrix wait)

注意这里是bootstrap.properties而不是appliction.properties因为bootstrap.properties会在应用启动之前读取,而spring.cloud.config.uri会影响应用启动

spring:
    cloud:
        config:
            name: config-client
            profile: default
            label: master
            uri: http://localhost:7001/
            
eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/
    

The above rule configuration config property, rule 7001 config-client / default / master access configuration of /: and in front of us directly with http: // localhost

URL mapping between the profile of

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

Client application is-config -dev.properties文件中的config-client值, profile is a profile of the active, generally used to indicate the environment information, label is an optional tag, typically used to represent a directory name.
For example, I file names on Github is config-client.properties, on Github are default environment, the default is the master branch. So it is/config-client/default/master

Fourth, the new springcloud-config-server module of the client:

I've written before with springcloud-feign-client module, increase in FeignController class a / testconfig method acts as the Client-side, springcloud-feign-client module reference content: together to learn Spring Cloud | Chapter 4: Consumer Services (Feign )

1. In pom.xml file, add a new module dependencies above, so that we can use springcloud-config module config configuration and eureka

 <dependency>
            <groupId>com.haly</groupId>
            <artifactId>springcloud-config</artifactId>
            <version>0.0.1-SNAPSHOT</version>
  </dependency>

2. Add a test method in the class FeignController

@Value("${configword}")
String configword;

@GetMapping(value = "/testconfig") public String testconfig(@RequestParam String name) {
return name +",git配置值:" + configword ;

}

3. Start services, testing

Sequentially starting springcloud-eureka-server module starts springcloud-config-server module starts springcloud-feign-client module

Visit: http: // localhost: 9600 / testconfig name = young farm yard,? Returns the result:

young yards agriculture, git configuration values: NewConfig!

Fifth, use local configuration to obtain configuration items:

1. Modify application.properties springcloud-config-server configuration module follows 

= springcloud-config-spring.application.name Server 
the server.port = 7001 
eureka.client.serviceUrl.defaultZone = HTTP: // localhost: 8761 / Eureka / 

# config configuration using the local 
spring.profiles.active = Native 
# represents the local reads the configuration file directory location 
spring.cloud.config.server.native.searchLocations: classpath: / config /

2. Create at springcloud-config-server module config file directory resources, the new profile at two config files, configuration items as follows:

configs-dev.properties:configword: dev-configword

configs-test.properties:configword: test-configword

3. Modify the config configuration springcloud-config module configuration file bootstrap.yml

ps: we have set for the current profile dev, it reads data from configs-dev.properties profile

spring:
    cloud:
        config:
            name: configs
            profile: dev
            label: config
            uri: http://localhost:7001/
            
eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/

4. Start services, operating results

Sequentially starting springcloud-eureka-server module starts springcloud-config-server module starts springcloud-feign-client module

First visit config-server, browser, enter: http: // localhost: 7001 / configs / dev / config

{
    "name": "configs",
    "profiles": [
        "dev"
    ],
    "label": "config",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "classpath:/config/configs-dev.properties",
            "source": {
                "configword": "dev-configword"
            }
        }
    ]
}

Access config-client, browser, enter: http: // localhost: 9600 / testconfig name = young farm yard, returned the following results?:

Agricultural young code, git configuration value: dev-configword

5. Modify profile properties springcloud-config module bootstrap.yml profile:

ps: we have set for the current profile dev, it reads data from configs-test.properties profile

spring:
    cloud:
        config:
            name: configs
            profile: test
            label: config
            uri: http://localhost:7001/        

6. start the service again, the results

Sequentially starting springcloud-eureka-server module starts springcloud-config-server module starts springcloud-feign-client module

First visit config-server, browser, enter: http: // localhost: 7001 / configs / dev / config

{
    "name": "configs",
    "profiles": [
        "test"
    ],
    "label": "config",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "classpath:/config/configs-test.properties",
            "source": {
                "configword": "test-configword"
            }
        }
    ]
}

Access config-client, browser, enter: http: // localhost: 9600 / testconfig name = young farm yard, returned the following results?:

Agricultural young code, git configuration values: test-configword

 

Guess you like

Origin www.cnblogs.com/haly/p/10945138.html