springcloud4 配置中心(码云或者github)

1.新建一个配置中心模块

备注:远程仓库的配置代码,和客户端或者服务端的基本一样。

1.1 主方法

@SpringBootApplication
@EnableEurekaClient //加入注册中心
@EnableConfigServer //启用配置服务端
public class UserProviderApplication_6666 {
    public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication_6666.class, args);
    }
}

1.2 application配置

server:
    #配置中心的端口号
  port: 6666 
eureka:
  client:
    service-url:
      defaultZone:  http://peer1000:1000/eureka/,http://peer1001:1001/eureka/
  #可以使用ip注册
  instance:
    prefer-ip-address: true
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          #是在仓库的配置中心地址
          uri: https://gitee.com/ccjdk/springcloud-demo.git
          username: ......
          password: ......

1.3 pom

<!--springboot支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

<!--eureka客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

 

 

2.客户端(配置文件名字是bootstrap.yml,优先级高于application)

2.1 基于springcloud3中的 客户端,

在pom中追加上

<!--开启加载配置中心-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2.2 bootstrap.yml配置

spring:
  cloud:
    config:
      #name+profile的总名字是在码云或者github上的名字application-getting-test
      name: application-getting #github上面名称
      profile: test #环境
      #master是在仓库上的分支名字,是默认的一个
      label: master #分支
      uri: http://localhost:6666 #配置中心服务器        
      #当集群的时候,也可以用名字替代配置中心服务器的uri
        discovery:
       #  enabled: true
       #  service-id: CONFIG-SERVER   #配置中心在注册中心的名字
eureka:
  client:
    service-url:
      defaultZone:  http://peer1000:1000/eureka/,http://peer1001:1001/eureka/
  instance:
    prefer-ip-address: true #显示客户端真实ip

2.3 运行主方法,端口号就变成了远程仓库中的端口号

转载于:https://my.oschina.net/u/4083694/blog/3079156

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值