加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

SpringCloud-7-Config

发布时间:2020-12-15 01:16:05 所属栏目:大数据 来源:网络整理
导读:Spring Cloud Config 目录 Spring Cloud Config 1. 概述 2. 服务端配置 1. 导入依赖 2. 配置文件 3. 配置主启动类 3. 客户端配置 1. 导入依赖 2. 配置文件 3. 配置主启动类 4. 配置测试用的controller 5. 实际运用 1. 概述 Spring Cloud Config 为微服务架构

Spring Cloud Config

目录
  • Spring Cloud Config
    • 1. 概述
    • 2. 服务端配置
      • 1. 导入依赖
      • 2. 配置文件
      • 3. 配置主启动类
    • 3. 客户端配置
      • 1. 导入依赖
      • 2. 配置文件
      • 3. 配置主启动类
      • 4. 配置测试用的controller
      • 5. 实际运用

1. 概述

  • Spring Cloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置

cloud-diagram-1a4cad7294b4452864b5ff57175dd983

Spring Cloud Config分为服务端客户端两部分

20190504154913458

2. 服务端配置

1. 导入依赖

<?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>SpringCloud</artifactId>
        <groupId>com.wang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringCloud-Config-server-3344</artifactId>

    <dependencies>
        <!--Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Spring Cloud Config Server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>

</project>

主要是web和config server的依赖

2. 配置文件

server:
  port: 3344
spring:
  application:
    name: springcloud-config-server
  #连接远程仓库
  #通过config-server,可以访问其中的资源以及配置
  cloud:
    config:
      server:
        git:
          uri: https://github.com/hello-world-cn/Spring-Cloud-Config.git
          default-label: main

注意

  • 默认的label为master
  • Github现在master改为了main,需要修改 default-label 属性,否则读不到

3. 配置主启动类

package com.wang.sringcloud;

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

@SpringBootApplication
//开启Config Server
@EnableConfigServer
public class Config_Server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_Server_3344.class,args);
    }
}

注意

  • 利用注解 @EnableConfigServer 开启Config Server服务

3. 客户端配置

1. 导入依赖

<?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>SpringCloud</artifactId>
        <groupId>com.wang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringCloud-Config-Client-3355</artifactId>

    <dependencies>
        <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>
        <!--Spring Cloud Client Config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>


</project>

注意

  • 客户端这里是 spring-cloud-starter-config

2. 配置文件

  • bootstrap.yaml
# 系统级别的配置,防止与远程冲突,这里级别最高
spring:
  cloud:
    config:
      uri: http://localhost:3344  #连接到本地的Config Server,通过服务端连接到github
      name: config-client #需要从git上读取的资源名称,不需要后缀
      profile: test
      label: main
  • application.yaml
# 用户级别的配置
spring:
  application:
    name: SpringCloud-Config-Client-3355

注意

  • 这里我们配置了两个配置文件,他们都可以被SpringBoot识别到,区别是优先级不同
  • 我们在bootstrap中配置springcloud client 的配置,防止远程配置与我们冲突. 这样无论如何都是本地的配置优先级最高
  • uri,name,profile,label 四个要素缺一不可

3. 配置主启动类

package com.wang.springcloud;

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

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

主启动类不需要任何除了SpringBoot以外的注解,因为客户端是连接本地的服务端!

4. 配置测试用的controller

方便测试,我们配置了controller

package com.wang.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;

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

    @RequestMapping("/config")
    public String getConfig() {
        return "applicationName: " + applicationName + "n" +
                "eurekaServer: " + eurekaServer + "n" +
                "port: " + port;
    }
}

注意

  • 由于我们在远程的配置文件上已经规定了端口,客户端只是中转给服务端去访问的,因此我们要访问的端口号与远程配置一致!

  • @Value("${}") ==> 用于获取配置文件中的属性值,这里读的是远程配置文件中的值

5. 实际运用

  • 先启动Server
  • 本地的配置只需要写bootstrap读取远程的配置即可
  • 本地配置利用Server提供的uri跳转,不要乱写!
  • 实现了配置和服务的解耦,方便运维调试!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读