SpringCloud学习笔记(八、配置服务器)

简介: SpringCloud学习笔记(八、配置服务器)

为什么配置服务器

有时候,微服务要做集群,这就意味着,会有多个微服务实例。 在业务上有时候需要修改一些配置信息,比如说 版本信息吧~ 倘若没有配置服务, 那么就需要挨个修改微服务,挨个重新部署微服务,这样就比较麻烦。

为了减少工作量,可以把 这些配置信息就会放在一个公共的地方,比如git, 然后通过配置服务器把它获取下来,然后微服务再从配置服务器上取下来。

这样只要修改git上的信息,那么同一个集群里的所有微服务都立即获取相应信息了,这样就大大节约了开发,上线和重新部署的时间了。

如图所示,我们先在 git 里保存 version 信息, 然后通过 ConfigServer 去获取 version 信息, 接着不同的视图微服务实例再去 ConfigServer 里获取 version.

image.png

在git上创建配置文件

本来想在github上面创建的,结果网卡了一点,所以就在码云上创建了一个。码云可以直接用github的账号登录。

1、创建仓库

选择公开吧,免得还得输密码什么的。

image.png

2、创建文件夹

创建一个名为respo的文件夹

image.png

3、创建配置文件

创建配置文件product-view-service-dev.properties

里面就一行东西:

version = LaughterYoung springcloud version 1.0

创建子项目

创建子项目cofig-server

pom.xml

添加spring-cloud-config-serve依赖

<?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>springcloud</artifactId>
        <groupId>edu.hpu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>config-server</artifactId>
    <dependencies>
        <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.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>

配置

application.yml:

spring:
  application:
    name: config-server
  cloud:
    config:
      label: master                         #表示分支
      server:
        git:
          uri: https://gitee.com/LaughterYoung/springCloudConfig/   #表示git地址
          searchPaths: respo                       #表示目录
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动类

ConfigServerApplication:

package edu.hpu.springcloud;
import cn.hutool.core.util.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer                 //表示这是一个配置服务器
@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigServerApplication {
    public static void main(String[] args) {
        int port = 8030;
        if(!NetUtil.isUsableLocalPort(port)) {
            System.err.printf("端口%d被占用了,无法启动%n", port );
            System.exit(1);
        }
        new SpringApplicationBuilder(ConfigServerApplication.class).properties("server.port=" + port).run(args);
    }
}

启动并访问

运行启动类,访问地址:http://localhost:8030/version/dev

image.png

参考:

【1】、http://how2j.cn/k/springcloud/springcloud-config-server/2047.html#nowhere

【2】、https://blog.csdn.net/yjw123456/article/details/84330523#Git_117


目录
相关文章
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
4492 1
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
1461 11
|
存储 弹性计算 网络协议
超详细的阿里云服务器购买流程,ECS自定义购买配置教程
本文详细图解阿里云ECS服务器自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像、存储、安全组及登录设置等核心步骤,助您轻松掌握专业级云服务器搭建方法。
|
存储 弹性计算 监控
阿里云渠道商:如何挑选阿里云服务器配置?
本文详解通用型、计算型、内存型等实例适用场景,结合性能评估与成本优化策略,助力用户按需选择。以日均1万访问企业网站为例,2核4G+3M带宽月费约200元,性价比高。合理配置更省钱。
|
弹性计算
阿里云ECS云服务器8核16G配置收费价格,多种ECS实例CPU及费用清单
阿里云8核16G云服务器价格因实例类型而异。计算型c9i约743元/月,一年6450元(7折);通用算力型u1仅673元/月,一年4225元(5.1折)。实际价格享时长折扣,详情见ECS官网。
|
弹性计算 Shell Windows
阿里云学生专享ecs服务器体验及个人配置
阿里云学生优惠专享的云服务器个人使用体验
阿里云学生专享ecs服务器体验及个人配置
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
1159 10
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
1114 8