栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud Config本地以及远端模式实践

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud Config本地以及远端模式实践

Spring Cloud Config本地以及远端模式实践 前言

本篇主要整理了spring cloud config的使用,包含本地模式以及远端模式。

项目工程

包含一个eurekaServer注册中心,一个eurekaClient,一个Spring Cloud Config配置中心。

Spring Cloud Config配置

首先是pom文件,需要添加spring cloud config的依赖支持,同时也将spring cloud config作为eureka Client注册到eureka上面,也要添加eureka相关依赖。

具体pom文件如下:



    4.0.0

    org.example
    configserver
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.10.RELEASE
    

    
        1.8
        1.8
        UTF-8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
        
    

    
        
            org.projectlombok
            lombok
        
        
            junit
            junit
            test
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

第二步,需要在启动类上面添加一个@EnableConfigServer注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {

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

第三步,添加相关配置项

这边分为两种模式,一种是本地模式,就是配置文件存放在本地目录,一种是远端模式,是配置文件存放在服务器上面。

  • 本地模式

    本地模式需要设置spring.profiles.active属性为native,同时设置spring cloud config指定存放配置文件的目录。

    spring:
      application:
        name: config-server
      profiles:
        active: native # 本地模式
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/cloud-repo # 指定目录方式
    server:
      port: 8888
    

    我这边在项目的resources目录下新建了一个cloud-repo目录,用于存储读取的配置文件信息。


    config.properties配置文件:

    name=yuanzhihao
    

    启动项目,可以通过浏览器输入http://{ip}:{port}/{application}-{profile}.properties去访问到我们配置的文件信息。

    application对应的就是配置文件的名称,profile对应的是配置文件的版本信息,我们这边开发的时候会根据profile后缀来区分不同的开发环境的配置信息,如果配置文件不区分版本的话,通过验证,profile随便填什么都是访问的自己。

  • 远端模式

    远端模式支持通过配置代码仓库的地址以及账号和密码来读取存放在代码服务器上面的配置文件。
    配置文件如下:

    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: http://localhost:5001/root/cloud-repo.git # 指定代码仓地址
              username: root # 用户名
              password: 12345678 # 密码
              default-label: main # 仓库分支
              search-paths: cloud-repo
    @RestController
    @Slf4j
    public class HelloController {
        @Value("${name}")
        private String name;
        
        @GetMapping("/hello")
        public String hello() {
            return "Hello " + name;
        }
    }
    

    启动client1,通过浏览器访问该接口,可以获取到name的值,读取成功!

    项目源码参考

    源码地址:https://github.com/yzh19961031/SpringCloudDemo

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602590.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号