SpringCloud config exemplary configuration git center (registered on Eureka)

One: Introduction

Introduction of two former clients are directly call server-side configuration center to obtain profile information. So that there is a problem of the coupling is too high, the client and server, if the server end to do a cluster, the client can only be routed through the original way, to change the IP address of the server end of time, the client also needs to modify the configuration, does not comply with the concept of springcloud service governance.

springcloud provides such a solution, we only need to register the server side as a service to the eureka, client end to eureka go get server-side configuration center services either.

Two .server end

2.1 Module to create a new project in the main Maven project, named config-serve-eureka. Create a way of using Spring Initializr way.
Here Insert Picture Description
2.2 config-serve-eureka pom.xml content is as follows:

<?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.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-serve-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-serve-eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </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>
    </dependencies>

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

</project>

Added dependence:

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

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

Main Module of pom.xml 2.3 plus:
Here Insert Picture Description
2.4 based on starting @EnableConfigServer annotation adding a functional configuration of the center server turn, coupled to the service registration @EnableDiscoveryClient comment open code is found as follows:

package com.example.configserveeureka;

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;

//开启服务注册于发现
@EnableDiscoveryClient
//注解开启配置中心服务器的功能
@EnableConfigServer
@SpringBootApplication
public class ConfigServeEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServeEurekaApplication.class, args);
    }

}

2.5 two new configuration file in the config-serve-eureka's resource folder application-dev1.yml and application-dev2.yml easy to do cluster. Application.yml profile and content, and application-dev1.yml application-dev2.yml follows:
application.yml:

spring:
  profiles:
    active: dev1  # dev1 和 dev2 都可以

application-dev1.yml:


server:
  port: 8772

spring:
  #配置程序名为config-serve-eureka
  application:
    name: config-serve-eureka
  cloud:
    config:
      label: master #配置仓库的分支
      server:
        git:
          uri: https://github.com/huangxuhenshuai/springcloud-config-demo     # 配置git仓库的地址
          search-paths: config-repo    # git仓库地址下的相对地址(放配置文件的文件夹),可以配置多个,用,分割。
          username: xxxxxxxxx   # git仓库的账号
          password:  xxxxxxxxx     # git仓库的密码


eureka:
  client:
    #服务注册地址
    serviceUrl:
      #注意: Eureka Server 的注册地址
      #将服务提供者注册到三个Eureka Server中去
      #defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
      #defaultZone: http://peer1:8001/eureka/
      defaultZone: http://localhost:8761/eureka/

application-dev2.yml:


server:
  port: 8773

spring:
  #配置程序名为config-serve-eureka
  application:
    name: config-serve-eureka
  cloud:
    config:
      label: master #配置仓库的分支
      server:
        git:
          uri: https://github.com/huangxuhenshuai/springcloud-config-demo     # 配置git仓库的地址
          search-paths: config-repo    # git仓库地址下的相对地址(放配置文件的文件夹),可以配置多个,用,分割。
          username: [email protected]   # git仓库的账号
          password: hx77082493hyx37     # git仓库的密码


eureka:
  client:
    #服务注册地址
    serviceUrl:
      #注意: Eureka Server 的注册地址
      #将服务提供者注册到三个Eureka Server中去
      #defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
      #defaultZone: http://peer1:8001/eureka/
      defaultZone: http://localhost:8761/eureka/

Compared to the configuration file config-serve content, much of the configuration information eureka

  1. uri: Configure address git repository
  2. search-paths: git relative address (release profile folders) under the address of the warehouse, a plurality may be configured with split.
  3. username: git repository account
  4. password: git repository password

2.6 Set Start two config-serve-eureka 8772,8773 two ports, as shown:
Here Insert Picture Description
Here Insert Picture Description

Three .client end

3.1 Module to create a new project in the main Maven project, named config-client-eureka. Create a way of using Spring Initializr way.
Here Insert Picture Description
3.2 config-client-eureka pom.xml content is as follows:

