目录

0、配置服务的需要

1、git

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

3、创建子项目

4、pom.xml

5、ConfigServerApplication

6、application.yml

7、启动

8、参考链接


 

0、配置服务的需要

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

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

在本知识点会讲解如何创建 ConfigServer

微服务设置多大内存_java

1、git

首先要准备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 系列教程

微服务设置多大内存_spring_02

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

老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
先启动 EurekaServerApplication, 再启动 ConfigServerApplication, 然后访问
http://localhost:8030/version/dev 看到如图所示,就表示配置服务器准备好了

微服务设置多大内存_java_03

3、创建子项目

创建子项目 config-server

微服务设置多大内存_spring_04

4、pom.xml

主要是 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>

5、ConfigServerApplication

主要是 @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);
     
    }
}

6、application.yml

name和eureka 信息略过不表。
config 配置信息里
uri 表示 git 地址:

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

label 表示 分支:

master

searchPaths 表示目录:

respo

微服务设置多大内存_微服务设置多大内存_05

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/

7、启动

先启动 EurekaServerApplication, 再启动 ConfigServerApplication, 然后访问
http://localhost:8030/version/dev 看到如图所示,就表示配置服务器准备好了

微服务设置多大内存_spring_06

8、参考链接

[01] How2j - SpringCloud视图微服务 - 配置服务器