详解Spring Cloud(统一配置中心gitee)四

一、目的

  一方面是统一配置管理,这样可以更方便的管理和更改。另一方面是在配置中心更改后可以同步更新。而且有的配置文件例如数据库密码等是不能够让所有程序员都可见的。

二、实现步骤

1、在原有的maven工程上,我们再创建一个名为config-server的Maven Module
2、pom.xml文件添加jar包

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

3、写启动类ConfigServerApplication

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
  public static void main(String[] args) throws Exception {
     SpringApplication.run(ConfigServerApplication.class, args);
  } 
}

4、写application.yml配置文件

server:
  port: 8080 #端口号
#eureka注册与发现,注册到8761端口的eureka上
eureka:
  client:
    service-url: 
     defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost
#配置应用的名字
spring:
  application:
    name: config-server
  #统一配置中心
  cloud:
    config:
      uri: 
      server:
        git: 
          uri:         #仓库地址
          username:    #仓库登录账号
          password:    #仓库登录密码
          basedir: ./git          #存储在git文件夹下

5、启动服务中心eureka1
6、启动ConfigServerApplication
7、访问http://localhost:8761,可以看到服务挂上了服务中心
在这里插入图片描述
8、访问http://localhost:8080/xxx-a.yml
其中xxx表示在仓库中创建的文件夹的名字,例如创建的文件名为order.yml,那么访问地址为http://localhost:8080/order-a.yml。-a是因为接口规定的。也可以是-dev等。
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值