SpringCloud学习系列之二-----配置中心(Config)使用翔解("^_^")

前言

这篇讲的是springcloud 的分布式配置中心(SpringCloud Config)的相关使用。

Spring Cloud Config介绍

Spring Cloud Config 项目是一个解决分布式系统的配置管理方案。包含客户端(client)和服务端(server)两个部分。服务端提供配置信息的存储,以接口的形式将配置信息的内容提供出去。客户端根据接口获取数据,并依据该数据初始化自己的应用。

开发准备

  1. idea (不一定要用idea,不一定要用idea,不一定要用idea,这只是我在用而已)
  2. JDK 1.8以上(最低也是1.8啊,不能在笑了{ ^ … ^ })
  3. Spring Boot 2.1.7.RELEASE

注:不一定非要用上述的版本,可以根据情况进行相应的调整。需要注意的是SpringBoot2.x以后,jdk的版本必须是1.8以上! 当然,如果你需要,请----》百度《 嘎嘎嘎》
需要创建的几个服务,一个一个的来,别着急,一个一个的来,别着急,一个一个的来,别着急。

1.cloud-config-eureka
2.cloud-config-server
3.cloud-config-client

一.肯定需要注册中心的啦,创建cloud-config-eureka
1.新建项目是添加jar包 / 编辑pom.xml:
<dependencies>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

注: 基于SpringBoot1.x以上SpringCloud是Dalston版本的eureka 依赖是 spring-cloud-starter-eureka,少了个 netflix 。
SpringCloud的版本命名方式是通过伦敦的地方来命名的,版本顺序是根据首字母的顺序来的。


2.编辑application.properties
spring.application.name=cloud-config-eureka
server.port=1000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:1000/eureka/

配置说明:

spring.application.name:服务名称
server.port : 服务端口
eureka.client.register-with-eureka: 表示是否将自己注册到eureka上,默认是true
eureka.client.fetch-register: 表示是否从eureka服务上获取注册信息
eureka.client.serviceUrl.defaultZone: 这是设置eureka 服务交互的地址,客户端的查询服务和注册服务都要依赖这个地址

3.代码实现

在启动类上添加@EnableEurekaServer 注解,表示当前服务是注册中心。
代码示例:

@EnableEurekaServer
@SpringBootApplication
public class CloudConfigEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudConfigEurekaApplication.class, args);
        System.out.println("注册中心启动。。。。");
    }

}

注册中心配置好了以后,添加配置中心。

二.配置中心:cloud-config-server
1.新建项目时添加jar包 / 编辑pom.xml;
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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>
    </dependencies>
2.配置application.properties 如下:
spring.application.name=cloud-config-server
server.port=1200
#spring.profiles.active=native
eureka.client.service-url.defaultZone=http://localhost:1000/eureka

 spring.cloud.config.server.git.uri = https://github.com/xpf0321/cloud-config-test/
 spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo
 spring.cloud.config.server.git.username =git用户名
 spring.cloud.config.server.git.password =git 密码

配置说明:

spring.application.name:服务名称
server.port : 服务端口
eureka.client.register-with-eureka: 表示是否将自己注册到eureka上,默认是true
spring.profiles.active:使用本地配置,将属性添加nation ,同时在resource 文件夹下添加一个properties文件配置就行
spring.cloud.config.server.git.uri: 配置的Git长裤的地址。
spring.cloud.config.server.git.search-paths: git仓库地址下的相对地址 多个用逗号","分割。
spring.cloud.config.server.git.username:git仓库的账号。
spring.cloud.config.server.git.password:git仓库的密码。

这里为了进行本地配置文件测试,新建一个configtest.properties配置文件,添加如下内容:、

mi=hello word
name=xpf
age=26

3.代码实现

so easy la 。在启动上添加一个@EnableConfigServer注解,表示配置中心服务,就OK啦,额,不好意思,忘了一个,在添加一个EnableDiscoveryClient 注解。
代码示例:

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudConfigServerApplication .class, args);
        System.out.println("配置中心启动了");
    }

}

