Spring Cloud Config 分布式配置中心使用

本文介绍如何使用 Spring Cloud Config 实现分布式配置管理。通过创建 ConfigServer 和 ConfigClient,实现配置文件的统一管理和动态加载。文章详细展示了如何配置 SVN 作为远程配置源,并演示了客户端如何从配置中心拉取配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简介

为了方便统一管理配置文件管理,使用spring cloud config作为分布式配置中心,国产比较好的有百度的disconf,携程的apollo,这里我们介绍使用spring cloud config。它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git、SVN等仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

 

二、创建Config Server

 

创建一个spring-cloud项目,取名为config-server,支持maven和gradle,这里配置中心使用svn远程库上的配置:

1.maven的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>config-server</groupId>
	<artifactId>config-server</artifactId>
	<packaging>jar</packaging>
	<name>dmw-config</name>
	<description>配置中心</description>
        <!--使用最新版的spring-boot ->
	 <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR1</spring-cloud.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>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-undertow</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>
   <!--依赖管理,用于管理spring-cloud的依赖,其中Edgware.SR1是版本号-->
	<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>
		<finalName>dmw-config</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
 

 2.gradle build.gradle依赖:

buildscript {
	ext {
		springBootVersion = '1.5.10.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'

group = 'com.config'
version = ''
sourceCompatibility = 1.8

repositories {
    maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
	mavenCentral()
}

configurations {
	providedRuntime
}

ext {
	springCloudVersion = 'Edgware.SR1'
	if(!project.hasProperty("profile")){
		profile='test'
	}
}
sourceSets {
	main {
		resources {
			srcDir "env/${profile}"
		}
	}
}
dependencies {
	compile('org.springframework.cloud:spring-cloud-config-server'){
	 exclude module: 'spring-boot-starter-tomcat'
	}
	compile('org.springframework.cloud:spring-cloud-starter-eureka')
	runtime('org.springframework.boot:spring-boot-starter-undertow')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	compile('org.tmatesoft.svnkit:svnkit')
	compile('org.springframework.boot:spring-boot-starter-actuator')
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

 

新建入口类BootApplication:

 

mport org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication .class, args);
    }
}
 application.yml:

 

 

spring:
  profiles:
    #使用svn这里必须指定为subversion,否则会报错,因为config默认配置的git
    active: subversion
  cloud:
    config:
      server:
        svn:
          # 配置svn仓库地址
          uri: svn://192.168.1.1/config-repo
          # 配置svn访问账号
          username: test
          # 配置svn访问密码
          password: test
          #配置svn项目配置文件所在目录
          default-label: profiles
 服务端完成,如果在svn上的config-repo的profiel目录下有一个application-dev.yml配置文件,则可以通过http://loalhost:8080/application-dev.yml访问获得配置信息,该项目没有指定端口,所以默认8080,

 

 

三、构建Config Client

重新创建一个springboot项目,取名为config-client

 1.maven 的pom文件引入依赖:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

 2. gradle gruadle.build

buildscript {
	ext {
		springBootVersion = '1.5.10.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'com.config'
version = ''
sourceCompatibility = 1.8

repositories {
    maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
	mavenCentral()
}

configurations {
	providedRuntime
}
ext{
    springCloudVersion = 'Edgware.SR1'
	if(!project.hasProperty("profile")){
		profile='demo'
	}
}

sourceSets {
	main {
		resources {
			srcDir "profiles/${profile}"
		}
	}
}
dependencies {
	compile('org.springframework.boot:spring-boot-starter-actuator')
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('org.springframework.cloud:spring-cloud-config-client')
	providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

 其配置文件bootstrap.yml

spring:
  profiles:
    active: dev
  application:
    name: config-client
  cloud:
    config:
      enabled: true
      profile: ${spring.profiles.active}
      uri: http://${spring.cloud.client.ipAddress}:8080
     #如果config-server配置了账号跟密码
      username: test
      password: test
     #重试机制
      fail-fast: true
      retry:
        initial-interval: 2000
        max-interval: 10000
        multiplier: 2
        max-attempts: 10

程序的入口类,写一个API接口“/hello”,返回从如果配置中心的有foo变量的值,则可以获取,代码如下:

 

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication .class, args);
    }
    
    @Value("${foo}") // svn配置文件里的key
    String foo;
    
    @RequestMapping(value = "/hello")
    public String hi(){
        return foo;
    }
    
}

 

 

 

 

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间和笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度和加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置和姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数和工具箱: 优化工具箱:用于解决运动学逆解和路径规划中的优化问题。 Simulink:可视化建模环境,适合构建和仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程和轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间和笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值