SpringCloud五大组件Config

编程知识 行业动态 更新时间:2024-06-13 00:17:24

SpringCloud五大组件Config

SpringCloudConfig配置中心我们使用Bus来做刷新配置

简介:
Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Cloud 项目,通过简单的配置即可实现功能。

一、我们创建一个SpringBoot项目,服务名为:conifg-server,Pom配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache/POM/4.0.0 https://maven.apache/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>主Pom的GroupId</groupId>
        <artifactId>主Pom的ArtifactId</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.*.*</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lezhuboot-cloud-config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、ConfigServerApplication类加上注解@EnableConfigServer,开启Config的功能,代码如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class LeZhuBootCloudConfigServerApplication {

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

三、在Gitee上创建一个仓库,然后创建一个文件起名为:bootstrap-member.yml,然后我们在文件中写上:

test: 
 testName: zhangsan123456

四、在Config服务Yml中添加配置,配置如下:

eureka:
  instance:
    #开启IP注册
    prefer-ip-address: true
    #地址名称
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    #心跳
    lease-renewal-interval-in-seconds: 5
    #无心跳,十秒踢出
    lease-expiration-duration-in-seconds: 10
    #获取此实例的相对健康检查URL路径。 健康检查页面URL,然后构造出主机名和通信的类型 - 安全或不安全,如securePort和nonSecurePort规定。 它通常用于制造基于实例的健康做出明智的决策 - 例如,它可以被用来确定是否进行部署到整个服务器场或停止部署,而不会造成进一步的损害。
    health-check-url-path: /actuator/health
  client:
    #表示的频率(以秒为单位)来从eureka服务器注册表的信息
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://127.0.0.1:28001}/eureka/
server:
  port: 28101
spring:
  application:
    name: config-server
  rabbitmq:
    host: xxxxx
    password: xxxx
    port: xxxx
    username: xxxx
  cloud:
    config:
      label: master
      server:
        git:
          password: xxxxx
          search-paths: /conifg
          uri: https://gitee/xxxx/configuration_file.git
          username: xxxxxx
          force-pull: true #设置强行pull拉取
#关闭安全认证
management:
  security:
    enabled: false
    #refresh接入点显式暴露出来
  endpoints:  # 暴露bus 接口 ,否则 更新 refresh 没用的
    web:
      exposure:    # expose: "*" 已过期
        include: "*"
  endpoint:
    bus-refresh:
      enabled: true
    health:
      #当显示完整的健康细节。
      show-details: ALWAYS


五、改造以前的Member项目,Pom配置添加如下:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

六、在Member服务Yml中添加,配置如下:

server:
  port: 28667
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
    health-check-url-path: /actuator/health
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://127.0.0.1:28001}/eureka/
spring:
  application:
    name: member
  rabbitmq:
    host: xxxxxx
    password: xxxxx
    port: xxxxx
    username: xxxx
  cloud:
    config:
      name: bootstrap-member               #对应{application}部分
      profile: master                      #对应{profile}部分
      uri: xxxxx   #配置中心的具体地址
      label: master                        #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
      discovery:
        service-id: config-server    #指定配置中心的service-id,便于扩展为高可用配置集群。
        enabled: true                      #开启Config服务发现支持
      fail-fast: true
      #配置重试机制
      retry:
        initial-interval: 2000
        max-attempts: 2000
        max-interval: 2000
        multiplier: 1.2
    bus:
      #动态刷新配置
      refresh:
        enabled: true
      #跟踪总线事件
      trace:
        enabled: true
      bus:
        id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value}

# 暴露监控点
management:
  endpoints:
    web:
      exposure:
        include: "*"

七、我们在Member服务中改造控制器,代码如下:

@RestController
@RequestMapping("/member")
@RefreshScope
public class MemberController {

    @Value("${test.testName}")
    private String testName;

    @GetMapping("/sayHiMember")
    public String sayHiMember(String name) {
        return "SayHi会员:" + name + ":" + ":从Git上取文件配置:" + testName;
    }
}

八、我们自己安装一个Rabbitmq,如何安装就不在演示,请自行查询。

九、我们启动服务,顺序是先启动注册中心,在启动config,再去启动zuul、user和member服务

十、启动完成,我们通过网关去访问User服务并且内部永Feign调用member服务,member服务里面从gitee上取配置文件,在网页中输入:http://127.0.0.1:28580/user/user/userInfo?name=zhangsan,演示结果如下:

圈红框的地方,就是从gitee上取的配置结果

十一、我们去Gitee上去修改配置文件,修改图如下:

把zhangsan123456改成zhangsan


十二、我们用Post请求去调用http://localhost:28101/actuator/bus-refresh,进行配置文件刷新,刷新完成后重新调用http://127.0.0.1:28580/user/user/userInfo?name=zhangsan,结果如下:

SpringCloud五大组件Config配置中心告一段落,后续讲解如何不去调用/actuator/bus-refresh接口去实现Git自动推送刷新配置

更多推荐

SpringCloud五大组件Config

本文发布于:2023-03-25 05:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/7bcfa389a566405d2fab1e7af06050fe.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:五大   组件   SpringCloud   Config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!