Spring Cloud Config 入门实例

Spring Cloud Config 入门实例

    • 一、简介
    • 二、Config Server 从本地文件读取数据
      • 1. 父pom.xml
      • 2.pom.xml
      • 3.bootstrap.yml
      • 4.ConfigServerApplication.java
      • 5.本地文件
      • 6.测试
    • 三、Config Client
      • 1.pom.xml
      • 2.bootstrap.yml
      • 3.ConfigClientDemoApplication.java
      • 4.测试

一、简介

配置中心可以从git、svn以及本地获取文件信息,这里采用本地文件方式读取配置信息

二、Config Server 从本地文件读取数据

1. 父pom.xml

<?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>org.example</groupId>
    <artifactId>parentSpringCloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eurekaDemo</module>
        <module>config-server-demo</module>
        <module>config-client</module>
    </modules>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
        </dependencies>

    </dependencyManagement>


</project>

2.pom.xml

<?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">
    <parent>
        <artifactId>parentSpringCloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server-demo</artifactId>

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

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

    </dependencies>


</project>

3.bootstrap.yml

server:
  port: 8088

spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

          #add-label-locations: classpath:/config


eureka:
  instance:
    prefer-ip-address: true
    #发送心跳给server的频率,每隔这个时间会主动心跳一次
    lease-renewal-interval-in-seconds: 1
    #Server从收到client后,下一次收到心跳的间隔时间。超过这个时间没有接收到心跳EurekaServer就会将这个实例剔除
    lease-expiration-duration-in-seconds: 1
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
    fetch-registry: true
    registry-fetch-interval-seconds: 8
    health-check-url-path: /actuator/health

4.ConfigServerApplication.java

package com.demo.config;

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);

    }
}

5.本地文件

文件名(/{name}-{profiles}.yml)
resource/config/config-client-dev.yml

server:
  port: 8087

6.测试

Spring Cloud Config 入门实例_第1张图片

三、Config Client

1.pom.xml

<?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">
    <parent>
        <artifactId>parentSpringCloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>

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

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


</project>

2.bootstrap.yml

spring:
  application:
    name: config-client
  profiles:
    active: dev
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      #uri: http://localhost:8088/
      fail-fast: true


eureka:
  instance:
    prefer-ip-address: true
    #发送心跳给server的频率,每隔这个时间会主动心跳一次
    lease-renewal-interval-in-seconds: 1
    #Server从收到client后,下一次收到心跳的间隔时间。超过这个时间没有接收到心跳EurekaServer就会将这个实例剔除
    lease-expiration-duration-in-seconds: 1
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
    fetch-registry: true
    registry-fetch-interval-seconds: 8
    health-check-url-path: /actuator/health

3.ConfigClientDemoApplication.java

package com.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

4.测试

通过8087(config -server配置的端口)可以正常访问
Spring Cloud Config 入门实例_第2张图片

你可能感兴趣的:(Spring,Cloud)