how2j.cn

下载区
文件名 文件大小
springcloud.rar 55k
步骤 1 : 配置服务的需要   
步骤 2 : git   
步骤 3 : 先运行,看到效果,再学习   
步骤 4 : 模仿和排错   
步骤 5 : 创建子项目   
步骤 6 : pom.xml   
步骤 7 : ConfigServerApplication   
步骤 8 : application.yml   
步骤 9 : 启动   

步骤 1 :

配置服务的需要

edit
有时候,微服务要做集群,这就意味着,会有多个微服务实例。 在业务上有时候需要修改一些配置信息,比如说 版本信息吧~ 倘若没有配置服务, 那么就需要挨个修改微服务,挨个重新部署微服务,这样就比较麻烦。
为了偷懒, 这些配置信息就会放在一个公共的地方,比如git, 然后通过配置服务器把它获取下来,然后微服务再从配置服务器上取下来。
这样只要修改git上的信息,那么同一个集群里的所有微服务都立即获取相应信息了,这样就大大节约了开发,上线和重新部署的时间了。

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

在本知识点会讲解如何创建 ConfigServer
配置服务的需要
首先要准备git。
如下是已经准备好的 git:
https://github.com/how2j/springcloudConfig/blob/master/respo/product-view-service-feign-dev.properties
这里就准备了版本信息: version = how2j springcloud version 1.0
注:如果重来没有接触过 git ,可以通过本站的 git 教程学习: git 系列教程
git
步骤 3 :

先运行,看到效果,再学习

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
先启动 EurekaServerApplication, 再启动 ConfigServerApplication, 然后访问
http://localhost:8030/version/dev
看到如图所示,就表示配置服务器准备好了
先运行,看到效果,再学习
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
创建子项目 config-server
创建子项目
主要是 spring-cloud-config-server 这个 jar 包
<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> <parent> <groupId>cn.how2j.springcloud</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>configServer</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>
步骤 7 :

ConfigServerApplication

edit
主要是 @EnableConfigServer 这个注解表示本springboot 是个配置服务器。
使用的是 8030 端口
package cn.how2j.springcloud; 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; import cn.hutool.core.util.NetUtil; @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); } }
name和eureka 信息略过不表。
config 配置信息里
uri 表示 git 地址:

https://github.com/how2j/springcloudConfig/

label 表示 分支:

master

searchPaths 表示目录:

respo
application.yml
spring: application: name: config-server cloud: config: label: master server: git: uri: https://github.com/how2j/springcloudConfig/ searchPaths: respo eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: config-server
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://github.com/how2j/springcloudConfig/
          searchPaths: respo
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
先启动 EurekaServerApplication, 再启动 ConfigServerApplication, 然后访问
http://localhost:8030/version/dev
看到如图所示,就表示配置服务器准备好了
启动


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2022-06-22 公司内网无法访问 https://github.com/how2j/springcloudConfig/,有什么替代方式不。能本地完成的最好。
zpj1334829606a

公司内网无法访问 https://github.com/how2j/springcloudConfig/,有什么替代方式不。能本地完成的最好。







回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2022-03-04 No custom http config found for URL 解决
Billow

把https://github.com/how2j/springcloudConfig/ 改成https://github.com/how2j/springcloudConfig 最后的 "/" 删掉







回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2021-12-22 把配置文件放在远程服务器上,获取的原理是什么呢?站长,我不理解。
2021-08-10 http://localhost:8030/version/dev出不来
2021-03-09 如果自己搭建为私有的记得加username和password


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 20 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 分布式和集群-SpringCloud-配置服务器 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图