这样我们的配置中心,也完成了。


三。客户端:cloud-config-client

1.新建项目时添加jar包 / 编辑pom.xml文件

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
		<!--导入这个只是方便写controller-->
        <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>
    </dependencies>

2.编辑application.properties

spring.application.name=cloud-config-client
server.port=1201

配置说明:

spring.application.name: 服务名称
server.port: 服务端口

3.程序主类(启动类)只要添加一个注解@EnableDiscoveryClient进行标识,就OK啦。
代码示例:

@EnableDiscoveryClient
@SpringBootApplication
public class CloudConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudConfigClientApplication.class, args);
        System.out.println("客户端启动。。。。");
    }
}

下面就是最大的变动说明:bootstrap.properties
创建一个bootstrap.properties文件,并添加如下信息:

spring.cloud.config.name=configtest
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=cloud-config-server
eureka.client.service-url.defaultZone=http://localhost:1000/eureka/

spring.cloud.config.name: 获取配置文件的名称
spring.cloud.config.profile: 获取配置的策略(就是你时生产环境,还是开发环境或测试环境)
spring.cloud.config.lable: 获取配置文件分支,默认时master。如果时获取的是本地配置,则无用
spring.cloud.config.discovery.enabled: 开启配置发现
spring.cloud.config.discovery.service-id: 指定配置中心的service-id,便于扩展为高可用配置集群。
eureka.client.service-url.defaultZone: 这个是设置与Eureka Server交互的地址,客户端的查询服务和注册服务都需要依赖这个地址。

注:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为bootstrap.properties的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。需要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.properties,不然客户端是无法获取配置中心参数的,会启动失败!

添加一个controller
为了方便查询,在控制中进行参数的获取,并返回。@Value注解是默认是从application.properties配置文件获取参数,但是这里我们在客户端并没有进行配置,该配置在配置中心服务端,我们只需指定好了配置文件之后即可进行使用。

@RestController
public class ClientController {

    /**
     * name
     */
     @Value("${mi}")
     private String name;

     @RequestMapping("hello")
     public String hello(@RequestParam("info")String info){
         return name+","+info;
     }

}

到此,客户端项目也就构建完成了。


功能测试

本地测试
项目启动顺序:

cloud-config-eureka
cloud-config-server 注:在application.properties里添加spring.profiles.active=native配置。并注释掉git相关配置。,然后在src/main/resources目录下新建一个configtest.properties文件,然后在里面添加一个配置mi=hello world。
cloud-config-client

启动成功后,访问路径

http://localhost:1000

在这里插入图片描述
访问配置中心

http://localhost:1200/configtest-1.properties

在这里插入图片描述
:配置文件的名称是configtest.properties,但是如果直接该名称的话是获取不到的,因为在配置文件名需要通过-来进行获取,如果配置文件名称没有-,那么添加了-之后,会自动进行匹配搜索。

springcloud config 的URL与配置文件的映射关系如下:

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

上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不同的分支,默认为master。

访问客户端服务

http://localhost:1201/hello?info=xpf

在这里插入图片描述
GIT测试
在完成本地测试之后,我们把这个spring.profiles.active=native配置注释掉,解除spring.cloud.config.server.git相关的注释(账号和密码要填写真实的),然后在git仓库上建立一个config-repo 文件夹,新建configtest-pro.properties、configtest-dev.properties两个配置,这两个的配置分别是mi=hello world!!和mi=hello world!, 然后和configtest.properties配置文件一起上传到config-repo 文件夹中。
上传了configtest.properties文件,但是这个文件名称没有-,我们想获取其中参数的信息的话,可以在然后-随意添加一个参数,它会自动进行匹配,在浏览器输入:

http://localhost:1200/configtest-dev.properties

浏览器返回:
在这里插入图片描述

客户端服务输入:

http://localhost:1201/hello?info=xpf

在这里插入图片描述

大神博客指导:神龙见首不见尾:虚无境

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值