微服务配置中心实现--SpringCloud-config-eureka高可用实现_手动刷新

定义:

Spring Cloud Config 提供了配置中心的功能,但是需要配合 git、svn 或外部存储(例如各种数据库),可以说是动态获取Git、SVN、本地的配置文件的一种工具。

高可用实现:

配置中心,作为很重要的一部分,一个服务无法保证其高可用; 将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:
在这里插入图片描述

项目目录结构:

在这里插入图片描述

父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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringCloud_config_bus_demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>SpringCloud-Config-Server</module>
        <module>SpringCloud-Config-Client</module>
        <module>SpringCloud-eureka</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencyManagement>`在这里插入代码片`
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

第一步,首先是注册中心SpringCloud-eureka:

在这里插入图片描述

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloud_config_bus_demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringCloud-eureka</artifactId>
    <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-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </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>
    </dependencies>
    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、application.yml 与 bootstrap.yml 配置文件内容:

application.yml :
server:
  port: 8866
eureka:
  server:
    enable-self-preservation: false
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostame}:${server.port}/eureka/
bootstrap.yml:
spring:
  application:
    name: SpringCloud-eureka
  security:
    basic:
      enabled: true
    user:
      name: root
      password: root
  cloud:
    inetutils: ## 网卡设置
      ignoredInterfaces:  ## 忽略的网卡
        - docker0
        - veth.*
        - VM.*
      preferredNetworks:  ## 优先的网段
        - 192.168

3、因为eureka开启了security验证,所以在此需要做csrf处理,如下:

package com.springcloud.eureka;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @auther: 坎布里奇
 * @description: xxxxx
 * @date: 2021/2/16 2:20
 * @version: 1.0.0
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

4、启动类:

package com.springcloud.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @auther: 坎布里奇
 * @description: xxxxx
 * @date: 2021/2/14 19:27
 * @version: 1.0.0
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

第二步,配置中心SpringCloud-Config-Server 注册到注册中心:

在这里插入图片描述

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloud_config_bus_demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SpringCloud-Config-Server</artifactId>
    <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-config-server</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、application-prod.yml 与 bootstrap.yml 配置文件内容:

application.yml:
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/dream_house_1/MyConfig.git
          username: ********
          password: ********
          default-label: master #配置文件分支
          search-paths: config  #配置文件所在根目录
    inetutils:
      ignoredInterfaces:
        - docker0
        - veth.*
      preferredNetworks:
        - 192.168
bootstrap.yml:
server:
  port: 8877
spring:
  application:
    name: SpringCloud-Config-Server
  profiles:
    active: prod
eureka:
  client:
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://root:root@localhost:8866/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

3、启动类:

package com.springcloud.configServer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @auther: 坎布里奇
 * @description: xxxxx
 * @date: 2021/2/14 14:39
 * @version: 1.0.0
 */

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class SpringCloudConfigServerApplication {

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

第三步,客户端SpringCloud-Config-Client在注册中心中请求配置中心中的文件:

目录:

在这里插入图片描述

请求的文件application-dev.yml,最后可以请求到foo的内容,则证明成功:

在这里插入图片描述

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloud_config_bus_demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringCloud-Config-Client</artifactId>
    <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-config-client</artifactId>
        </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>
        <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-actuator</artifactId>
        </dependency>
    </dependencies>

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

</project>

2、配置文件,这里只展示bootstrap.xml:

spring:
  application:
    name: SpringCloud-Config-Client
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        # 默认false,设为true表示使用注册中心中的配置服务(服务发现)而不自己指定配置服务的地址(即uri)
        enabled: true
        # 指向配置中心在Eureka 注册的服务名称,请求的是服务名,所以配置中心部署多个,通过负载均衡,可以实现高可用(即:spring.application.name)
        serviceId: SpringCloud-Config-Server
eureka:
  client:
    serviceUrl:
      defaultZone: http://root:root@localhost:8866/eureka/
#开启所有端点 refresh config
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、创建类,获取配置中心中foo的属性值:

package com.springcloud.configClient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @auther: 坎布里奇
 * @description: xxxxx
 * @date: 2021/2/14 17:17
 * @version: 1.0.0
 */ 
@Component
// 标注此类配置在手动刷新时,需要重新向git获取
@RefreshScope
public class ConfigServerConfig {

    // 获取的是, 配置中心,配置文件中的属性。
    @Value("${foo}")
    String foo;

    public String getFoo() {
        return foo;
    }
    public void setFoo(String foo) {
        this.foo = foo;
    }
}
package com.springcloud.configClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther: 坎布里奇
 * @description: xxxxx
 * @date: 2021/2/14 17:18
 * @version: 1.0.0
 */
@RestController
public class GitController {

    @Autowired
    private ConfigServerConfig configServerConfig;

    @RequestMapping(value = "/hi")
    public Object hi(){
        return configServerConfig;
    }
}

4、启动类:

package com.springcloud.configClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**

  • @auther: 坎布里奇

  • @description: xxxxx

  • @date: 2021/2/14 14:43

  • @version: 1.0.0
    */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class SpringCloudConfigClientApplication {

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

测试:

依次启动:

SpringCloud-eureka、SpringCloud-Config-Server、SpringCloud-Config-Client;

SpringCloud-eureka启动后,请求http://localhost:8866 : 需要输入账号密码: root root

在这里插入图片描述

输入账号密码后,此时是空的,没有应用注册:

在这里插入图片描述

SpringCloud-Config-Server 启动后,如下,配置中心注册成功:

在这里插入图片描述

SpringCloud-Config-Client启动后如下,客户端注册成功:

在这里插入图片描述
可以看出status下,两个不一样,是因为SpringCloud-Config-Server 添加了:

instance:
	prefer-ip-address: true
	instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

关键的时刻到了,测试foo 属性是否获取成功,请求地址 http://localhost:8886/hi ,结果如下:

在这里插入图片描述
大功告成!!!!!
因为是测试,所以配置中心,只启动了一次,在这里可以把SpringCloud-Config-Server部署多个服务,通过负载均衡,实现配置中心的高可用;

有一个不好的消息,就是这样的配置,如果你修改了配置文件,只有重启服务才能生效,如下:

请求方式如下: post http://localhost:端口(客户端端口)/actiator/refresh;返回结果显示如下,表示已经更新了修改。
在这里插入图片描述

备注: 实现手动刷新有几点:

1、客户端pom中需要加入:

<!-- 手动刷新客户端配置依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、配置文件bootstrap.yml中加入:

#开启所有端点 refresh config
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、获取配置中心文件中属性的类要加注解 @RefreshScope :

@Component
// 标注此类配置在手动刷新时,需要重新向git获取
@RefreshScope
public class ConfigServerConfig {
    // 获取的是, 配置中心,配置文件中的属性。
    @Value("${foo}")
    String foo;
    public String getFoo() {
        return foo;
    }
    public void setFoo(String foo) {
        this.foo = foo;
    }
}

4、修改完后,以 post 请求 http://localhost:端口(客户端端口)/actiator/refresh

源码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值