手把手搭建SpringCloud,第六篇——使用Spring Cloud Config统一管理微服务配置

前言概述:

为什么要使用统一管理微服务配置

对于传统的单体应用,常使用配置文件管理所有配置。例如一个SpringBoot开发的单体应用,直接使用application.yml配置,如果需要切换环境,可设置多个Profile,并在启动应用时指定spring.profiles.active={profile}.

然而,在微服务架构中,微服务的配置管理一般有以下需求

  • 集中管理配置。一个使用微服务架构的应用系统可能会包含成千上百个微服务,所以集中配置是很有必要的。
  • 不同环境不同配置。例如,数据源配置在不同环境(开发、测试、预发布、生产)中是不同的。
  • 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整时不停止微服务。
  • 配置修改后可自动更新。如配置发生变化,微服务能够自动更新配置

 项目创建:

笔者新建了三个项目,Eureka服务集群使用,在这里就不再赘述,前几篇博客中都有提及,可以去看下操作步骤。

另,统一配置的文件是放在github上的,所以以下项目配置也是针对于GitHub配置。也可以在svn或者本地,效果是一样的。

如果没有GitHub,并且不熟悉GitHub,那就现在本地测试,最后也会将本地测试案例。

 

创建项目前,先在GitHub上创建配置文件,建议名字规范,dev开发环境,test测试环境,pro生产环境

 

 内容,根据你的需求而定,我这里是根据不同的环境,在name后面加了环境标识

 

项目创建1 

pom.xml

<!-- Eureka Client -->
		<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-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Config Server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

application.yml

server:
  port: 8283
  
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:spc-config-server:8283
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1  
       
spring:
  application:
    name: spc-config-server
  cloud:
    config:
      #配置仓库分支,github默认master
      label: master
      server:
        git:
          #仓库地址
          uri: https://github.com/Leexiaobei/repositoryTest
          #指定路径,如果配置文件是直接放置仓库路径下,可以不用写search-paths
          search-paths: config
          #用户名,密码,我这里是公有仓库,无密码
          username: 
          password: 
          
      
          

启动类添加注解

package com.portal;

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

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpcConfigServerApplication {

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

}

 项目1,config server 配置完毕,启动访问Eureka管理界面,下图,代表服务注册成功

那么来测试一下

 注:看图中标识的红色框框,先说请求路径,这应该是规定的请求方法,会把配置文件名称的最后一个“-”转成“/”方式请求,其他都是不可以了,其实也不需要深究。

再说说红色框框二,GitHub的仓库路径,直接能够获取到你请求的文件绝对路径

红色框框三中,就是配置文件的信息了。已经获取到了。所以到此 Config Server已经配置成功了。

 

项目创建2:

同样新建一个springboot-web项目,

注:这一步时,我们创建bootstrap.yml或bootstrap.properties文件,而不创建application.yml或application.properties文件;配置在bootstrap中才能生效,而不是在application配置文件中。

原因:bootstrap配置文件会比application更加早的加载

pom.xml

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

 

bootstrap.yml

server:
  port: 8284
  
spring:
  application:
    name: spc-config-client-server
  cloud:
    config:
      #指定对应config-server的地址端口号
      uri: http://localhost:8283
      #环境
      profile: dev
      #git上仓库的分支,默认为master
      label: master

 编写测试类

package com.portal;

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

@RestController
public class Controller {
	
	@Value("${user.name}")
	String name;
	
	@GetMapping("/name")
	public String getname()
	{
		return name;
	}

}

插播的小知识

 开始测试项目2

不要惊讶和费解,为啥获取到的是dev的配置信息,因为我的配置文件里标识的是dev

 

再测试一次,把配置文件中 profile: dev 改成  profile: test,测试

 很明显,结果是成功的。

为了方便理解,附加关系图


 上面讲了在GitHub上配置,如果你不是很熟悉GitHub,就按照这种方式先了解一下。但建议还是去学习学习GitHub

好了,废话不多说了,直接上码,还是已上面两个项目进行修改

项目1修改,

application.yml

server:
  port: 8283
  
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:spc-config-server:8283
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1  
       
spring:
  application:
    name: config-server
  profiles:
  #   自动搜索资源文件夹下的配置文件
    active: native

新建一个Source Folder,这里注意一下,不是新建Folder。不然是没用的

 

 

其他不变,启动项目,来测试一下 

 

结果,成功。

修改项目2

bootstrap.yml

server:
  port: 8284
  
spring:
  application:
    #注意。这里名称要和Config Server的配置文件对应
    name: myClient
  cloud:
    config:
      #指定对应config-server的地址端口号
      uri: http://localhost:8283
      #环境
      profile: test
      #git上仓库的分支,默认为master
      label: master

 编写测试类

package com.portal;

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

@RestController
public class Controller {
	
	@Value("${person.name}")
	String name;
	
	@Value("${person.gender}")
	String gender;
	
	@GetMapping("/name")
	public String getname()
	{
		return name + "\r\n" + gender;
	}

}

 启动项目,测试

返回结果,成功。 


总结:笔者写博客不仅仅是为了分享,也是为了给自己做笔记。

以上方法亲测有效,如果问题可下方回复,

不当之处,请多指教

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值