暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

springcloud-config配置中心高可用

dean技术分享 2019-12-16
147

config server

在config-server项目基础上修改

  1. 添加依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <dependencies>
    <!-- config server依赖 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- eureka-client依赖 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    </dependencies>
  2. 修改application.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    server:
    port: 8888

    spring:
    application:
    name: config-server
    cloud:
    config:
    server:
    git:
    #git仓库地址
    uri: https://github.com/dean4lee/springcloud-demo
    #仓库地址下的路径,多个用,隔开
    search-paths: conf
    #用户名
    username:
    #密码
    password:

    eureka:
    client:
    #是否将自己注册到Eureka服务中
    register-with-eureka: true
    #是否从Eureka服务中获取注册信息
    fetch-registry: false
    #Eureka注册中心的地址,多个注册中心用,隔开
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/
  3. 修改启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @SpringBootApplication
    //将config-server服务注册到注册中心
    @EnableDiscoveryClient
    @EnableConfigServer
    public class ConfigServer2Application {

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

先启动eureka-server服务,再启动config-server服务


config client

在config-client项目基础上修改

  1. 添加依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    </dependencies>
  2. 修改bootstrap.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    spring:
    cloud:
    config:
    #配置服务器地址
    #uri: http://localhost:8888
    #对应{application}
    name: hello
    #对应{profile}
    profile: dev
    #git分支
    label: master
    #如果有密码验证
    username:
    password:
    discovery:
    #开启config配置中心发现
    enabled: true
    #config配置中心的service-id,即服务名
    service-id: config-server
  3. 修改application.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    server:
    port: 8880

    spring:

    application:
    name: config-client

    eureka:

    client:
    #是否将自己注册到Eureka服务中
    register-with-eureka: true
    #是否从Eureka服务中获取注册信息
    fetch-registry: true
    #Eureka注册中心的地址,多个注册中心用,隔开
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/
  4. 修改启动类

    1
    2
    3
    4
    5
    6
    7
    8
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ConfigClient2Application {

    public static void main(String[] args) {
    SpringApplication.run(ConfigClient2Application.class, args);
    }
    }
  5. 测试
    浏览器访问http://localhost:8880/getUsername

    admin


阅读原文有项目链接

文章转载自dean技术分享,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论