SpringCloud深入学习(十一)——Config配置中心高可用配置和使用

一、简介

实际开发中,我们必须要保证整个项目服务的高可用。

一个微服务宕机,并不影响其他服务器的业务逻辑。

为了实现高可用,我们必须将单个的微服务做集群,做服务注册与发现配置,保证某个服务宕机但不影响其他流程。

二、高可用的搭建

2.1、开发前的准备工作

  • 搭建注册中心 10000
  • config server 注册至注册中心
  • config client 注册至注册中心

此处依旧使用之前的注册中心 Eureka-server-10000、SpringCloud-Config-Client-bus-security-4000、SpringCloud-Config-Server-bus-security-3000。

2.2、 config server的修改和配置

注册至注册中心上,需要引入下列依赖:

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

在配置文件中,增加注册至注册中心的地址指向:

####服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:10000/eureka/

修改启动类注解信息,表明这是一个eureka client。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer  //表明这是一个  config server
public class SpringCloudConfigApplication3000 {
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigApplication3000.class, args);
	}
}

启动Eureka-Server和SpringCloud-Config-Client-bus-security-4000,完成服务的注册与发现。

2.3、config client的修改和配置

同上,在依赖文件中增加eureka client的依赖信息、在配置类中增加注册中心的指向配置信息和增加启动类的注解信息,依次如下所示:

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

bootstrap.yml

## config client 相关配置
###服务名称(服务注册到eureka名称)  
spring:
  application:
    name: config-client  ## 对应 config server 所获取到的配置文件的{application}
  cloud: 
    config:
      #uri: http://localhost:3000/   ## config server 的地址
      username: xiangjiao  ## 安全认证账号
      password: bunana  ## 安全认证密码
      profile: dev  ## profile 对应config server 所获取到的配置文件中的 {profile} 
      label: master  ## 指定的git 仓库的分支,对应 config server 中的{label} 
      discovery:
        enabled: true  # 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认false
        service-id: springcloud-config-service-bus  # 指定Config Server在服务发现中的serviceId,默认是configserver
      
  ## 配置rabbitmq的连接属性
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: xiangjiao
    password: bunana
    virtual-host: /springcloud
    
###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:10000/eureka/

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

启动并注册至注册中心中,如下所示:
在这里插入图片描述

三、测试

请求 config server 查看指定github上的配置文件:

http://localhost:3000/config-client/dev
在这里插入图片描述

请求 config client ,查看能否从 config server 上获取指定信息:

http://localhost:4000/test
在这里插入图片描述

修改github上的指定文件信息,测试刷新:

在这里插入图片描述
重新请求 config server:
http://localhost:3000/config-client/dev
在这里插入图片描述

此时请求 config client 数据结果一定还是以前的,并未及时的做更新处理:

http://localhost:4000/test
在这里插入图片描述

如何刷新呢?

四、数据的刷新

  • client 端的刷新
  • server 端采取 bus刷新

修改后重新请求 config server接口数据,发现变更;
但我们再次请求 config client 接口,数据依旧未变

先看 client 端的刷新,我们直接使用的接口为:

POST http://localhost:4000/actuator/refresh
在这里插入图片描述
再次请求 config client端:
http://localhost:4000/test
在这里插入图片描述

但是这里我遇到了个问题:

使用server中配置了bus,注册至注册中心后,
http://localhost:3001/actuator/bus-refresh post
请求有效,但数据却并未发生修改!

猜你喜欢

转载自blog.csdn.net/qq_38322527/article/details/105657997