eclipse创建子工程config本地配置中心(cloud搭建三)

一 简介

      Config Server可以从本地仓库读取配置文件,也可以从远处Git仓库读取。本地仓库是指

将所有的配置文件统一写在Config Server工程目录下。Config Server暴露Http API接口,Config
Client通过调用Config Server的Http API接口来读取配置文件。

二配置

1.创建子服务工程
2.修改pom文件

<dependencies>
	  <dependency>
	     <groupId>org.springframework.boot</groupId>
	     <artifactId>spring-boot-starter-web</artifactId>
	  </dependency>

	  <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
  </dependencies>

3.创建启动类

package com.fsp.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer//开启ConfigServer功能
public class ConfigApplication {
	
	public static void main(String[] args) {
		
		SpringApplication.run(ConfigApplication.class, args);
	}

}

4.修改application.yml

spring:
  #服务名称
  application:
    name: fsp-config
  profiles:
    active:
    - native #代表从本地文件读取
  cloud:
    config:
      server:
        native:
          search-locations:
          - classpath:/shared  #配置文件路径
    
#服务端口
server:
  port: 8762
  
eureka:
  client:
    service-url:
      defaultZone: http://administrator:123456@10.0.199.19:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

5.在resources下创建shared文件夹,这个名称和application中配置的路径一致
在这里插入图片描述
6.在shared文件夹下创建配置文件

#服务端口
server:
  port: 8082
  
eureka:
  client:
    service-url:
      defaultZone: http://administrator:123456@10.0.199.19:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

7.config客户端获取测试
客户端在他的pom文件添加

<dependency>
		 <groupId>org.springframework.cloud</groupId>
		 <artifactId>spring-cloud-starter-config</artifactId>
	  </dependency>

然后修改application.yml为bootstrap.yml内容(bootstrap优先于application加载)

spring:
  #服务名称
  application:
    name: fsp-test
  profiles:
    active:
    - dev
  cloud:
    config:
      uri:
      - http://10.0.199.19:8762
      fail-fast: true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值