微服务SpringCloud之Spring Cloud Config配置中心服务化

在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文件信息。如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。

一、Server端改造

1.添加依赖

在SpringCloudConfigServer项目中,添加spring-cloud-starter-netflix-eureka-client引用。

微服务SpringCloud之Spring Cloud Config配置中心服务化微服务SpringCloud之Spring Cloud Config配置中心服务化
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>

View Code

微服务SpringCloud之Spring Cloud Config配置中心服务化微服务SpringCloud之Spring Cloud Config配置中心服务化
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.6.RELEASEversion>
        <relativePath/>
    parent>
    <groupId>com.examplegroupId>
    <artifactId>SpringCloudConfigServerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <name>SpringCloudConfigServername>
    <description>Demo project for Spring Bootdescription>
<span>&lt;</span><span>properties</span><span>&gt;</span>
    <span>&lt;</span><span>java.version</span><span>&gt;</span>1.8<span><span>java.version</span><span>&gt;</span>
    <span>&lt;</span><span>spring-cloud.version</span><span>&gt;</span>Greenwich.SR2<span><span>spring-cloud.version</span><span>&gt;</span>
<span><span>properties</span><span>&gt;</span>

<span>&lt;</span><span>dependencies</span><span>&gt;</span>
    <span>&lt;</span><span>dependency</span><span>&gt;</span>
        <span>&lt;</span><span>groupId</span><span>&gt;</span>org.springframework.boot<span><span>groupId</span><span>&gt;</span>
        <span>&lt;</span><span>artifactId</span><span>&gt;</span>spring-boot-starter-web<span><span>artifactId</span><span>&gt;</span>
    <span><span>dependency</span><span>&gt;</span>
    <span>&lt;</span><span>dependency</span><span>&gt;</span>
        <span>&lt;</span><span>groupId</span><span>&gt;</span>org.springframework.cloud<span><span>groupId</span><span>&gt;</span>
        <span>&lt;</span><span>artifactId</span><span>&gt;</span>spring-cloud-config-server<span><span>artifactId</span><span>&gt;</span>
    <span><span>dependency</span><span>&gt;</span>

<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>

<span>&lt;</span><span>dependencyManagement</span><span>&gt;</span>
    <span>&lt;</span><span>dependencies</span><span>&gt;</span>
        <span>&lt;</span><span>dependency</span><span>&gt;</span>
            <span>&lt;</span><span>groupId</span><span>&gt;</span>org.springframework.cloud<span><span>groupId</span><span>&gt;</span>
            <span>&lt;</span><span>artifactId</span><span>&gt;</span>spring-cloud-dependencies<span><span>artifactId</span><span>&gt;</span>
            <span>&lt;</span><span>version</span><span>&gt;</span>${spring-cloud.version}<span><span>version</span><span>&gt;</span>
            <span>&lt;</span><span>type</span><span>&gt;</span>pom<span><span>type</span><span>&gt;</span>
            <span>&lt;</span><span>scope</span><span>&gt;</span>import<span><span>scope</span><span>&gt;</span>
        <span><span>dependency</span><span>&gt;</span>
    <span><span>dependencies</span><span>&gt;</span>
<span><span>dependencyManagement</span><span>&gt;</span>

<span>&lt;</span><span>build</span><span>&gt;</span>
    <span>&lt;</span><span>plugins</span><span>&gt;</span>
        <span>&lt;</span><span>plugin</span><span>&gt;</span>
            <span>&lt;</span><span>groupId</span><span>&gt;</span>org.springframework.boot<span><span>groupId</span><span>&gt;</span>
            <span>&lt;</span><span>artifactId</span><span>&gt;</span>spring-boot-maven-plugin<span><span>artifactId</span><span>&gt;</span>
        <span><span>plugin</span><span>&gt;</span>
    <span><span>plugins</span><span>&gt;</span>
<span><span>build</span><span>&gt;</span>

project>

View Code

2.增加了eureka注册中心的配置

