SpringCloud五大组件Config

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.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/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.com/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自动推送刷新配置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值