<?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.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-client-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client-eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

 <!--增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,
        可以监控程序在运行时状态,其中就包括/refresh的功能。-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>
    </dependencies>

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

</project>

Added dependence:

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

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

Main Module of pom.xml 3.3 plus:
Here Insert Picture Description
content of 3.4 bootstrap.yml profile config-client-eureka follows:


server:
  port: 8774


eureka:
  client:
    #服务注册地址
    serviceUrl:
      #注意: Eureka Server 的注册地址
      #将服务提供者注册到三个Eureka Server中去
      #defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
      #defaultZone: http://peer1:8001/eureka/
      defaultZone: http://localhost:8761/eureka/


spring:
  application:
    name: config-client-eureka
  cloud:
    config:
      name: config-eureka-client
      profile: dev
      label: master
      discovery:
        enabled: true #是否通过eureka发现config-serve-eureka
        service-id: config-serve-eureka #指定配置中心的service-id,便于扩展为高可用配置集群。(config-serve-euraka在eureka中的名称)



#spring.application.name:对应{application}部分
#spring.cloud.config.profile:对应{profile}部分
#spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
#spring.cloud.config.uri:配置中心的具体地址
#spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。(config-serve-euraka在eureka中的名称)
#spring.cloud.config.discovery.enabled: true   是否通过eureka发现config-service


#特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.yml,config部分内容才能被正确加载。
#因为config的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。

Special Note:
The above those associated with spring-cloud properties must be configured in bootstrap.yml, config part of the contents in order to be loaded correctly. Because of config configuration will precede application.yml, and bootstrap.yml is loaded prior to application.yml.

  1. spring.application.name: portion corresponding to {application}
  2. spring.cloud.config.profile: portion corresponding to {profile}
  3. spring.cloud.config.label: git of the corresponding branch. If the configuration of local storage centers, the parameter useless
  4. Specific address configuration center: spring.cloud.config.uri
  5. spring.cloud.config.discovery.service-id: Specifies the configuration of the central service-id, scalable cluster is highly available configuration. (Config-serve-eureka name of eureka)
  6. spring.cloud.config.discovery.enabled: true whether config-serve-eureka discovery by eureka

3.5 plus plus @EnableDiscoveryClient comment on startup class open to discovery service registration code is as follows:

package com.example.configclienteureka;

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

//开启服务注册于发现
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientEurekaApplication.class, args);
    }

}

IV. Test

A new class TestController config-client-eureka project, using @Value annotation server side to get the value of the parameters. code show as below:

package com.example.configclienteureka.web;

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


// 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。
@RefreshScope
@RestController
public class TestController {

    @Value("${neo.hello}")
    private String hello;

    @RequestMapping("/test")
    public String from() {
        return this.hello;
    }

}

Start eureka-serve, config-serve-eureka (8772,8773 two ports), config-client-eureka service.

Access in the browser: HTTP: // localhost: 8761 /
Here Insert Picture Description

Access on the browser are HTTP: // localhost: 8772 / config-Eureka-Client / dev , HTTP: // localhost: 8773 / config-Eureka-Client dev / information returned as follows:

{
    "name":"config-eureka-client",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"3ca3e72dd7763d0091624c7b93549c7d45d6c414",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/huangxuhenshuai/springcloud-config-demo/config-repo/config-eureka-client-dev.yml",
            "source":{
                "neo.hello":"config-eureka-client-dev"
            }
        }
    ]
}

DESCRIPTION two ends are properly read the server configuration information.

Another visit in the browser: HTTP: // localhost: 8774 / the Test , returned:

config-eureka-client-dev

Description Client has read the contents of the server side, we randomly stopped a server-side service, once again visit HTTP: // localhost: 8774 / the Test ,
returned:

config-eureka-client-dev

Description achieve the purpose of high availability.

Published 50 original articles · won praise 75 · views 8643

Guess you like

Origin blog.csdn.net/weixin_40991408/article/details/104193805