springcloud 微服务config配置中心 本地获取和远端获取

springcloud-Config + eureka 本地获取和远程获取 (gitlab)

一、创建eureka注册中心

springboot版本为2.2.2.RELEASE
springcloud版本为Hoxton.SR6
以下都是这个版本

eureka注册中心,相信你会,没有什么特别的操作,就是一个简单的eureka注册中心即可
按自己的来就行!
application.yml

spring:
 application:
    name: config-eureka # 实例名
server:
  port: 1001 # 注册端口号
eureka:
  client:
    fetch-registry: false # 不注册自己,即自己为注册中心
    register-with-eureka: false # 不可被其他服务发现
    service-url:  # 注册地址
      defaultZone: http://localhost:1001/eureka/

主代码:

@EnableEurekaServer
@SpringBootApplication
public class ConfigserverApplication {
    
    

二、创建配置中心的服务端config-server(重点)

创建工程config-server
需要加入
在这里插入图片描述

此时的属性文件application.yml

spring:
  application:
    name: config-server # 注册在eureka中心的实例名
  profiles:
    active: native  # 表示从本地获取配置文件信息
server:
  port: 1002 # 注册端口号
eureka:
  client:
    service-url: # 注册在注册中心上
      defaultZone: http://localhost:1001/eureka/

主代码:

@EnableDiscoveryClient //这是一个eureka服务端
@EnableConfigServer //这是一个配置中心服务端
@SpringBootApplication
public class ConfigApplication {
    
    

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

1. 简单演示 这里采用本地获取配置文件,即在resources目录下直接创建配置文件比如(servicestation-dev.properties) ,以简化客户端获取该配置文件信息,其中"servicestation" 是意义上的文件名,“dev"是文件策略,中间以”-"连接
在这里插入图片描述

该配置文件内容:

word=hello world

三、创建配置中心的客户端config-client(重点)

创建工程config-client
需要添加的模块
在这里插入图片描述

在其属性文件里面,不仅需要application.yml 还需要 bootstrap.yml, 原因是什么CSDN里面有解释的

application.yml

spring:
  application:
    name: config-client #注册在eureka中心的实例名
server:
  port: 1003 # 注册端口号

bootstrap.yml

spring:
  cloud:
    config:
      name: servicestation # 获取配置文件的名称
      profile: dev # 获取配置文件的策略
#      label: dev # 获取配置文件的分支默认为master,由于是本地获取 label无效 则注释了
      discovery:
        enabled: true # 开启配置服务发现,表示从eureka注册中心发现服务 (service-id)
        service-id: config-server #指定配置中心的service-id 也就是服务名
eureka:
  client:
    service-url: # 注册在eureka注册中心
      defaultZone: http://localhost:1001/eureka/

主代码:

@EnableDiscoveryClient //表示这是一个eureka服务端
@SpringBootApplication 
public class ConfigclientApplication {
    
    

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

}

定义一个controller 获取配置文件信息

@RestController
public class ClientController {
    
    
    
    /**
     * 表示从配置文件中获取word
     */
    @Value("${word}")  
    private String word;

    @RequestMapping("/hello")
    public String index() {
    
    
        return "获取信息:" + this.word;
    }
}

一个简单的本地获取配置文件搭建完成,依次启动eureka注册中心、config-server和config-client
输入地址:http://localhost:1001/
出现下图表示config-server 和 config-client注册成功
在这里插入图片描述

输入地址http://localhost:1003/hello
出现下图,表示客户端获取配置信息成功!
在这里插入图片描述

改进!

在config-server的resources目录下新建一个目录config,用来存放配置文件,即将配置文件servicestation-dev.properties移动到config目录下
在这里插入图片描述

此时由于配置文件位置变化
则需要改动config-server工程里面的application.yml

spring:
  application:
    name: config-server # 注册在eureka中心的实例名
  profiles:
    active: native  # 使用本地获取
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config # 指定配置文件存放位置
server:
  port: 1002 # 注册端口号
eureka:
  client:
    service-url: # 注册在注册中心上
      defaultZone: http://localhost:1001/eureka/

其他的无需改动! 依次启动 输入http://localhost:1003/hello 效果一致

当然了你也可以放在盘的文件夹里,如D:\git_rep
在这里插入图片描述

只需要改变路径即可

  spring:
    cloud:
      config:
        server:
          native:
            search-locations: D:/git_rep  # 指定配置文件存放位置

!!!注意!!!!! 这里路径写法不一样 当路径盘里为D:\git_rep时,yml不可识别,需要改成D:/git_rep

下面为远程获取

在远程获取中 演示的是从gitlab获取配置文件信息
现在我的gitlab里面存放的配置文件
仓库:my-private-repo
配置文件所在分支:dev
配置文件:servicestation-dev.properties(这里的-dev dev是文件策略)
在仓库中的路径为 /config
在这里插入图片描述

本地获取改为远程获取,需要变动的地方有两个如下:

1.config-server里面的application.yml
注释spring.profiles.active=native 增加了git连接信息

spring:
  application:
    name: config-server # 注册在eureka中心的实例名
#  profiles:
#    active: native
  cloud:
    config:
      label: dev # 配置文件所在分支
      server:
        git:
          uri: [email protected]:xxx/my-private-repo.git # git仓库地址
          search-paths: /config # 配置文件地址
          username: xxxx # git仓库用户名
          password: xxxx # git仓库密码
server:
  port: 1002 # 注册端口号
eureka:
  client:
    service-url: # 注册在注册中心上
      defaultZone: http://localhost:1001/eureka/

2.config-client里面的bootstrap.yml
去掉label注释

spring:
  cloud:
    config:
      name: servicestation # 获取配置文件的名称
      profile: dev # 获取配置文件的策略
      label: dev # 获取配置文件的分支
      discovery:
        enabled: true # 开启配置服务发现,表示从eureka注册中心发现服务 (service-id)
        service-id: config-server #指定配置中心的service-id 也就是服务名
eureka:
  client:
    service-url: # 注册在eureka注册中心
      defaultZone: http://localhost:1001/eureka/

依次启动输入地址 localhost:1003/hello 效果一致!

总结

configServer
本地测试里面,
1 .如果配置文件是在resources目录下(resources/xxx.properties),即可不需要配置spring.cloud.config.server.native.search-locations=xxx 其他的情况下都需要配置该参数。
2. 如在resources目录下新建一个目录config, 在config目录存放xxx.properties,此时,需要配置spring.cloud.config.server.native.search-locations=classpath:/config ,或者配置文件在D盘里,则需要配置到相应的目录下(D:/config)!!!注意!!!,从盘里复制出来的路径可能是D:\config 但是yml文件识别不了,只能识别D:/config / 和 \ 一定要改过来!!!

configClient
注意情况
1.一般情况下需要application.properties和bootstrap.properties两个属性文件
在application中,只需要配置spring.application.name实例名 和 server.port端口号

在bootstrap中,需要配置获取配置文件的相关信息如
spring.cloud.config.name(配置文件名最好不要加文件策略和分支),
xxx.profile(文件策略dev或pro)
xxx.label(决定哪个环境的分支)
xxx.enabled(配置信息发现)
xxx.service-id(指定配置中心的服务名)
eureka.client.serviceUrl.defaultZone (注册在注册中心的地址)

第一次写博客,各位手下留情!有什么问题,评论即可!

猜你喜欢

转载自blog.csdn.net/qq_41831842/article/details/112622804
今日推荐