spring cloud config,读git数据 简单实例

pom,服务端

 <dependencies>
        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml 这里真的是磕磕碰碰,刚开始用的是ssh,后来各种报错,最后用的https
这里用的是 国产gitee。并且已经有个仓库名字springcloud-config

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
          git:
            uri: https://gitee.com/caomengwei1/springcloud-config.git
            search-paths:
                  - springcloud-config
            username: git账号
            password: 密码
      label: master #分支
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka
  instance:
    instance-id: config-3344  #修改主机name
    prefer-ip-address: true   #显示ip端口

主启动

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

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

gitee springcloud-config仓库,里面有个 config-text.yml文件 里的内容乱打的字母
在这里插入图片描述

这已经是踩完坑的代码了,当时报了很多个错,忘了截图了,就记得有个 验证失败的错误,后来加了账号密码就好了。好了下面测试图,因为在win 已经做过映射,直接config-3344.com,如果没加,访问http://localhost:3344/master/config-text.yml
在这里插入图片描述
服务端自测通过
client端 3355
pom

dependencies>
		<!--client端 config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

bootstrap.properties配置,因为发生了很多事,后来解决了本身是yml的。又改成 properties。就没改回去
这里随便说一下,bootstrap 和 application,bootstrap优先级要高于application
spring cloud config要做的就是 共有的配置提取。从git获取,所以先配置必须得是共有的,所以用了系统级别的 bootstrap配置。

server.port=3355
spring.application.name=config-client
spring.cloud.config.label=master
#spring.cloud.config.name=config name 和 profile 这两个是会组合到一起 最终形成,config-text,自动会加上 “-” 
#spring.cloud.config.profile=text
spring.cloud.config.name=config
spring.cloud.config.profile=text
# 通过服务端 334去拿数据
spring.cloud.config.uri=http://localhost:3344
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka
management.endpoints.web.exposure.include=*

主启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class);
    }
}

controller

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {
    @Value("${config.info}") //这里的意思 下面会给截图,
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

@Value("${config.info}") 在springcloud-config仓库里有个键值对的方式,config info 简单一句话就是 读取 config下的info里面的内容
在这里插入图片描述
测试成功图,成功拿到数据,

在这里插入图片描述
但是这里是有问题的,如果仓库的数据更改,服务端3344是可以拿到最新的数据,但是3355需要重启,或者需要重新发一个post 请求刷新3355,才能拿到最新的数据。

前提已经安装 rabbitmq。并且通过,
config端口 3344 yml修改 添加如下

  rabbitmq:
     host: localhost
     port: 5672
     username: guest
     password: guest
  management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

client端 3355yml添加如下

  rabbitmq:
     host: localhost
     port: 5672
     username: guest
     password: guest

controller

package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope //刷新
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

测试启动 3344 3355 下面分别是 3344 3355通过截图
在这里插入图片描述
在这里插入图片描述
简单说一下ribbmq 解决的问题,只需要发送一个post请求,刷新3344,所有client都会被刷新,这里就测试一个 client 3355
cmd命令

curl -X POST "http://localhost:3344/actuator/bus-refresh"

在这里插入图片描述
测试成功
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值