Eclipse runtime configuration SpringCloud (Hoxton + 2.2.4) Micro Services Framework + highly available distributed configuration center Spring Cloud Config

Brief introduction

Spring Cloud Config is used as a distributed system infrastructure services and micro applications centralized external configuration support, which is divided into server and client in two parts ConfigServer ConfigClient. Wherein the service center side is also referred to a distributed configuration, it is an independent micro-service applications, warehouse and configured to connect the client to provide access to configuration information, the encryption / decryption information access interface and the like; and the client is a micro-service architecture the individual micro-service applications or infrastructure, they manage application resources and business-related content through the configuration specified configuration center, and obtaining and loading configuration information from a configuration center at boot time. Spring Cloud Config achieve the abstract mapping of server and client environment variables and configuration properties, so it is applicable not only to build Spring applications outside, also can be used in any application to run in other languages. Since the Spring Cloud Config achieve configuration center default using Git to store configuration information, use the configuration server Spring Cloud Config constructed natural to support version management for micro-service application configuration information, and can be easily by Git client tools of management configuration and access content.
Here Insert Picture Description

Server configuration Config Server (HA)

Here Insert Picture Description

Add file dependencies POM.xml

<?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.springcloud</groupId>
    <artifactId>springcloud-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-config-server</artifactId>
  <name>springcloud-config-server</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <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.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>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Application.yml configuration file

Here Insert Picture Description

  • application.yml
spring:
  application:
    name: springcloud-config-server
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456
  cloud:
    config:
      # 配置仓库的分支
      label: master
      server:
        git:
          # 配置git仓库地址
          # uri: [email protected]/springcloud-config.git
          uri: file:E:/SpringBoot/SpringCloud/springcloud-root/springcloud-config
          # 配置仓库路径
          search-paths: config-file
          # 访问git仓库的用户名
          username: zhaojq
          # 访问git仓库的用户密码
          password: 123456

server:
  port: 8130

eureka:
  instance:
    hostname: eureka-config-server.com
    instance-id: eureka-config-server
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

Modify C: \ Windows \ System32 \ drivers \ etc \ hosts

127.0.0.1 eureka-config-server.com

Configure the local Git repository

Download and install git Service
https://git-scm.com/

git bash configuration:

git config --global user.name "zhaojq" 
git config --global user.email "[email protected]"
git config --global user.password "123456"

Create a local git repository
Here Insert Picture Description
Here Insert Picture Description

  • application-test.properties
    test environment configuration file
    Here Insert Picture Description

  • application-dev.properties
    development environment configuration file
    Here Insert Picture Description

  • application-pro.properties
    formal environment configuration file
    Here Insert Picture Description

Add Config Server startup class

Here Insert Picture Description

  • ConfigServerApplication.java

In the startup class plus @EnableConfigServer comment

package org.springcloud.config.server;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

Test Config Server

Start-config-Server project springcloud
Here Insert Picture Description
HTTP request and address resource file mapping is as follows:

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

Access address http://eureka-config-server.com:8130/application/test/master
Here Insert Picture Description
access address http://eureka-config-server.com:8130/application-test.yml
Here Insert Picture Description
access address http: // eureka- config-server.com:8130/master/application-test.yml
Here Insert Picture Description
access address http://eureka-config-server.com:8130/application-test.properties
Here Insert Picture Description
access address http://eureka-config-server.com: 8130 / master / application-test.properties
Here Insert Picture Description
access address http://eureka-config-server.com:8130/master/test
Here Insert Picture Description
prove to configure the service center can obtain configuration information.

Client Configuration Config Client

Here Insert Picture Description

Add file dependencies POM.xml

<?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.springcloud</groupId>
    <artifactId>springcloud-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-config-client</artifactId>
  <name>springcloud-config-client</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</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>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Bootstrap.yml configuration file

spring:
  application:
    name: springcloud-config-client
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456
  profiles:
    #读取测试环境配置文件
    #active: test
    #读取开发环境配置文件
    #active: dev
    #读取正式环境配置文件
    active: pro    
  cloud:
    config:
      label: master
      fail-fast: true
      #指明配置服务中心的网址
      uri: http://eureka-config-server.com:8130

server:
  port: 8131

eureka:
  instance:
    hostname: eureka-config-client.com
    instance-id: eureka-config-client
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

Test Config Client

Start-config-Client project springcloud
Here Insert Picture Description
Config Client successfully read the official environment configuration file to Config Server.

