springcloud-config总结

一:springcloud-config
1,什么是SpringCloud Config?
在这里插入图片描述
2,SpringCloud Config 架构图
在这里插入图片描述
3,开发config-server

		引入依赖
		//1.引入eurekaclient和config-server的依赖
	<dependency>
	   <groupId>org.springframework.cloud</groupId>
	   <artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<dependency>
	   <groupId>org.springframework.cloud</groupId>
	   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>

4,配置服务

	server:
	  port: 9999
	spring:
	  application:
	    name: config-server

5,启动类添加注解

@EnableConfigServer

注意,还需要配置远程仓库地址,否则启动会报以下错误
启动会报错:
Invocation of init method failed; 
nested exception is java.lang.IllegalStateException: 
You need to configure a uri for the git repository

6,远程仓库编写配置文件(或者上传配置文件)

7,config-server,配置远程仓库

8,启动config-server,访问远程的配置文件信息

启动工程,访问远程文件
http://localhost:9999/application-a.yml
文件的命名格式要求是*-*的格式,当没有匹配的文件时,则会找默认的配	置文件application.yml

9,将config-server作为服务注册到注册中心

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9090/eureka/,http://localhost:9091/eureka/

10,配置中心的高可用问题,我们只需要发布多个实例即可,就像上面那样以服务的方式注册到注册中心即可

二:springcloud-client
目标:
修改product-service,让其去读取config-server的配置信息

实现步骤:
1,引入依赖

	//1.引入依赖 config-client
	<dependency>
	   <groupId>org.springframework.cloud</groupId>
	   <artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
	   <groupId>org.springframework.cloud</groupId>
	   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>

2,启动类注解,不需要做改变

		@EnableEurekaClient

3,修改配置

spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      profile: default
  application:
    name: product-service

//profile: default 表示读取application.yml

4,项目中引用公共的配置参数

@Value("${image.server}")
private String imageServer;

@RequestMapping("getImageServer")
public String getImageServer(){
    return imageServer;
}

5,启动项目,观察结果;出错

需要将本地的配置文件改名为:bootstrap.yml
引导上下文会在主应用上下文前创建,是主应用上下文的父上下文
引导上下文在创建时会去读取远程配置(GitHub)
就是让文件先读取,再去初始化image.server环境值


重新启动,可以访问到公共的配置
	 

问题:
	在git修改配置信息,config-service能获取到最新的消息,config-client需要重启;

三:引用springcloubus ;消息同步刷新

1,存在的问题

我们现在github上面的修改如果发生变更,我们的消费端需要重启才能感知到。
那么,为了达到无需重启,就可以感知到公共配置变更的效果
我们可以结合消息总线和MQ中间件来实现

2,给config-server项目,引入SpringCloud-bus依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

//添加RabbitMQ的配置
rabbitmq:
  host: 192.168.77.128
  virtual-host: /java1807
  username: java1807
  password: 123

3,product-service项目,也需要引入此依赖和配置

这个组件会自动给config-server创建队列和product-service(config-client)创建队列

4,调用bus-refresh接口,同步刷新配置信息,config-server需要配置开放接口

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

5,product-service(config-client)要能够刷新变化
需要加上注解

@RestController
@RefreshScope
@RequestMapping("product")
public class ProductController {

    @Value("${image.server}")
    private String imageServer;

    @RequestMapping("getImageServer")
    public String getImageServer(){
        return imageServer;
    }
}

6,当我们的配置发生变化,需要客户端能感知到,我们需要手工调用下刷新配置接口
注意,要以post方式发送该请求

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值