SpringCloud高可用配置中心Config详解(springcloudconfig作用)

网友投稿 291 2022-08-09


SpringCloud高可用配置中心Config详解(springcloudconfig作用)

目录前言源码环境开发工具正文commons 工程commons 工程 - POM 文件commons 工程 - 项目结构配置文件service 工程registry-service(注册中心)registry-service - POM 文件registry-service - application.yml配置文件registry-service - 启动类registry-server - 启动项目server(获取远程的配置信息)server- POM 文件server- application.yml配置文件server- 启动项目client(读取注册中心的server 中的配置信息)client- POM 文件client- application.yml配置文件client- bootstrap.yml 配置文件(注意)client- controller 前端控制器client- 启动类client- 启动项目service 工程 -项目结构把多工程项目使用 IntelliJ IDEA 打开

前言

SpringCloud 是微服务中的翘楚,最佳的落地方案。

Spring Cloud Config 是一个解决分布式系统的配置管理方案,它包含了 server 和 client 两个部分。

server 用来获取远程的配置信息(默认为 Git 仓库),并且以接口的形式提供出去;

client 根据 server 提供的接口读取配置文件,以便于初始化自己的应用。

如果配置中心出现了问题,将导致灾难性的后果,因此在生产环境下配置中心都会做集群,来保证高可用。

此处配置高可用实际就是把多个配置中心(指定同一个 Git 远程仓库)注册到注册中心。

源码

github地址:https://github.com/intomylife/SpringCloud

环境

JDK 1.8.0 +Maven 3.0 +SpringBoot 2.0.3SpringCloud Finchley.RELEASE

开发工具

IntelliJ IDEA

正文

commons 工程

commons 工程 - POM 文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-commons

1.0

springcloud-config-eureka-commons

公用工程

jar

UTF-8

1.8

Cairo-SR3

Finchley.RELEASE

io.spring.platform

platform-bom

${platform-bom.version}

pom

import

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud-dependencies.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-commons

1.0

springcloud-config-eureka-commons

公用工程

jar

UTF-8

1.8

Cairo-SR3

Finchley.RELEASE

io.spring.platform

platform-bom

${platform-bom.version}

pom

import

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud-dependencies.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

配置一些共用依赖

commons 工程 - 项目结构

配置文件

创建一个 Git 库,里面存放配置文件,文件夹名称为:config-repo;这里模拟不同的环境,所以分别构建了

dev(开发)、uat(测试)以及online(线上)三种不同的配置文件;此文件夹存在此项目的根目录中

- springcloud-config-eureka

? - config-repo

? ? -?system-dev.properties

? ? -?system-online.properties

? ? -?system-uat.properties

+ springcloud-config-eureka-commons

+ springcloud-config-eureka-service

service 工程

① 此工程下有四个模块:一个注册中心,二个 server 以及一个 client

② 二个 server 除端口不一致以外,其他代码基本一致

registry-service(注册中心)

registry-service - POM 文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-service

1.0

com.zwc

springcloud-config-eureka-registry-service

1.0

springcloud-config-eureka-registry-service

注册中心

jar

com.zwc

springcloud-config-eureka-commons

1.0

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-service

1.0

com.zwc

springcloud-config-eureka-registry-service

1.0

springcloud-config-eureka-registry-service

注册中心

jar

com.zwc

springcloud-config-eureka-commons

1.0

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-maven-plugin

主要加入spring-cloud-starter-netflix-eureka-server依赖

registry-service - application.yml配置文件

# 端口

server:

port: 8761

# 应用名称

spring:

application:

name: eureka-server

eureka:

instance:

# 使用 ip 代替实例名

prefer-ip-address: true

# 实例的主机名

hostname: ${spring.cloud.client.ip-address}

# 实例的 ID 规则

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

# 是否向注册中心注册自己

registerWithEureka: false

# 是否向注册中心获取注册信息

fetchRegistry: false

serviceUrl:

# 注册中心地址

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致

