微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringCloud组件--服务配置--SpringConfig

1. SpringConfig服务端

1.1 如何使用?

1.导入依赖

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

2.主启动类

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

3.配置yml

spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: [email protected]:Wang-QingLong/Springcloud-config.git
          search-paths:  #搜索路径,也就是你的仓库名字
            - Springcloud-config
      label: master   #分支

浏览器可以访问: http://localhost:3344/master/config-prod.yml

在这里插入图片描述

1.2 格式

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

2. SpringConfig客户端

2.1 第一步导入依赖

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

2.2 第二步,主启动类上

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

2.3 第三步,客户端需要使用 bootstrap.yml

在这里插入图片描述

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config: #客户端配置
      label: master   #分支名称
      name: config    #配置文件名称
      profile: dev    #读取后缀名称
      #上述综合: http://localhost:3344/master/config-dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://admin:[email protected]:7001/eureka/

2.4 测试

@RestController
public class ConfigClientController {

    @Value("${config.info}")   // config.info 为github上的
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

在这里插入图片描述


在这里插入图片描述


理论: 3355不会作为config的客户端不会去找github,而是去找服务端,通过服务端连接github

在这里插入图片描述

3. bootstrap.yml是什么

在这里插入图片描述

4. 手动刷新github配置

1.当修改了github上的数据时,config的服务端,比如3344自动会刷新的,因为他直连github,
但是客户端不会,他需要手动刷新,因为需要注意以下几点:

1.依赖当中必须要有,说白了以后除了gateway网关,其他都给他加上

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

2.控制层需要加上刷新配置注解 @RefreshScope

@RefreshScope
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

3.bootstrap.yml 添加暴露接口

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config: #客户端配置
      label: master   #分支名称
      name: config    #配置文件名称
      profile: dev    #读取后缀名称
      #上述综合: http://localhost:3344/master/config-dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://admin:[email protected]:7001/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"

在这里插入图片描述


必须手动发送Post请求刷新: curl -X POST "http://localhost:3355/actuator/refresh"

在这里插入图片描述


然后再访问 就可以看到最新数据:

在这里插入图片描述

5. 额外话题: curl的使用

curl(CommandLine Uniform Resource Locator),即在命令行中利用URL进行数据或者文件传输。
https://curl.haxx.se/

这是curl的官网。可以从上面的官网地址下载最新的curl版本。同时可以在官网看出curl支持的各种协议(如HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S等)

通过curl -h我们可以获取到所有curl的命令以及其用法描述。
常用的几种命令:

1. curl url 获取该url的网址的文本信息

在这里插入图片描述

2.curl -i url 获取该网址的文本信息以及协议头部信息

在这里插入图片描述


3. curl -v www.baidu.com 显示一次HTTP请求的通信过程

4.Curl执行GET/POST/PUT/DELETE操作 hou -X后跟指定的命令参数去执行
curl -X PUT www.baidu.com
curl -X DELETE www.baidu.com
curl -X POST www.baidu.com
curl -X GET www.baidu.com

比如:post请求
curl -X POST --header “Content-Type:application/json” --data ‘{}’ 127.0.0.1:8088/user/getAllUserInfo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