Spring Cloud之配置中心搭建

一、配置中心服务端搭建

1)引入相关Maven坐标

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

2)加入相关注解

//加上@EnableConfigServer注解开启配置服务器的功能
@EnableConfigServer
@EnableEurekaClient

3)相关配置属性

server:
  port: 8989
spring:
  application:
    #  需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
    name: zbq-config-server
#  指定主机名称
#  cloud:
#    client.hostname: localhost


# 在配置文件中注明自己的服务注册中心的地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8781/eureka
# 开启除了主机名也可以通过ip来定义注册中心地址
  instance:
    prefer-ip-address: true
    ip-address: localhost
    # 定义服务续约任务的调用间隔时间,默认为30秒
    lease-renewal-interval-in-seconds: 30
    # 定义服务失效的时间,默认为90秒
    lease-expiration-duration-in-seconds: 90
#    preferIpAddress: true
#    hostname: ${spring.cloud.client.ipAddress}
#    instance-id: ${spring.cloud.client.ipAddress}:${server.port}





# 配置config中心
spring.cloud.config:
  server:
#  配置git仓库地址 http方式
#    git.uri: https://github.com/zhangboqing/zbq-config-center.git
#  配置git仓库地址 ssh方式
    git:
      uri: ssh://[email protected]:zhangboqing/zbq-config-center.git
#  跳过ssh校验
#      skipSslValidation: true
      ignoreLocalSshSettings: true
#  下面两个参数没怎么弄明白,配上不好用,感兴趣的可以研究下
#      hostKey: someHostKey
#      hostKeyAlgorithm: ssh-rsa
      privateKey:  |
                    -----BEGIN RSA PRIVATE KEY-----
                    你的私钥
                    -----END RSA PRIVATE KEY-----
#  配置仓库路径下相对搜索位置,可以配置多个
    git.searchPaths: zbq/myconfig1,zbq/myconfig2
# 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
#  访问git仓库的用户名
#    username:
#  访问git仓库的用户密码
#    password:
# 配置仓库的分支
  label: master
#logging:
logging:
  level:
    root: info
    additivity: off
  file: /data/home/logs/zbq-config-server/zbq-config-server.log

二、配置中心客户端搭建

1)将项目配置迁移到zbq-config-center中(统一存放配置的项目)

  配置文件的命名必须按照下面的规范

  {application}-{profile}.properties 或 {application}-{profile}.properties 

  application和profile代表什么,看4点!!!

2)引入相关Maven坐标

 <!--spring cloud-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 连接配置中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- retry -->
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- actuator监控模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

3)加入相关注解

@EnableDiscoveryClient

4)相关配置属性,删除原来项目的application.yml文件,加入bootstrap.yml配置文件,这样就可以读取到远程的配置文件了

##在配置文件中注明自己的服务注册中心的地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8781/eureka

# 注意:下面配置必须配置在bootstrap.yml或.properties中
# 访问配置信息URL与配置文件的映射关系
# /{application}/{profile}/{label}
# /{application}/{profile}.yml
# /{label}/{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.properties
spring:
# 需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
# 对应配置文件规则中的{application}部分
  application.name: zbq-backend
  cloud.config:
# 对应配置文件规则中的{profile}部分
    profile: dev
# 对应配置文件规则中的{label} 部分
    label: master
# 配置中config-server的地址
# 1.通过URI指定配置中心
#    uri: http://localhost:8783/
# 2.通过注册中心,来发现注册中心
    discovery:
# 开启通过服务访问Config Server的功能
      enabled: true
#  指定Config Server注册的服务名
      serviceId: zbq-config-server

# 失败快速响应与重试
# 开启客户端优先判断Config Server获取是否正常,并快速响应失败内容
    fail-fast: true

# 设置重试参数
    retry:
# 下一间隔的乘数
      multiplier: 1.1
# 初始重试间隔时间(单位毫秒),默认为1000
      initial-interval: 1000
# 最大间隔时间,默认2000毫秒
      max-interval: 2000
# 最大重试次数,默认6次
      max-attempts: 6

# 动态刷新配置
# config-client中新增spring-boot-starter-actuator监控模块,其中包含了/refresh端点实现
# 该端点可用于实现客户端重新获取和刷新配置信息
#log
logging:
  level:
    root: info
    additivity: off
  file: /data/home/logs/zbq-backend/zbq-backend.log
  pattern:
    console: "%d{yyyy-MM-dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"

猜你喜欢

转载自www.cnblogs.com/756623607-zhang/p/9632657.html
今日推荐