Java-Spring-Spring Cloud Config的简单使用

首先你需要一个eureka服务,用来分发和服务间互相调用。

导入包(这个是官网的配置)

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>{spring-boot-docs-version}</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>{spring-cloud-version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

<build>
	<plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
	</plugins>
</build>

   <!-- repositories also needed for snapshots and milestones -->

然后,只需要开启就可以了,重点在配置文件。

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

配置application.yml

spring : 
  cloud : 
    config : 
      server : 
#        git : 
#          uri : http://192.168.30.59/ly-sky/lygtc.git     # 配置git仓库的地址
#          searchPaths : ly-cloud-config-repo # git仓库下的相对地址,可以配置多个,用,分割。
#          username : lyyfsb  # git仓库的账号
#          password : lygtc123456  # git仓库的密码
        native:
          search-locations: ${searchPath:classpath:/properties/}
  profiles: 
#    active: dev
    active: native # 读取本地配置文件
# 把配置服务器发布到配置中心
eureka : 
    instance : 
        prefer-ip-address : true
        lease-renewal-interval-in-seconds : 30
        lease-expiration-duration-in-seconds : 10
    client : 
        service-url : 
            defaultZone : ${defaultZone:http://${discovery:discovery}:1200/eureka/}
        register-with-eureka : true
        fetch-registry : true

这里官方推荐的是使用git仓库来作为远程配置文件储存,具体可以去看看,这里采用本地读取。

配置文件的读取位置为运行时环境参数searchPath指定的位置,这里最好就不要写死。

比如设定运行时环境变量为:

searchPath: file:/opt/properties

那就会到/opt/properties文件夹下去读取相应的配置文件。

search-locations可以是绝对路径也可以是classpath路径。

然后,我们来配置服务的bootstrap.yml

spring : 
  application : 
    name : ly-bd-data-collection-svc
  cloud:
      config:
#       uri : http://localhost:8040/ #从注册服务中取(指定配置服务器地址)
        profile: dev # 此名称将和application-name一起构成配置文件名
        label: master
        discovery:
          enabled: true
          service-id: ly-cloud-config-server #配置服务器的服务名称

读取的配置文件名称为#{spring-application-name}-#{spring-cloud-config-profile}.yml,这里就是:

ly-bd-data-collection-svc-dev.yml

这里也看到了配置服务器也是可以直接指定url的,所以不通过eureka也是可以使用的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值