SpringCloud-Spring Cloud编写Config Client

概述

编写Config Client



创建工程microservice-config-client




pom.xml

config-learning/microservice-config-client/pom.xml

<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.itmuch.cloud</groupId>
		<artifactId>microservice-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservice-config-client</artifactId>
	<packaging>jar</packaging>

	<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>
	</dependencies>

</project>




配置文件加载顺序

bootstrap.*里面的配置----》链接Config Server,加载远程配置----》加载application.*里面的配置;

说明:

         1)配置文件加载顺序,分别为:

                  1.1)bootstrap.*里面的配置;

                  1.2)链接ConfigServer,加载远程配置;

                  1.3)加载application.*里面的配置;




bootstrap.yml

spring:
  cloud:
    config:
      uri: http://localhost:8080
      profile: dev
      label: master   # 当configserver的后端存储是Git时,默认就是master 
  application:
    name: foobar

说明:

     1)spring.cloud.config.label 指明远程仓库的分支spring.cloud.config.profile

        1.1)dev:开发环境

        1.2)test:测试环境

        1.3)prod:正式环境

     2)spring.cloud.config.uri= http://localhost:8080/ 指明配置服务中心的网址。

     3)spring.cloud.application.name="foobar",指的是配置文件的名称;

     4)profile:dev,说明对应的配置文件是foobar-dev.yml

     5)由于配置文件的加载顺序,所以在配置config server的时候,配置在bootstrap.yml(自己猜的,不知道理解对不对);


小知识点:

    spring.cloud.config.profile可以不在application.yml中说明,可以在该服务启动时配置,如下图:)


有人会问,为什么不都配置在application.yml里面呢,而要配置bootstrap里面呢?

    因为boostrap用于应用程序上下文的引导阶段,通常用于引导上下文从外部资源获取配置属性,比如Spring Cloud Config Server,或者解密外部配置文件的属性等。

     默认的Config Server地址是localhost:8888. 所以我们只能在bootstrap.yml或者bootstrap.properties中修改。
    如需禁用引导过程,你可以设置spring.cloud.bootstrap.enabled=false







远程配置文件

https://gitee.com/it-much/config-repo-51cto-video/blob/master/foobar-dev.yml



application.yml

server:
  port: 8081



ConfigClientController

com.itmuch.cloud.ConfigClientController

package com.itmuch.cloud;

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

@RestController
public class ConfigClientController {

  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String getProfile() {
    return this.profile;
  }
}

说明:

    1)profile映射远程的配置;



ConfigServerApplication

com.itmuch.cloud.ConfigServerApplication

package com.itmuch.cloud;

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

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




测试


访问配置文件中配置的值

http://localhost:8081/profile



相关文章

1)https://blog.csdn.net/hunan961/article/details/79230859

2)https://blog.csdn.net/hh652400660/article/details/79012040












==============================

QQ群:143522604

群里有相关资源

欢迎和大家一起学习、交流、提升!

==============================


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值