registry-service - 启动类

package com.zwc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

@EnableEurekaServer

public class SpringcloudConfigEurekaRegistryServiceApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudConfigEurekaRegistryServiceApplication.class, args);

}

}

在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心

registry-server - 启动项目

1. 项目启动成功后访问http://localhost:8761/即可看到eureka-server 主页面

server(获取远程的配置信息)

server- POM 文件

注:一共有两个 server,但是除端口以外的代码基本上一致,所有这里就列举其中一个

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-service

1.0

com.zwc

springcloud-config-eureka-serverfirst-service

1.0

springcloud-config-eureka-serverfirst-service

config server

jar

com.zwc

springcloud-config-eureka-commons

1.0

org.springframework.cloud

spring-cloud-config-server

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-service

1.0

com.zwc

springcloud-config-eureka-serverfirst-service

1.0

springcloud-config-eureka-serverfirst-service

config server

jar

com.zwc

springcloud-config-eureka-commons

1.0

org.springframework.cloud

spring-cloud-config-server

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-maven-plugin

加入spring-cloud-config-server依赖:config server加入spring-cloud-starter-netflix-eureka-client依赖:提供和注册服务

server- application.yml配置文件

# 端口

server:

port: 8000

spring:

application:

# 应用名称

name: config-eureka-server

cloud:

config:

server:

git:

# 仓库地址

uri: https://github.com/intomylife/SpringCloud

# 对应 {label} 部分,即 Git 的分支

label: master

# 仓库文件夹名称,多个以逗号分隔

search-paths: springcloud-config-eureka/config-repo

# git 仓库用户名(公开库可以不用填写)

username:

# git 仓库密码(公开库可以不用填写)

password:

eureka:

instance:

# 使用 ip 代替实例名

prefer-ip-address: true

# 实例的主机名

hostname: ${spring.cloud.client.ip-address}

# 实例的 ID 规则

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

serviceUrl:

# 注册中心地址

defaultZone: http://${eureka.instance.hostname}:8761/eureka/

注意这里 uri 只能写到仓库名如果配置文件在仓库中多层目录下,那么search-paths 就从根目录开始写起注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口

package com.zwc;

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

@EnableConfigServer

@EnableEurekaClient

public class SpringcloudConfigEurekaServerfirstServiceApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudConfigEurekaServerfirstServiceApplication.class, args);

}

}

添加@EnableConfigServer 注解表示开启配置中心添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务

server- 启动项目

1.项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

2. 访问地址:http://localhost:8000/config-repo/dev(获取完整配置信息)

3.输出内容:

{    "name":"config-repo",    "profiles":[        "dev"    ],    "label":null,    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",    "state":null,    "propertySources":[

    ]}

4. 访问地址:http://localhost:8000/system/dev(获取完整配置信息)

5. 输出内容:

{    "name":"system",    "profiles":[        "dev"    ],    "label":null,    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",    "state":null,    "propertySources":[        {            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-dev.properties",            "source":{                "hello":"I'm dev."            }        }    ]}

6. 访问地址:http://localhost:8000/system-dev.properties(获取指定配置文件内容)

7. 输出内容:‘hello: I’m dev.’

8. 启动另一个 server(springcloud-config-eureka-serversecond-service)

9.项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

10.访问地址:http://localhost:8001/config-repo/uat(获取完整配置信息)

11. 输出内容:

{    "name":"config-repo",    "profiles":[        "uat"    ],    "label":null,    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",    "state":null,    "propertySources":[

    ]}

12.访问地址:http://localhost:8001/system/uat(获取完整配置信息)

13.输出内容:

{    "name":"system",    "profiles":[        "uat"    ],    "label":null,    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",    "state":null,    "propertySources":[        {            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-uat.properties",            "source":{                "hello":"I'm uat."            }        }    ]}

14.访问地址:http://localhost:8001/system-uat.properties(获取指定配置文件内容)

15. 输出内容:‘hello: I’m uat.’

