SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码

网友投稿 222 2023-02-09


SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码

一、简介

当要将配置中心部署到生产环境中时,与服务注册中心一样,我们也希望它是一个高可用的应用。Spring Cloud Config实现服务端的高可用非常简单,主要有以下两种方式。

传统模式:不需要为这些服务端做任何额外的配置,只需要遵守一个配置规则,将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护。而客户端在指定Config Server位置时,只需要配置Config Server上层的负载均衡设备地址即可, 就如下图所示的结构。

服务模式:除了上面这种传统的实现模式之外,我们也可以将Config Server作为一个普通的微服务应用,纳入Eureka的服务治理体系中。这样我们的微服务应用就可以通过配置中心的服务名来获取配置信息,这种方式比起传统的实现模式来说更加有利于维护,因为对于服务端的负载均衡配置和客户端的配置中心指定都通过服务治理机制一并解决了,既实现了高可用,也实现了自维护。由于这部分的实现需要客户端的配合,具体示例读者可详细阅读 “客户端详解 ”一节中的 “服务化配置中心” 小节。

二、前期准备

一个服务注册中心,EUREKASERVER,端口为5555;

三、改造Config-Server

(1)pom.xml,添加spring-cloud-starter-eureka依赖

org.springframework.cloud

spring-cloud-config-server

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.boot

spring-boot-starter-test

test

(2)application.yml,配置参数eureka.client.serviceUrl.defaultZone以指定服务注册中心的位置

server:

port: 5588

spring:

application:

name: config-server

eureka:

client:

serviceUrl:

defaultZone: http://localhost:5555/eureka/ #配置服务注册中心

cloud:

config:

server:

git:

uri: https://gitee.com/smartdt/springcloudconfig.git #配置Git仓库位置。

searchPaths: config-repo #配置仓库路径下的相对搜索位置,可以配置多个。

username: username #访问 Git 仓库的用户名。

password: password #访问 Git 仓库的用户密码。

label: master #配置仓库的分支

###如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。

(3)入口类,新增@EnableDiscoveryC巨ent注解,用来将config-server注册到上面配置的服务注册中心上去。

@EnableDiscoveryClient

@EnableConfigServer

@SpringBootApplication

public class SpringcloudconfigserverApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudconfigserverApplication.class, args);

}

}

(4)启动config-server,通过Eureka-Server查看

四、改造Config-Client

(1)pom.xml,添加spring-cloud-starter-eureka依赖

org.springframework.cloud

spring-cloud-starter-config

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.boot

spring-boot-starter-test

test

(2)bootstrap.properties,添加配置服务中心信息

spring.application.name=configspace

spring.cloud.config.label=master

spring.cloud.config.profile=dev

spring.cloud.config.uri= http://localhost:5588/

server.port=5589

eureka.client.serviceUrl.defaultZone=http://localhost:5555/eureka/

(3)入口类,添加@EnableDiscoveryClient

@EnableDiscoveryClient

@SpringBootApplication

public class SpringcloudconfigclientApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudconfigclientApplication.class, args);

}

}

(4)测试类不变

@RefreshScope

@RestController

public class ConfigController {

@Value("${from}")

private String from;

@Value("${username}")

private String username;

@Value("${password}")

private String password;

@RequestMapping("/from")

public String from() {

return this.from + "~user:" + this.username + "~pass:" + this.password;

}

}

(5)启动测试,通过Eureka-Server查看

(6)浏览器测试,访问http://localhost:5589/from


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot整合Spring Security的示例代码
下一篇:mac连接共享文件夹错误(mac 网络共享文件夹)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~