springcloud-config配置中心高可用

config server

在config-server项目基础上修改

  1. 添加依赖
<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>
  1. 修改application.yml
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/
  1. 修改启动类
@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. 添加依赖
    <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>
  1. 修改bootstrap.yml
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
  1. 修改application.yml
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/

  1. 修改启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClient2Application {

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

admin

项目路径


作者博客

作者公众号
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值