springcloud config使用的小demo

springcloud config的简单实例

因为案例中有服务端和客户端多个服务,所以先创建一个父项目,其他服务为子项目
父项目只需引入springboot依赖即可

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/>
    </parent>

一 服务注册与发现-eureka

eureka是springcloud的五大组件之一,先创建eureka服务,注意该项目为子项目,路径在父项目之后,如图
eureka

依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
```java

如图
在这里插入图片描述

配置文件

添加application.yml配置文件

server:
  port: 8081
spring:
  application:
    name: eureka-server

#eureka的配置项有3:
#client,instance,server
eureka:
  instance:
    hostname: localhsot
  client:
    service-url:
      defaultZone:http://${eureka:instance:hostname}:${server:port}/eureka/:
    register-with-eureka: false
    fetch-registry: false
  server:
    enable-self-preservation: true #线上阶段应该打开自我保护机制,避免因为网络抖动(网络分区故障)而产生的服务剔除
    renewal-percent-threshold: 0.85 #服务续约的比例,服务列表
    eviction-interval-timer-in-ms: 5000

创建启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

eureka项目架构如图:
在这里插入图片描述
点击启动类的运行按钮,开始运行
在这里插入图片描述
启动成功
在这里插入图片描述
可以在浏览器输入localhost:8081查看eureka页面
在这里插入图片描述

springCloud config服务端

创建config子项目

依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>
</dependencies>

创建配置文件

server:
  port: 8082
  tomcat:
    uri-encoding: utf-8
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/qfp17393120407/spring-cloud-config.git #gitee上的仓库名称,此处为http方式
          #搜索目录
          search-paths:
            - springcloud-config
          username: 173****0407
          password: 123455
      #读取的分支
      label: master
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka

注意:springcloud config配置中心是为了统一管理配置文件,所以需要将配置放到github仓库或gitee,我使用的是gitee,github经常连不上,不推荐。(没号的可以在gitee上建一个账号,网址gitee.com

gitee创建配置文件

在gitee上创建一个新的仓库
在这里插入图片描述
然后在创建一个配置文件
在这里插入图片描述
注意:填写代码中的gitee的配置时,url为gitee仓库中点击“克隆/下载”按钮—HTTP----复制后的内容,用户名和密码为gitee的用户名和密码,如图
在这里插入图片描述
最后启动服务,在浏览器输入localhost:8082/master/config-dev.yml,读取到配置信息。
在这里插入图片描述

spring Cloud config客户端

创建子项目后添加依赖

依赖

<dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.2.3.RELEASE</version>
        </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>
    </dependencies>

配置文件

创建名为bootstrap.yml的配置文件

server:
  port: 8083
spring:
  application:
    name: config-client
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称 上述三个综合:master分支上config-dev.yml的配置文件被读取http://localhost:8082/master/config-dev.yml
      uri: http://localhost:8082
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类

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

读取gitee中的配置

在springcloud config的客户端中读取gitee中的配置
创建controller

@RestController
@RefreshScope
public class ClientController {

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

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

启动服务后访问http://localhost:8083/configInfo
在这里插入图片描述
读取成功!
配置中心是为了能够修改配置后无需重启服务,动态刷新。修改gitee的config-dev.yml文件后,需要需要发送POST请求:"http://localhost:8083/actuator/refresh,可以通过postman软件发送
请求,如图
在这里插入图片描述
发送成功后再次访问http://localhost:8083/configInfo,即可发现显示的是已更新内容。

参考博客:SpringCloud Config介绍与使用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值