【SpringCloud Greenwich版本】第七章:配置中心服务端(config server)

一、SpringCloud版本

本文介绍的Springboot版本为2.1.1.RELEASE,SpringCloud版本为Greenwich.RC1,JDK版本为1.8,集成环境为IntelliJ IDEA

二、config server介绍

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

Spring Cloud Config分为服务端和客户端,下面我们先来为大家介绍服务端,服务端为外部配置(名称值对或等效的properties内容)提供了基于资源的HTTP。

要在您的项目中包含config server,请使用组org.springframework.cloud和spring-cloud-config-server的启动器。

三、创建config服务端

  • 3.1创建

创建一个module,取名为configserver,选择Cloud Config,再勾选上Config Server,完成
在这里插入图片描述
创建完成的pom文件,可以看到spring-cloud-config-server依赖

<?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.jthao</groupId>
    <artifactId>configserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configserver</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.jthao</groupId>
        <artifactId>cloudser</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

    </dependencies>

</project>
  • 3.2启动

在启动类上增加@EnableConfigServer,开启config服务

package com.jthao.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigserverApplication {

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

}

修改配置文件,设置端口8101,服务名config,配置服务注册中心

spring.cloud.config.server.git.uri为github仓库地址
spring.cloud.config.server.git.search-paths为github配置文件路径
spring.cloud.config.label为仓库分支
spring.cloud.config.server.git.username为github用户名
spring.cloud.config.server.git.password为github密码

github如果是私有仓库,必须填写用户名和密码,本文示例为私有仓库,需自己重新配置

eureka.client.service-url.defaultZone= http://localhost:8001/eureka/
spring.application.name=configserver
server.port=8101

#git repo的url地址
spring.cloud.config.server.git.uri=https://gitee.com/jthao225/spring-cloud
spring.cloud.config.server.git.search-paths=/config
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
#config-dev.properties    config-test.propertiess
  • 3.3访问

通过浏览器访问http://localhost:8101/config-dev.properties
在这里插入图片描述

四、更多文章阅读

【SpringCloud Greenwich版本】汇总
【JAVA设计模式】汇总

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值