基于 Consul 的分布式配置中心(Spring Cloud Config)实例

基于 Consul 的分布式配置中心(Spring Cloud Config)实例

开发环境及工具
  • JDK 1.8
  • Maven 3
  • SpringBoot 2.0
  • SpringCloud Finchley.M7
  • Consul
  • Eclipse
准备:创建 Maven 父项目 negen-demo-parent
  • 配置 pom.xml 引入相关依赖
<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>com.negen</groupId>
  <artifactId>negen-demo-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

	<!-- SpringBoot依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-context</artifactId>
		</dependency>

		<!-- 健康检查 -->
		<dependency>
		 <groupId>org.springframework.boot</groupId>
		 <artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<!-- SpringBoot整合fegnin客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<!-- lombok 依赖 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<!-- hystrix断路器 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>
		<!-- eureka/consul二选一且配置文件不同 -->
		<!-- SpringBoot整合eureka客户端 -->
<!-- 		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency> -->
		<!-- consul依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<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>
<modules>
	<module>config-server</module>
	<module>config-client</module>
</modules>
</project>
  • 启动 Consul ,windows中启动命令如下:
consul agent -dev
一、创建配置服务中心 config-server
1、创建一个空的 Maven module
2、引入相关的 pom 依赖
<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>com.negen</groupId>
    <artifactId>negen-demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>config-server</artifactId>
  <packaging>war</packaging>

  <dependencies>
  	<!-- cloud-config-server依赖 -->
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-config-server</artifactId>
	</dependency>
  </dependencies>
</project>
3、创建启动文件 AppConfigServer.java
package com.negen;


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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class AppConfigServer {
	public static void main(String[] args) {
		SpringApplication.run(AppConfigServer.class, args);
	}
}
4、创建启动配置文件 application.yml
  • git.uri:用于存储配置文件的仓库地址
  • 重点配置:git.uri
spring:
  application:
    name: config-server

  cloud:
    consul:
      host: 127.0.0.1     #consul地址
      port: 8500          #consul端口号
    config:
      server:
        git:              #配置文件 git 地址
          uri: https://github.com/BBBBigFlyPig/SpringConfigServer-Configs.git
5、git仓库中创建 cloud-config-dev.yml 文件并添加以下内容
age: 300
name: test
server:
  port: 8888
6、启动 AppConfigServer.java 并在浏览器中访问 http://localhost:8080/cloud-config-dev.yml

效果图如下:
运行结果.png

至此,server端创建完成

一、创建客户端 config-client
1、创建一个空的 Maven module
2、引入相关的 pom 依赖
<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>com.negen</groupId>
		<artifactId>negen-demo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>config-client</artifactId>
	<packaging>war</packaging>

	<dependencies>
		<!-- cloud-config 客户端依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

	</dependencies>
</project>
3、resource 目录下创建 bootstrap.yml 文件
server:
  port: 8000
spring:
  application:
    name: cloud-config

  cloud:
    consul:
      host: 127.0.0.1
      port: 8500

    config:
      profile: dev          #选择获取的配置文件是dev还是 test之内的;git仓库配置文件命名格式  ${applicatio.name}-dev/test.yml
      discovery:
        enabled: true
        service-id: config-server    #config服务器的 ${applicatio.name}

management:
  endpoints:
    web:
      exposure:
        include: refresh,health  #暴露手动刷新接口
4、创建启动文件 AppConfigClient.java
package com.negen;

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

@SpringBootApplication
@EnableDiscoveryClient
public class AppConfigClient {
	public static void main(String[] args) {
		SpringApplication.run(AppConfigClient.class, args);
	}
}
5、创建测试类 TestConfigServerController.java
package com.negen.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope   //在需要刷新配置的地方使用
public class TestConfigServerController {
	@Value("${age}")
	String age;
	@RequestMapping(value = "/getAge")
	public String getAge() {
		return age;
	}
}
6、启动应用并用浏览器访问 http://localhost:8888/getAge

应用会优先加载从配置中心拉取到的配置,所以访问的端口就是8888;

7、手动刷新配置信息

用 Post 方式访问 http://localhost:8888/actuator/refresh 即可获取最新的配置信息;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个用于构建分布式系统的开发工具集合,它基于Spring Boot框架,提供了一系列的组件和工具,用于简化分布式系统的开发和部署。Spring Cloud提供了诸多功能,包括服务注册与发现、负载均衡、断路器、配置管理、消息总线等,可以帮助开发者快速构建可靠、弹性和高可用的分布式系统。 以下是Spring Cloud的一些核心组件和功能: 1. 服务注册与发现:Spring Cloud提供了服务注册与发现的功能,通过集成Eureka、Consul等注册中心,可以实现服务的自动注册与发现。 2. 负载均衡:Spring Cloud通过集成Ribbon组件,实现了负载均衡的功能,可以根据配置的负载均衡策略,将请求分发到多个服务实例中。 3. 断路器:Spring Cloud集成了Hystrix组件,实现了断路器模式,可以在服务调用失败或超时时进行降级处理,保证系统的稳定性。 4. 配置管理:Spring Cloud提供了统一的配置管理功能,通过集成Config Server组件,可以将应用的配置信息集中管理,并支持动态刷新配置。 5. 消息总线:Spring Cloud通过集成Spring Cloud Bus组件,实现了消息总线的功能,可以在分布式系统中实现配置的动态刷新和事件的广播。 6. 链路追踪:Spring Cloud集成了Zipkin组件,可以实现分布式系统的链路追踪,帮助开发者快速定位和解决问题。 7. 服务网关:Spring Cloud提供了Zuul组件,可以实现统一的API网关,对外暴露统一的接口,并提供路由、过滤等功能。 8. 分布式事务:Spring Cloud通过集成Seata等组件,可以实现分布式事务的管理,保证多个服务之间的数据一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值