springcloud(二)配置中心config

config配置中心介绍

spring cloud config是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他为分服务端和客户端。什么意思呢?简单点来说,再分布式架构中,通常会有很多的微服务,这时候就会产生很多的配置文件,springcloud config就是来集中管理这些配置文件,为什么需要集中管理呢?放到各自服务的资源目录下不香吗?1、分布式架构配置文件多;2、在开发过程中我们通常会经历3-4个服务器环境,比如开发环境-----测试环境------预发布环境-------生产环境,作为开发人员,他们是不知道测试服务器和生产服务数据库链接信息的,如果采用将配置文件放到当前项目下,那么每次发布版本的时候还要再项目中改来改去,很不友好,还有很重要的一点,那就是config配置中心支持修改配置无需重启的功能,要想使用这个功能,还得引入spring cloud的另外一个组件:bus,这里不做讨论。

好了,废话不多说,开始spring cloud config的项目搭建吧

1.需要有一个注册中心,参考:https://blog.csdn.net/qq_33220089/article/details/103150642

2.新建一个cloud-config的项目:

3.引入springcloud 与spring cloud config的依赖:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RC2</spring-cloud.version>
    </properties>

    <dependencies>

        <!--配置中心config依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--eureka客户端-->
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

4.编写配置文件bootstrap.yml
 

server:
  port: 9100  #端口

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ #注册中心地址

spring:
  application:
    name: cloud-config  #服务名
  profiles:
    #    active: native #拉取本地配置
    active: git #拉取git配置
  #    active: subversion
  cloud:
    config:
      server:
        #        native:
        #          search-locations: file:F:\git\springcloud\config #本地拉起
        git:
          uri: https://github.com/361426201/cloud-config.git #git地址   需要注意一点,我拉取的是公共仓库,不需要用户名和密码,如果你拉取的是私有仓库,需要加上用户名密码
          #   username: xxxx
          #   password: xxxx
          label: master #指定拉取git的那个分支
#        svn:
#          uri: xxxxxxxxxxxxx  #svn的地址
#          username: xxx  #用户名
#          password: xxx  #密码
#          default-label: trunk  #类似git中的分支
#          search-paths: {application}  #可有可无


上面有三种管理配置文件的方式:本地、git、svn,按需放开对应的配置。

5.修改启动类:

package com.ymy;

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

@SpringBootApplication  //springboot启动注解    相当于springboot1.5之前的:@SpringBootConfiguration + EnableAutoConfiguration + @ComponentScan
@EnableDiscoveryClient // 将服务注册到注册中心
@EnableConfigServer  //开启配置中心
public class CloudConfigApplication {

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

}

这样,配置中心就配置完成了,那微服务如何使用这个配置中心呢?

1.打开cloud-user,在eureka的使用中已经创建了,请看:springcloud(一)注册中心eureka

2.引入spring cloud config客户端所需要的依赖:

<!--连接配置中心所需要的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

3.修改bootstrap.yml:

server:
  port: 8800
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: cloud-user
  # could的配置信息一定要写到bootstrap.yml中
  # 因为cloud框架会优先读取bootstrap.yml配置文件,如果发现这里面没有这些配置,那么是无法将当前服务注册到注册中心的,这点需要特别注意,服务名也要写到ootstrap.yml文件中哦
  cloud:
    config:
      discovery:
        enabled: true   #如果希通过在配置中心找服务名的方式招待配置中心,那么这个属性就要设置成 true  默认false
        service-id: cloud-config #配置中心的服务名
      fail-fast: true   #将这个设置成true 表示连接配置中心失败,让程序启动失败

打开cloud-order:请看:springcloud(一)注册中心eureka

<!--连接配置中心所需要的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
server:
  port: 8900
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true  #是否注册到注册中心
    fetch-registry: true  #是否可以被检索

spring:
  application:
    name: cloud-order
  # could的配置信息一定要写到bootstrap.yml中
  # 因为cloud框架会优先读取bootstrap.yml配置文件,如果发现这里面没有这些配置,那么是无法将当前服务注册到注册中心的,这点需要特别注意,服务名也要写到ootstrap.yml文件中哦
  cloud:
    config:
      discovery:
        enabled: true   #如果希通过在配置中心找服务名的方式招待配置中心,那么这个属性就要设置成 true  默认false
        service-id: cloud-config #配置中心的服务名
      fail-fast: true   #将这个设置成true 表示连接配置中心失败,让程序启动失败
# 2.0之后,默认的监控端点地址加了上下文路径 actuator。可通过 management.endpoints.web.base-path属性进行修改,默认是: actuator
# 2.0之后,默认只开启了端点 info、 health。其他的需要通过 management.endpoints.web.exposure.include进行额外配置。
# 配置刷新
management:
  endpoints:
    web:
      exposure:
        include: '*'

4.由于我使用的是git,所以我在github上面准备了一份配置文件

这里需要注意一点,在spring cloud config中配置文件的名字是有规则的,要与你的服务名一致,如果不一致,会找不到配置问及那,如果不想一致,也是可以的,可以在cloud-user的配置文件中做修改即可,默认情况一定要一致哦。

启动eureka、config、cloud-user、cloud-order

为了方便测试,我在cloud-user中写了一个controller类做测试:

对应git上面的配置文件:

cloud-user:

对应git配置文件:

浏览器访问:http://localhost:8800/test:

浏览器输入:http://localhost:8900/test:

看到如下结果,表示已经从git中获取到了配置文件信息,好了,spring cloud config的配置就将到了这里,这仅仅是一个入门案例,这里面还可以为客户端加密解密等功能,感兴趣的小伙伴可以自己尝试。

发布了41 篇原创文章 · 获赞 79 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_33220089/article/details/103170508