springcloud-config简单搭建及从git获取配置文件问题

springcloud-config从git获取配置文件

配置文件git地址:https://github.com/1147549577/springCloud

1. springcloud-config-server

  • maven包
	<dependency>     
	     <groupId>org.springframework.cloud</groupId>
	     <artifactId>spring-cloud-config-server</artifactId> 
	</dependency>
  • 将@EnableConfigServer添加至启动类
  • application.yml文件,我有使用Eureka。
server:
  port: 1001

eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      defaultZone: http://localhost:1000/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    appname: springcloud-config-server

spring:
  application:
    name: springcloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/1147549577/springCloud
          searchPaths: '{application}'

'{application}'的意思为 Eureka中注册的各个ID,即配置文件中 spring.application.name名称。记得加’号,不然访问有问题的哦!
searchPaths: '{application}'这个的意思是:访问https://github.com/1147549577/springCloud这个项目下的命为 '{application}'的文件夹。当然你也可以不使用'{application}'占位符,如searchPaths: development
在这里插入图片描述
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

  • /{application}/{profile}[/{label}]

匹配规则:

  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

访问:http://localhost:1001/随便写({application})/随便写({profile}) 会返回一个json文件,就表示成功了。
http://localhost:1001/springcloud-test-server/dev会返回 springcloud-test-server-dev.yml(因为我就创了一个.yml文件)的json文件。

{"name":"springcloud-test-server","profiles":["dev"],"label":null,"version":"93c651cb0f70844a2b64a1cce8ffc2f826095fa3","state":null,"propertySources":[{"name":"https://github.com/1147549577/springCloud/springcloud-test-server/springcloud-test-server-dev.yml","source":{"server.port":8001,"foo":1234}}]}

当然 http://localhost:1001/随便写/随便写 这个访问,也会获取到json文件就是了,只是propertySources里没有东西罢了。
反正只要就返回json文件访问git就是成功的。

2. springcloud-config-client

  • maven包,一定记得写啊,我就是漏了,导致无法获取到值。如果正常导入,idea中在编写bootstrap.yml的时候ALT+/能快速补全单词。
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
  • 在application.properties下新增一个bootstrap.yml文件,springcloud加载顺序是先加载bootstrap,然后加载application。
server:
  port: 1996
spring:
  application:
    name: springcloud-test-server
  cloud:
    config:
      discovery:
        enabled: true
        # 这个是去Eureka中获取命为springcloud-config-server的服务
        service-id: springcloud-config-server
      fail-fast: true
      # 表明文件名是以dev结尾的
      profile: dev
      # git分支
      label: master

eureka:
  client:
    service-url:
      defaultZone: http://localhost:1000/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    appname: springcloud-test-server
  • 修改启动类
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
public class SpringCloudTestServerApplication {

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

	@Value("${server.port}")
	String port;
	@Value("${foo}")
	String foo;

	@RequestMapping("/hi")
	public String home(@RequestParam(value = "name", defaultValue = "Test") String name) {
		return "hi " + foo + " ,i am from port:" + port;
	}
}

然后这样就阔以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值