在application.properties中增加eureka注册中心的配置,http://localhost:8088是后面启动的eureka server的地址

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

3.增加注解

在main方法中增加@EnableDiscoveryClient注解

二、客户端改造

1、添加依赖

也是引入spring-cloud-starter-netflix-eureka-client。

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>

微服务SpringCloud之Spring Cloud Config配置中心服务化微服务SpringCloud之Spring Cloud Config配置中心服务化
xml version="1.0" encoding="UTF-8"-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>?>cs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>
<://localhost:8002/hello](http://localhost:8002/hello),可以读取到配置文件的属性信息

![微服务SpringCloud之Spring Cloud Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>project lo](http://localhost:8002/hello),可以读取到配置文件的属性信息

![微服务SpringCloud之Spring Cloud Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>xmlnsst:8002/hello),可以读取到配置文件的属性信息

![微服务SpringCloud之Spring Cloud Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>="http://maven.apache.org/POM/4.0.0"d Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

> xmlns:xsi//johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>="http://www.w3.org/2001/XMLSchema-instance"0190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>
    xsi:schemaLocation8002/hello](http://localhost:8002/hello),可以读取到配置文件的属性信息

![微服务SpringCloud之Spring Cloud Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"ttps://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>>.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>
    <oud之Spring Cloud Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>modelVersion中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>>hngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>4.0.0modelVersion/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>>6/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>
    <ud之Spring Cloud Config配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>parentfig配置中心服务化](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>>://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/733213-20190825210552981-384559247.png)

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>
        <ouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>groupIdud/2017/05/25/springcloud-config-eureka.html

>>ingcloud-config-eureka.html

>org.springframework.bootgroupIdww.cnblogs.com/5ishare/p/11409371.html
>>share/p/11409371.html
>
        <uknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

>artifactId017/05/25/springcloud-config-eureka.html

>>loud-config-eureka.html

>spring-boot-starter-parentartifactIds.com/5ishare/p/11409371.html
>>1409371.html
>
        <://www.cnblogs.com/5ishare/p/11409371.html
>versionishare/p/11409371.html
>>.html
>2.1.6.RELEASEversion>
        <//www.cnblogs.com/5ishare/p/11409371.html
>relativePath/p/11409371.html
>/> Author: 社会主义接班人
>
    parent>
    <groupId>com.examplegroupId>
    <artifactId>SpringCloudConfigClientartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <name>SpringCloudConfigClientname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Greenwich.SR2spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

View Code

2.配置文件

在bootstrap.properties中去掉指定配置服务的地址url,增加了最后的三个配置:

spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server
eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/

spring.cloud.config.discovery.enabled :开启Config服务发现支持
spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
eureka.client.serviceUrl.defaultZone :指向注册中心的地址

3.启动类增加注解

在main方法中增加注解@EnableDiscoveryClient。

三、测试

1.分别启动eureka server、SpringCloudConfigServer、SpringCloudConfigClient。

2.浏览器输入http://localhost:8088/,可以看到注入到eureka server中的服务。

微服务SpringCloud之Spring Cloud Config配置中心服务化

3.输入http://localhost:8001/neo-config/dev,可以看到配置文件的配置信息。

微服务SpringCloud之Spring Cloud Config配置中心服务化

4.输入http://localhost:8002/hello,可以读取到配置文件的属性信息

微服务SpringCloud之Spring Cloud Config配置中心服务化

参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

Original: https://www.cnblogs.com/5ishare/p/11409371.html
Author: 社会主义接班人
Title: 微服务SpringCloud之Spring Cloud Config配置中心服务化

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/542046/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Paxos 协议简单介绍

    一、简介 Paxos 协议是少数在工程实践中证实的强一致性、高可用的去中心化分布式协议。Google 的很多大型分布式系统都采用了 Paxos 算法来解决分布式一致性问题,如 Ch…

    Java 2023年6月7日
    095
  • 第二周总结-Spring学习

    java;gutter:true;</p> <h2>Spring_day01</h2> <p><strong>今日目标&…

    Java 2023年6月7日
    089
  • springboot使用log4j2代替内置log4j

    前言 log4j是apache实现的一个开源日志组件 logback同样是由log4j的作者设计完成的,拥有更好的特性,用来取代log4j的一个日志框架,是slf4j的原生实现 l…

    Java 2023年5月30日
    078
  • Java源码赏析(六)Java String 三顾

    在大致了解了String之后,可能有的读者发现了,我们并没有谈到CharSequence接口。 原因是在这一节,CharSequence要和StringBuilder(Java1….

    Java 2023年6月8日
    064
  • [Java]Map接口有关面试题

    Map接口 1、HashMap和Hashtable的区别 线程安全方面。 HashMap是非线程安全的, Hashtable是线程安全的。因为 Hashtable内部方法基本都经过…

    Java 2023年6月5日
    0149
  • 稀疏数组详细讲解

    稀疏数组的应用场景 稀疏sparsearray数组 稀疏:从字面意思理解就是为了压缩重复冗余的数据 基本介绍: 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组…

    Java 2023年6月6日
    083
  • mysql 常用函数

    CONCAT(s1,s2) 例子:SELECT CONCAT (“SQL “,”Runoob “,”Gooogle &#…

    Java 2023年6月5日
    062
  • jvm

    双亲委派机制 什么是类加载器? 类加载器是jre的一部分,负责动态将类添加到Java虚拟机。 类加载分类 1、启动类加载器 bootstrap classloader :加载jre…

    Java 2023年6月13日
    077
  • Flutter水印

    如何给Flutter页面添加水印? 可以通过OverlayState实现 如下效果图: 具体实现源码 时刻怀有一颗虔诚之心,乐于分享。知识才更有意义。 posted @2020-0…

    Java 2023年5月29日
    093
  • 推荐几款最好用的MySQL开源客户端,建议收藏!

    一、摘要 众所周知,MYSQL 是目前使得最广泛、最流行的数据库技术之一,为了更方便的管理数据库,市场上出现了大量软件公司和个人开发者研发的客户端工具,比如我们所熟知的比较知名的客…

    Java 2023年6月9日
    088
  • 27.多线程,分组服务端,1万客户端连接,每秒处理200万包

    问题:网络抖动,不能稳定在每秒钟,200万个包。波动比较大,80万—-300万之间波动。 服务端: CELLTimestamp.hpp DataHeader.hpp E…

    Java 2023年5月29日
    076
  • Java使用HTTPS抓取网页实现

    如果网站不需要登录,直接抓取即可;如果网站需要登录,请参考上一篇文章:Java使用HTTPS登录网站代码实现,登录后,再抓取网页。 实现代码如下: /** * 抓取页面的子程序,返…

    Java 2023年5月29日
    0100
  • bat脚本一键配置java开发环境

    背景 在新电脑配置或者新人入职时需要对java开发相关环境进行配置安装,但时常会因为安装配置不到位或者操作错误导致时间的浪费,所以在空余时间收集了一系列软件的免安装版本,并且编写了…

    Java 2023年6月15日
    091
  • Springboot国际化信息(i18n)解析

    国际化信息理解 国际化信息也称为本地化信息 。 Java 通过 java.util.Locale 类来表示本地化对象,它通过 “语言类型” 和 &#8220…

    Java 2023年5月30日
    0115
  • Spring

    一、Spring SSH:Struct2 + Spring + Hibernate SSM:SpringMVC + Spring + MyBatis! 配置文件 org.sprin…

    Java 2023年6月8日
    085
  • BeanFactory与FactoryBean有什么区别?

    相同点:都是用来创建bean对象的 不同点:使用beanFactory创建对象的时候,必须要遵循严格的生命周期流程,太复杂了,如果想要简单的自定义某个对象的创建,同时创建好的对象想…

    Java 2023年6月8日
    059
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球