Client Configuration Config Client (HA)

Reference https://blog.csdn.net/miaodichiyou/article/details/104160284

Modify springcloud-eureka-provider program

Here Insert Picture Description

Dependencies file POM.xml

<?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.springcloud</groupId>
    <artifactId>springcloud-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-eureka-provider</artifactId>
  <name>springcloud-eureka-provider</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-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-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Increase bootstrap.yml file

spring:
  application:
    name: springcloud-eureka-provider
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456
  cloud:
    config:
      #profile: dev
      label: master
      fail-fast: true
      #指明配置服务中心的网址
      uri: http://eureka-config-server.com:8130
      #discovery:
        #service-id: springcloud-config-server
        #enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

Modify application-provider.yml

  • application-provider1.yml
spring:
  cloud:
    config:
      profile: dev

server:
  port: 8001
  
eureka:
  instance:
    hostname: eureka-provider1.com
    instance-id: eureka-provider1
  • application-provider2.yml
spring:
  cloud:
    config:
      profile: pro
    
server:
  port: 8002
  
eureka:
  instance:
    hostname: eureka-provider2.com
    instance-id: eureka-provider2
  • application-provider3.yml increase
spring:
  cloud:
    config:
      profile: test

server:
  port: 8003
  
eureka:
  instance:
    hostname: eureka-provider3.com
    instance-id: eureka-provider3

Add file

  • ConfigProviderApplication.java
package org.springcloud.eureka.provider;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ConfigProviderApplication {

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

    @Value("${server.port}")
    String port;

    @RequestMapping("/config")
    public String Home(@RequestParam String name) {
        return "Hello "+name+", This is from serverport:" + port+",content="+content;
    }

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

In order to start the project

springcloud-eureka-cluster-peer1
springcloud-eureka-cluster-peer2
springcloud-eureka-cluster-peer3
springcloud-config-server
springcloud-eureka-provider1
springcloud-eureka-provider2
springcloud-eureka-provider3

Here Insert Picture Description

Browser access http://eureka-provider1.com:8001/config?name=zhaojq
Here Insert Picture Description

Browser access http://eureka-provider2.com:8002/config?name=zhaojq
Here Insert Picture Description

Browser access http://eureka-provider3.com:8003/config?name=zhaojq
Here Insert Picture Description

Feign test

Reference https://blog.csdn.net/miaodichiyou/article/details/104304853

Modify the following files:

  • FeignConsumer.java
package org.springcloud.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Component
/*指定这个接口所要调用的提供者服务名称 */
@FeignClient(value = "springcloud-eureka-provider",configuration = FeignConfig.class,fallback = FeignHystrix.class)

public interface FeignConsumer {
    @GetMapping(value = "/hi")
    String sayHiFromEurekaProvider(@RequestParam(value = "name")String name);
    
    @GetMapping(value = "/config")
    String getConfigFromConfigServer(@RequestParam(value = "name")String name);
}
  • FeignController.java
package org.springcloud.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*调用提供者的home方法*/
@RestController
public class FeignController {
    @Autowired
    FeignService feignService;

    @GetMapping("/hi")
    public String hi(@RequestParam(defaultValue = "zhaojq",required = false)String name){
        return feignService.hi(name);
    }
    
    @GetMapping("/config")
    public String config(@RequestParam(defaultValue = "zhaojq",required = false)String name){
        return feignService.config(name);
    }
}
  • FeignService.java
package org.springcloud.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeignService {
    @Autowired
    FeignConsumer feignConsumer;
    public String hi(String name){
        return feignConsumer.sayHiFromEurekaProvider(name);
    }
    
    public String config(String name){
        return feignConsumer.getConfigFromConfigServer(name);
    }
}
  • FeignConsumer.java
package org.springcloud.feign;

import org.springframework.stereotype.Component;

@Component
public class FeignHystrix implements FeignConsumer {

    @Override
    public String sayHiFromEurekaProvider(String name) {
        return "hi,"+name+", use Feign + hystrix, eureka-provider is down!";
    }
    
    @Override
    public String getConfigFromConfigServer(String name) {
        return "hi,"+name+", use Feign + hystrix, eureka-provider is down!";
    }
}

Start springcloud-feign
in the command window curl http://eureka-feign.com:8101/config, Feign has been found to achieve load balancing
Here Insert Picture Description

Published 72 original articles · won praise 66 · Views 150,000 +

Guess you like

Origin blog.csdn.net/miaodichiyou/article/details/104420764