SpingCloud 2020微服务教程【35】配置中心之服务端模块搭建

视频链接:2020最新版SpringCloud框架开发教程-周阳
文章源码:https://github.com/geyiwei-suzhou/cloud2020/

首先,在自己的github账号中创建一个新仓库springcloud-config,代码可从git@github.com:geyiwei-suzhou/springcloud-config.git拉取

cloud-config-center-3344模块

1. 建module

New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-config-center-3344] --> Finish

2. 改pom

<?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">
  <parent>
    <artifactId>cloud2020</artifactId>
    <groupId>com.antherd.springcloud</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>cloud-config-center-3344</artifactId>

  <dependencies>
    <!-- spring cloud config -->
    <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-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

3. 写yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center # 注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/geyiwei-suzhou/springcloud-config.git # 私库需要配置ssh秘钥或者配置用户名、密码
          # 搜索目录
          search-paths:
            - springcloud-config
          # 读取分支
          default-label: master

# 服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4. 主启动

新建类:com.antherd.springcloud.ConfigCenterMain3344

package com.antherd.springcloud;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {

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

修改主机的host文件(如mac系统:sudo vim /etc/hosts),添加如下内容

# SpringCloud Config
127.0.0.1 config-3344.com

5. 启动测试

启动:cloud-eureka-server7001,cloud-config-center-3344 两个模块

访问:http://config-3344.com:3344/master/config-dev.yml 即可访问到config-dev.yml的文件内容

HTTP服务资源可用通过如下几种形式获得

  • /{application}/{profile}[/label]:返回json
  • /{application}-{profile}.yml:默认label为master,返回文件内容
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

application:应用名、profile:环境(dev/test/prod)、label:分支(branch)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值