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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Cloud 远程配置可以使用 GitHub 存储配置文件,以便在不同的环境中使用相同的配置。以下是如何在 Spring Boot Cloud 中使用 GitHub 存储配置文件的步骤: 1. 在 GitHub 上创建一个仓库,并在仓库中创建一个名为 `application.yml` 的配置文件,内容如下: ``` server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: myuser password: mypassword ``` 2. 在 Spring Boot 项目的 `src/main/resources` 目录中创建一个名为 `bootstrap.yml` 的配置文件,内容如下: ``` spring: cloud: config: uri: https://github.com/your-github-username/your-github-repo/ username: your-github-username password: your-github-password profile: dev ``` 其中,`uri` 指定了 GitHub 仓库的 URL,`username` 和 `password` 是 GitHub 的用户名和密码,`profile` 指定了要使用的配置文件的环境。 3. 在 Spring Boot 项目中添加以下 Maven 依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> ``` 4. 在 Spring Boot 项目的启动类上添加 `@EnableConfigServer` 注解: ``` @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 5. 启动 Spring Boot 项目,访问 `http://localhost:8080/application/dev` 可以看到 GitHub 上的配置文件已被加载。 以上就是使用 GitHub 存储配置文件的 Spring Boot Cloud 远程配置的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值