springcloud(十)

背景:基于springcloud(九)继续

分布式配置中心服务端

1. 创建spring-cloud-config-server工程,pom.xml文件依赖

<?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.0</modelVersion>

    <groupId>com.zsx</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-cloud-config-server</name>
    <description>Spring Cloud Config Server project</description>

    <parent>
        <groupId>com.zsx</groupId>
        <artifactId>spring-cloud-customize-dependencies</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../spring-cloud-customize-dependencies/pom.xml</relativePath>
    </parent>

    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.zsx.ConfigServerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2. application.yml配置文件

spring:
  application.name: spring-cloud-config-server
  cloud:
    config:
      # 配置仓库的分支
      label: master
      server:
        git:
          # 配置 Git 仓库地址
          uri: https://github.com/
          # 配置仓库路径(存放配置文件的目录)
          search-paths: config-repository
          # 访问 Git 仓库的账号
          username:
          # 访问 Git 仓库的密码
          password:
server:
  port: 8088
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/

3. 引导类

package com.zsx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {

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

4. 提交config-client-dev.yml文件到git配置仓库路径下

server:
  port: 8089

5. 启动引导类ConfigServerApplication主程序

6. 查看请求http://localhost:8088/config-client/dev/master

7. 查看请求http://localhost:8088/config-client-dev.yml 

从结果可以看出:配置中心服务端从远程程序获取配置信息成功

8. 获取git上的资源信息遵循如下规则

http://ip:port/{application}/{profile}[/{label}]
http://ip:port/{application}-{profile}.yml
http://ip:port/{label}/{application}-{profile}.yml
http://ip:port/{application}-{profile}.properties
http://ip:port/{label}/{application}-{profile}.properties

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值