配置中心git服务化和高可用

简介

  客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。

springcloud提供了这样的解决方案,将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。

2.Eureka服务注册中心

2.1pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent> 
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<!--注意:这里必须要添加,否则各种依赖有问题-->
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

2.2application.yml

spring: 
  application: 
    name: EurekaServer

server: 
  port: 5555
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://127.0.0.1:5555/eureka/

2.3main方法启动

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerRun {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerRun.class, args);
    }
}

3.server服务端

3.1pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent> 
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<!--注意:这里必须要添加,否则各种依赖有问题-->
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

3.2application.yml

server:
  port: 8007

spring:
  application:
    name: serverConfigHigh
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mayunchenwei/test.git  #git仓库地址
          username: 17809213302@163.com    
          password: chenwei0307 

eureka:
  client:
   service-url:
defaultZone: http://127.0.0.1:5555/eureka/  #注册中心eureka地址

3.3main方法启动

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ServerConfigHighRun {
    public static void main(String[] args) {
        SpringApplication.run(ServerConfigHighRun.class, args);
    }
}

4.client客户端

4.1pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent> 
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
       <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 监控程序在运行时的状态,包括refresh功能 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<!--注意:这里必须要添加,否则各种依赖有问题-->
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

4.2application.yml

server:
  port: 8008

4.3bootstrap.yml

spring:
  application:
    name: clientConfigHigh
  cloud:
    config:
      name: neo-config
      profile: dev
      uri: http://localhost:8007
      discovery:
        enabled: true 
        serviceId: serverConfigHigh

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:5555/eureka/

4.4main方法启动

@SpringBootApplication
@EnableDiscoveryClient
public class ClientConfigHighRun {

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

### 4.5Controller

@RestController
@RefreshScope //使用该注解的类,会在接收springcloud配置中心配置刷新时,自动将新的配置更新到该类的对应的字段
public class testHello {
    @Value(value="${neo.hello}")
    private String hello;

    @RequestMapping("/testController")
    public String tetst(){
        return this.hello; 
    }
}

5 高可用

为了模拟生产集群环境,我们改动server端的端口为8009,再启动一个server端来做服务的负载,提供高可用的server端支持。

单独测试服务端,分别访问:http://127.0.0.1:8007/neo-config/dev、http://127.0.0.1:8009/neo-config/dev返回信息:则可正常读取配置信息。
浏览器输入 http://127.0.0.1:8008/testController 返回 dev is updata,关闭其中一台服务器,再次输入http://127.0.0.1:8008/testController 返回 dev is updata,
说明已经达到了高可用的目的。

文章标题:配置中心git服务化和高可用

发布时间:2019-11-13, 17:11:42

最后更新:2019-11-13, 17:11:42