Spring Cloud 的 配置远程服务中心简单入门操作

本文介绍了如何使用Spring Cloud构建远程配置服务中心。首先在Git仓库中创建包含数据库配置的.properties文件,接着在本地Maven项目中添加相关依赖,并在application.properties中配置仓库地址和端口(移除.git后缀)。然后通过一个主类启动服务,最后验证通过浏览器访问配置中心是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.配置 spring cloud 的远程服务中心 ,需要先在Git上生成一个仓库。在仓库中配置  .properties 文件




 配置连接数据库的四要素




2 .然后创建本地maven文件 在pom.xml中配置 依赖的包

<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>cn.et</groupId>
  <artifactId>SpringCloud_ConfigServer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!--  依赖springboot -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>

	
	<!-- 配置远程中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  
</dependencies>
  
</project>


3.在maven项目的src/main/resources 创建一个application.properties文件。在文件中配置 仓库的地址 和给仓库配置一个端口 仓库的地址后缀的.git 要去掉

spring.cloud.config.server.git.uri= https://github.com/zmw0221/SpringCloud
server.port=8089

4.最后 一个带main方法的类用来启动 服务

package cn.et;

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

//必须添加SpringbootApplication 启用Spring的自动配置功能
//要扫描添加@ComponentScan(扫描的包名)
@SpringBootApplication
/**
 * 启用分布式配置中心 读取远程git仓库获取配置
 * 所有spring的配置中心的配置文件 都有以下几种格式组成
 * 		1 配置文件属于哪个应用的(application)
 * 		2 配置文件属于哪个 阶段(profile) 开发阶段 测试阶段 产品阶段
 * 		3 在git中的哪个分支(label)
 */
@EnableConfigServer
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}


5.在浏览器中输入路径 看是否能访问 远程服务中心


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值