16. 证明两个 server 都已经成功从远程仓库中获取到了配置信息

注:接口访问有如下规则:

/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

client(读取注册中心的server 中的配置信息)

client- POM 文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-service

1.0

com.zwc

springcloud-config-eureka-client-service

1.0

springcloud-config-eureka-client-service

config client

jar

com.zwc

springcloud-config-eureka-commons

1.0

org.springframework.cloud

spring-cloud-starter-config

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zwc

springcloud-config-eureka-service

1.0

com.zwc

springcloud-config-eureka-client-service

1.0

springcloud-config-eureka-client-service

config client

jar

com.zwc

springcloud-config-eureka-commons

1.0

org.springframework.cloud

spring-cloud-starter-config

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-maven-plugin

加入spring-cloud-starter-config依赖:config client加入spring-cloud-starter-netflix-eureka-client依赖:提供和注册服务

client- application.yml配置文件

# 端口

server:

port: 8002

spring:

application:

# 应用名称

name: config-eureka-client

client- bootstrap.yml 配置文件(注意)

spring:

cloud:

config:

# 对应 {label} 部分,即 Git 的分支

label: master

# 对应 {application} 部分

name: system

# 对应 {profile} 部分

profile: dev

discovery:

# 开启 Config 服务发现与注册

enabled: true

# 指定 server

service-id: config-eureka-server

eureka:

instance:

# 使用 ip 代替实例名

prefer-ip-address: true

# 实例的主机名

hostname: ${spring.cloud.client.ip-address}

# 实例的 ID 规则

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

serviceUrl:

# 注册中心地址

defaultZone: http://${eureka.instance.hostname}:8761/eureka/

bootstrap.yml 配置文件加载先于application.yml 配置文件与 Spring Cloud Config 相关的属性必须配置在bootstrap.yml 中这里就不是直接指定 server 地址了,而是 server 的应用名(spring.application.name)从注册中心的 server 中读取 dev(开发)环境的配置文件信息注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口

client- controller 前端控制器

package com.zwc.client.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName HelloController

* @Desc TODO 读取 git 配置文件

* @Date 2019/6/2 16:54

* @Version 1.0

*/

@RestController

public class HelloController {

@Value("${hello}")

private String hello;

/*

* @ClassName HelloController

* @Desc TODO 读取 git 配置文件

* @Date 2019/6/2 16:56

* @Version 1.0

*/

@RequestMapping("/hello")

public String hello() {

return this.hello;

}

}

直接使用@Value 注解就可以从注册中心的server 中读取到对应的配置信息

client- 启动类

package com.zwc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

public class SpringcloudConfigEurekaClientServiceApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudConfigEurekaClientServiceApplication.class, args);

}

}

添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务

client- 启动项目

1.项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

2.访问地址:http://localhost:8002/hello

3. 输出内容:‘I’m dev.’

4. 证明 client 已经成功从注册中心的 server 中读取到了配置信息

5. 此时当 client 已经启动完毕后,配置文件就已经全部读取到了,所以即使停止其中一个 server 或者停止

全部 server,client 依然可以读取到配置文件。此处高可用应该是指在 client 启动时能保证有 server 可用

service 工程 -项目结构

把多工程项目使用 IntelliJ IDEA 打开

把项目从 GitHub 中下载到你的本地打开IntelliJ IDEA点击 File -> Open打开你下载到本地的项目目录springcloud-config-eureka ->springcloud-config-eureka-service(选择打开此工程)打开 service 工程后再次点击 File -> Project Structrue选择 Modules,点击 ‘+’ 符号点击 ImportModule还是打开你下载到本地的项目目录springcloud-config-eureka ->springcloud-config-eureka-commons -> pom.xml点击 OK点击 Next,Finish点击 Apply,OK

希望能够帮助到你


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

上一篇:Spring Security+JWT实现认证与授权的实现(spring festival是什么意思)
下一篇:java简明例举讲解泛型(深入理解java泛型详解)
相关文章

 发表评论

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