配置中心搭建(一)基础搭建

版本

springboot

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

springcloud

<spring-cloud.version>2020.0.1</spring-cloud.version>

配置中心config-server

引入依赖

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

启动类加注解

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {

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

}

配置文件application.yml

server:
  port: 9005

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: http://git.xxx.com/xxx/config.git #gitlab地址
          search-paths: /config-repo/*
          username: your username #账号
          password: your password  #密码
          label: master #分支名称
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

客户端config-client

引入依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <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-bootstrap</artifactId>
        </dependency>

启动类加注解

@EnableDiscoveryClient  // 在注册中心发现服务
@SpringBootApplication
public class ConfigClientApplication {

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

测试controller

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

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return name+","+this.word;
    }
}

bootstrap.yml
注意一定要是bootstrap 不然会读取不到

server:
  port: 9006

spring:
  application:
    name: config-client
  cloud:
    #config客户端配置
    config:
      profile: dev #后缀名称
      name: config-client #配置文件名称
      label: master #分支名称
      uri: http://localhost:9005/ #配置中心地址
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

将配置文件放到gitlab上

在这里插入图片描述

启动测试

1.依次启动eureka、config-server、config-client

2.访问地址http://localhost:9005/config-client-dev.yml
在这里插入图片描述
3.访问地址http://localhost:9006/hello?name=11
在这里插入图片描述
测试成功,搭建完成

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值