springcloud之config配置中心-Finchley.SR2版

本篇和大家分享的是springcloud-config配置中心搭建,写到这里突然想起自己曾今开源过基于Redis发布订阅编写的一个配置中心,刚看了git星数有点少哈哈,这里顺势发个连接欢迎大侠们点赞:https://github.com/shenniubuxing3/IConfCenter


  • springcloud版本说明
  • config-server配置中心
  • config-client配置客户端
  • eureka注册中心实现配置高可用

springcloud版本说明

由于市面上其版本比较多,版本不一可能造成了读者尝试时版本问题,所以这里指明当前作者写文章时使用的cloud版本
springboot版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

springcloud版本:

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

config-server配置中心

config配置中心主要是用来获取要发布的配置文件信息,并开放接口被其他调用者使用,先上maven配置:

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

通常在程序入口处添加注解 @EnableConfigServer 然后我们还需要知道开放那些配置文件作为配置信息来源,因此需要在application.yml文件中配置如下信息:

spring:
  application:
    name: config-server  #springcloud-config默认采用application作为name
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/my_study/study_java/springcloud_3/config-server/src/main/resources/config
  profiles:
    active: native
server:
  port: 4020

这里我使用本地的配置文件目录 src/main/resources/config 来提供配置文件,如果在windows上其实不用写file:///,不过官网还特别标注了windows上file后面要多一个 '/' 这里需要大家注意;这里我config文件夹下有两个配置文件,如下:
image
此刻我们最简单的配置服务就搭建好了,启动程序并访问如下地址:http://10.0.75.1:4020/config-server/conf1,conf0

值得注意的时候这里用 ',' 分割了下,在浏览器中得到如下两个配置文件合并后的信息:
image
可以去掉其中任何一个conf1或者conf0,得到的就是对应配置文件的信息,这里通过浏览器访问的路径规则是:
http://xx.xx.xx/{application}/{profiles}/{label} label默认null


config-client配置客户端

同样先来看pom的config-client对应的配置,这里多了个web依赖因为我打算在api接口信息看配置效果

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

然后在Application入口处增加注解 @EnableDiscoveryClient 下面就是配置文件中的信息了,要注意的是这个版本config-client的config相关配置要放在名称为 bootstrap.properties 的文件中(这是默认的配置文件名),如下bootstrap.yml信息:

spring:
  cloud:
    config:
      name: config-server    #application
      profile: conf1,conf0   #profile 后来者覆盖,没有合并
      label:                 #label
      uri: http://10.0.75.1:4020

需要注意的是uri配置的是刚才上面我们访问的config-server地址,其他的几个配置对应刚才说的url规则
application.yml配置:

spring:
  application:
    name: config-client
server:
  port: 5020

再来定义个api接口:

@RestController
public class ConfigController {

    @Value("${shenniu.author}")
    private String author;

    @Value("${shenniu.des}")
    private String des;

    @GetMapping("/getPort")
    public String getPort() {
        return "作者:" + author +
                "描述:" + des;
    }
}

此时运行config-client,通过开放的api接口返回映射的配置信息如下:
image


eureka注册中心实现配置高可用

高可用通俗来讲就是部署多个服务,当某个挂掉的时候其他的顶上去,这里使用Eureka注册中心(后面可能会分享关于zk和consul);先创建个eureka-server项目并运行起来:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

在Application入口处添加 @EnableEurekaServer 注解启动eureka服务,这里我分别启动1020,1021,1022三个eureka服务端口,使其自身是高可用的,相关配置:

spring:
  application:
    name: eureka
server:
  port: 1020
eureka:
  instance:
    appname: ${spring.application.name}
  client:
#    register-with-eureka: false  #开启自动注册到eureka中心,高可用
#    fetch-registry: false
    service-url:
      defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/
  server:
    eviction-interval-timer-in-ms: 30000  #检测失效信息的时间
    enable-self-preservation: false  #关闭自我保护
    use-read-only-response-cache: false

下面需要分别改造下config-server和config-client的配置,可以遵循如下信息:
config-server
pom增加:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

application.yml增加:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/
  instance:
    appname: ${spring.application.name}
    prefer-ip-address: true

config-client
pom增加:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

bootstrap.yml改造:

spring:
  cloud:
    config:
      name: config-server    #application
      profile: conf1,conf0   #profile 后来者覆盖,没有合并
      label:                 #label
#      uri: http://10.0.75.1:4020
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/
  instance:
    appname: ${spring.application.name}
    prefer-ip-address: true

如果可以吧config-server多开几个端口,都注册到eureka中心,成功后如下信息:
image
同样访问api接口时得到如下获取配置成功信息
image

项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值