SpringCloud config文件配置中心

SpringCloud config文件配置中心

  • 项目结构
    eurake-server
    config
    config-client

  • eurake-server
    eurake-server注册中心的创建就不讲了。上面已经讲过了。

  • config 配置的注册中心
    pom主要引入的架包

     	<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>
    

在这里插入图片描述
在这里插入图片描述

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
        System.out.println("配置中心服务端启动成功!");
    }
}

配置文件

spring:
  application:
    name: config
    #本地访问用profiles.active= native
  profiles:
    active: native
    #本地访问注释掉git
  #cloud:
  #  config:
  #    server:
  #      git:
  #        uri: https://github.com/xuwujing/springcloud-study/ #配置的Git长裤的地址
  #        search-paths: /springcloud-config/config-repo #git仓库地址下的相对地址 多个用逗号","分割
  #        username:
  #        password:
server:
  port: 9005
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8801/eureka/

添加配置访问文件 configtest.yml(一般用于数据库,redis等文件配置)

word: hello world
  • config-client配置调用方

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

    启动类

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

配置文件
application.yml

		server:
		  port: 8805
		  application:
		    name: config-client

bootstrap.yml

    spring:
	  cloud:
	    config:
	      name: configtest #获取配置文件的名称。
	      profile: pro #获取配置的策略。
	      label: master #获取配置文件的分支,默认是master
	      discovery:
	        enabled: true #开启配置信息发现。
	        serviceId: config #指定配置中心的service-id,便于扩展为高可用配置集群
	eureka:
	  client:
	    serviceUrl:
	      defaultZone: http://localhost:8801/eureka/

创建一个contoller

@RestController
public class HelloController {
    @Value("${word}")
    private String word;

    @GetMapping("/index")
    public String index(@RequestParam String name) {
        return name+","+this.word;
    }
}
  • 启动测试
    先后启动项目 eurake-server,config,config-client
    访问效果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值