Spring Cloud项目(二): spring cloud config + eureka + git 远程仓库配置文件

Config远程git仓库操作

分布式配置中心

所有服务的配置文件归于中心仓库管理,spring cloud config 提供了将配置服务放在本地,也提供了远程仓库中,提供了两个角色,一是config server,二是config client

git
---->config 1,config 2,congfig3(可以看成一个仓库的不同分支,为不同服务提供定制的配置文件)
—>将config server注册在eureka中—>config client通过eureka的中地址获取仓库地址,连接上config server

在这里插入图片描述

新建项目顺序

1. git仓库,提供配置文件
2. eureka项目,config服务端和客户端都需要注册在上面
3. configServer项目,服务端主要负责连接git,获取git参数,并且注册在eureka中,方便客户端调用
4. configClient项目,客户端通过传递参数,从eureka中获取服务端的地址去对应参数的配置文件

一、新建配置文件git仓库,并将配置文件放入库中 (https://github.com/zz790609619/configRes/blob/master/application.yml)


server: 
  port: 8090

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
    username: root
    password: 999521
    initialSize: 5
    minIdle: 1
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
  jpa:
    database: mysql
    show-sql: true
    hibernate:
    ddl-auto: none

二、新建Eurake项目,作为服务注册中心(https://github.com/zz790609619/eurekaserver)

作用:为springcloud config server(服务端)、springcloud config client(客户端)提供统一的注册地址

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eurekazz</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekazz</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
    	<!-- eureka的服务器端口-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
        	<!-- spring boot 对应 spring cloud版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

#设置tomcat服务端口号
server:
  port : 8761
spring:
  application:
    name: eurekazz
eureka:
  client:
    fetch-registry: false #自己就是注册中心,负责维护服务,不需要去检索服务
    register-with-eureka: false #不向注册中心注册自己


启动类

@SpringBootApplication
@EnableEurekaServer  //开启eureka服务
public class EurekazzApplication {

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

当控制台出现如下提示,则可以打开http://localhost:8761/,就能看到eureka服务页面
在这里插入图片描述

三、新建Springcloud configServer 项目。作为配置中心的服务端(项目地址:https://github.com/zz790609619/springcloud-configserver

项目作用:提供连接git的参数并且连接git,获取到git上的配置文件,并且将本身注册在eureka中,方便客户端通过配置中心的地址取获取git上的远程配置文件
  1. pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- springboot版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> 
    </parent>
    <groupId>com.example</groupId>
    <artifactId>configsever</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configsever</name>
    <description>Demo project for Spring Boot</description>
    <!-- jdk版本,和与上面springboot版本对应的springcloud版本 -->
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
    	<!--提供springboot启动服务  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>  
<!--        <dependency>-->
<!--        <groupId>org.springframework.cloud</groupId>-->
<!--        <artifactId>spring-cloud-netflix-eureka-server</artifactId>-->
<!--        <version>2.1.2.RELEASE</version>-->
<!--    </dependency>-->
 		<dependency>
 			<!-- eureka依赖 如果artifactId为spring-cloud-eureka ,则需要解除上面注释的依赖   -->   
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
        	<!-- 客户端依赖,注册在eureka上   -->   
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
       		<!-- 熔断器  -->  
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
        	<!-- 远程配置的依赖  -->  
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
       
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
        	<!-- 开启web访问的依赖  -->  
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  1. bootstrap.yml(一般将重要参数放入该文件里,优先级高,bootstrap一定会被读取(覆盖application.yml的相同配置))
server:
  port: 6666 #端口号
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka #注册到注册中心
  instance:
    prefer-ip-address: true #显示ip
spring:
  application:
    name: configsever #在注册中心显示的主机名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zz790609619/configRes #github的地址
          username: 790609619@qq.com #github的账户名
          password: xxxxxx #gitHub的密码
          clone-on-start: true

  1. 启动类
package com.example.configsever;

import org.springframework.boot.SpringApplication;

import org.springframework.cloud.client.SpringCloudApplication;

import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@EnableEurekaClient //加入注册中心
@SpringCloudApplication
@EnableConfigServer //启用配置服务端
/**
 * author : ww
 */

public class ConfigseverApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigseverApplication.class, args);
    }
}
  1. 启动结果
    在这里插入图片描述
  2. 访问http://localhost:8761/,查看是否注册在eureka上
    在这里插入图片描述
  3. 用接口调用软件(postman)访问,获取到git上面的配置文件即为成功

/{application}/{profile}[/{label}] /{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。
{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。
{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了

在这里插入图片描述

四、新建Springcloud configClient 项目。作为配置中心的客户端(项目地址:https://github.com/zz790609619/configclient

项目作用:需要用到git中配置文件的项目,通过访问eureka中的configserver的地址,传递参数去获取git中对应分支的配置文件
  1. pom.xml
    <dependencies>
    	<!-- 客户端需要传递参数(分支名,文件名),需要提供config依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- 客户端也要注册在eureka中 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    


  1. bootstrap.yml
spring:
  application:
    name: configclient #应用名称
  cloud:
    config:
      label: master #分支名
      discovery:
        enabled: true
        service-id: configsever # 服务端的spring.application.name 便于从eureka中寻找对应的名字的地址,以获取git中参数
      fail-fast: true #快速获取失败
      profile: master #相同文件名的后缀,application-dev.yml、application-master.yml
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/ #注册在eureka

  1. 启动类
@SpringCloudApplication
public class ConfigclientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigclientApplication.class, args);
    }
}

  1. 运行项目,查看控制台的端口号是否和git中一致?看看eureka是否有当前的服务注册在上面?
    在这里插入图片描述

补充说明:

ConfigClient如何获取到git中的文件内容?
  1. configclient注册在eureka中,通过配置文件中spring.cloud.congfig中discovery.service-id去eureka中获取该id对应的服务端的地址(点击下图标红,则能获取该服务端的ip+端口 http://192.168.93.1:6666/)
    在这里插入图片描述
  2. 根据配置文件中的label作为参数,通过步骤1获取的地址去获取git对应label分支中的文件,作为客户端的配置文件,如果该分支下有多个文件,则根据profile去获取对应的版本。
ConfigServer如何与git进行连接?
  1. 项目启动类中通过注解(@EnableConfigServer)中的ConfigServerConfiguration
    在这里插入图片描述
  2. 打开ConfigServerConfiguration,可以看到只会返回一个marker,查询marker被调用的类
    在这里插入图片描述
  3. 找到ConfigServerAutoConfiguration类,可以看到与config有关的是EnvironmentRepositoryConfiguration类
    在这里插入图片描述
  4. 查看import的类则能看到git相关的@Import({CompositeRepositoryConfiguration.class, JdbcRepositoryConfiguration.class, VaultConfiguration.class, VaultRepositoryConfiguration.class, CredhubConfiguration.class, CredhubRepositoryConfiguration.class, SvnRepositoryConfiguration.class, NativeRepositoryConfiguration.class, *GitRepositoryConfiguration.class*, DefaultRepositoryConfiguration.class})
  5. 根据@Profile({“git”}) 可以获取到配置文件中配置的参数
    在这里插入图片描述
  6. 因为GitRepositoryConfiguration继承于DefaultRepositoryConfiguration类,图中标红的类都实现了EnvironmentRepositoryFactory的方法在这里插入图片描述
  7. EnvironmentRepositoryFactory继承与EnvironmentRepository,EnvironmentRepository中存在一个findone的方法
    在这里插入图片描述
  8. 看下方法的参数,和我们配置文件中的参数有点像,可以查到ResourceController中有该方法,可以大概推测Config Server是通过建立一个RestController来接收读取配置请求的,然后使用EnvironmentRepository来进行配置查询,返回一个org.springframework.cloud.config.environment.Environment对象的json串,推测客户端接收时也应该是反序列化为org.springframework.cloud.config.environment.Environment的一个实例
  9. 所以client(客户端)从服务端获取序列化的参数,并且将之反序列化,作用于自己项目。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值