【Spring Cloud】学习(三)

接上文继续

注意!后续application.yml中的spring.application.name需使用驼峰命名或其他,名称中不可存在符号,如provider-user,需改成providerUser,不然后面讲到的Config组件中,git中的配置文件命名也会带-,然后就会出现无法自动加载配置的bug。

5、Config配置中心

5.1 配置Config Server
本次demo是在Windows上运行的,后续需要RabbitMQ来自动加载配置,所以Windows上要预先安装RMQ,参看我的博客
依赖如下
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- 使用bus总线的方式通知所有的微服务配置文件需要刷新 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

		<!-- Spring Cloud Config Server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

		<!-- 配置中心监控 -->
		<!-- 在远端git仓库修改配置后需要触发远端git的webhook,
		请求本地的/monitor接口来触发修改,所以需要引入spring-cloud-config-monitor依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-monitor</artifactId>
		</dependency>
		
		<!-- Spring Cloud Eureka Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
bootstrap.yml
解释下这里为什么要改名为bootstrap,这里涉及到一个先后顺序,Spring Boot会先加载bootstrap.yml然后再加载application.yml。
server:
  port: 6001

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/BigYoungZhao/spring-cloud-config.git
          username: 你的码云账号
          password: 你的码云密码
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  application:
    name: configServer6001
management:
  endpoints:
    web:
      exposure:
        include: "*"
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServer6001Application {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServer6001Application.class, args);
	}
}
检查是否从git获取到配置文件

在这里插入图片描述

启动Config Server项目,访问http://localhost:6001/user-dev.yml

在这里插入图片描述

检查monitor 接口是否可用,访问http://localhost:6001/monitor

在这里插入图片描述

为什么是访问这个接口呢?来看看源码

在这里插入图片描述

spring-cloud-config-monitor包下,该包定义了目前支持通知提醒的方式。常用的支持gitee 、github 、gitlab这三种git的配置 。notifyByPath,顾名思义,该接口表示根据路径通知服务的方法
@RequestMapping(method = RequestMethod.POST)
	public Set<String> notifyByPath(@RequestHeader HttpHeaders headers,
			@RequestBody Map<String, Object> request) {
		PropertyPathNotification notification = this.extractor.extract(headers, request);
		if (notification != null) {

			Set<String> services = new LinkedHashSet<>();

			for (String path : notification.getPaths()) {
				services.addAll(guessServiceName(path));
			}
			if (this.applicationEventPublisher != null) {
				for (String service : services) {
					log.info("Refresh for: " + service);
					this.applicationEventPublisher
							.publishEvent(new RefreshRemoteApplicationEvent(this,
									this.busId, service));
				}
				return services;
			}

		}
		return Collections.emptySet();
	}

5.2 配置micro-service(需要获取配置的对象服务)
改造上一篇的一个微服务
依赖如下
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- 使用bus总线的方式通知所有的微服务配置文件需要刷新 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

		<!-- Spring Cloud Config Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>

		<!-- Spring Cloud Eureka Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

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

	</dependencies>
application.yml
server:
  port: 8001
bootstrap.yml
spring:
  cloud:
    config:
      name: user #对应git上的user-dev.yml中的user
      profile: dev #对应git上的user-dev.yml中的dev
      discovery:
        service-id: configServer6001 #Config Server中bootstrap.yml的spring.application.name
      uri: http://localhost:6001 #Config Server的访问路径,不写这个直接报错:Cannot determine embedded database driver class for database type NONE
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderUser8001Application {

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

写个controller测试下
@RestController
@RefreshScope //刷新范围,必要,注意
public class UserController {

    @Value("${spring.datasource.url}")
    private String datasourceUrl;

    @GetMapping("/getDatasourceUrl")
    public String getDatasourceUrl() {
        return this.datasourceUrl;
    }

}
按顺序启动Eureka、Config Server、微服务,访问http://localhost:8001/getDatasourceUrl

在这里插入图片描述

当我把user-dev.yml里的数据库改为test_prod时,再次去访问http://localhost:8001/getDatasourceUrl,还是上图的结果。
一旦修改,配置中心的数据是刷新了,而微服务的配置数据只是在请求时获取到,之后不会主动去重新拉取新的配置数据。只能靠 POST请求 http://localhost:6001/actuator/bus-refresh进行手动刷新。这个主要是通过我们配置的RabbitMQ,将刷新数据的消息放到MQ,并被微服务消费,从而刷新。
那么该如何自动刷新呢?
webhook机制,相当于一个回调,在我们push时,调用这个接口http://公网ip或域名/monitor

在这里插入图片描述

注意:由于我们的工程在本机,外网无法访问到你的服务,所以搞个内网穿透的工具(natapp)。然后修改本地yml文件再push上去,再调用上面微服务的接口即可看到自动刷新的效果。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值