资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringCloudConfig配置Client读取不到server配置文件

学习Spring Cloud Config 的时候,很容易遇到各种各样的问题,这里我就把我遇到的:Client 端读取不到Server端的配置文件中的属性总结一下。

首先我搭建了一个Eureka 注册中心,这里就不着重介绍了,不知道的小伙伴可以
网上查资料!

成都网站建设哪家好,找创新互联公司!专注于网页设计、重庆网站建设公司、微信开发、微信平台小程序开发、集团成都企业网站建设等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:茶楼设计等众多领域,积累了大量丰富的经验,同时也获得了客户的一致表扬!

1.搭建Config 配置中心

POM 文件:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.sg.config
    config-demo
    0.0.1-SNAPSHOT
    config-demo
    Demo project for Spring Boot

    
        1.8
        Greenwich.SR2
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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


这里面Config用到的依赖是 spring-cloud-config-server ,其他自己看着引入即可

2.application.yml 的配置

server:
  port: 8095
spring:
  application:
    name: config-demo
  cloud:
    config:
      server:
        git:
          uri: https://github.com/****/spring-cloud-demo.git
          username:
          password: 
          search-paths: spring-cloud-demo-config
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8090/eureka/
    register-with-eureka: true
    fetch-registry: true

Config Server 的配置主要是spring.cloud.config.server.git 下面的东西,其他按需配置即可。

3.Config Server 启动类

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
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigDemoApplication {

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

}

至此 Config Server 端就配置完成了。

4.配置Config Client 端

首先引入依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.sg.eureka.client
    eureka-client-demo
    0.0.1-SNAPSHOT
    eureka-client-demo
    Demo project for Spring Boot

    
        1.8
        Greenwich.SR2
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.sg.common
            spring-cloud-common
            0.0.1-SNAPSHOT
        
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

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

主要有关的依赖是 spring-cloud-starter-config ,其他的按需引入即可

5.application.yml 配置文件
server:
  port: 8091
spring:
  application:
    name: eureka-client-demo
  cloud:
    config:
      profile: test
      uri: http://127.0.0.1:8095/
      label: master
      discovery:
        enabled: true
        service-id: config-demo

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8090/eureka/

### 端点控制
management:
  endpoints:
    web:
      exposure:
        # 开启指定端点
        include: hystrix.stream

project:
  name: hahha

启动类上什么配置也不用添加,就可以了,重要的一点:
1:如果用了Eureka ,则需要配置 spring.cloud.config.discovery.enable: true 和 spring.cloud.config.discovery.service-id: config-demo 这两个属性

6.读取Server中配置的属性 
package com.sg.eureka.client.controller;

import com.sg.common.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

/**
 * @author liuhailong
 * @date 2019-08-08
 */
@RestController
@RequestMapping(value = "users")
@Configuration
public class UserController {

    @Autowired
    private Environment environment;

    @Value("${project.name}")
    private String name;

    @GetMapping(value = "/config")
    public String getConfig(@RequestParam("key")String key){
        return environment.getProperty(key);
    }

    @GetMapping(value = "/projectName")
    public String projectName(){
        return name;
    }

}

我这里用了 两种方式去读取配置文件中的内容
1:使用Spring core中的Environment 类 中的getProperty 可以取到
2:使用Spring 的 @Value("${project.name}") 注解

6.接下来验证一下:访问 http://localhost:8091/users/config?key=project.name 结果发现获取不到Config Server中配置的参数。
主要原因:
Spring Cloud 会首先加载bootstrap.yml 和bootstrap.properties 配置文件,然后再去加载spplication.properties 配置文件,所以在Config client 中的配置文件名称要修改为 bootstrap.yml 。然后在读取配置中心Config Server 中的 eureka-client-demo-test 的配置文件,这样就可以读取到了。

网站栏目:SpringCloudConfig配置Client读取不到server配置文件
标题URL:http://cdkjz.cn/article/gcoesc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220