Spring Cloud Config Svn 配置项目

SpringCloud微服务使用SVN存储配置信息,并且Config-Server提供高可用配置。

一、创建Config-Server工程

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gf</groupId>
  <artifactId>eureka-order</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>eureka-order Maven Webapp</name>
  <url>http://maven.apache.org</url>
	<!-- 
	SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
	-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-sleuth</artifactId>
		</dependency>
		 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	</dependencies>
  
	<!-- 
		Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
		版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
		比如最早的Release版本号为Angel,第二个Release版本为Brixton
	 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<!-- 
	 根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
	 mvn  package  spring-boot:repackage
	 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

在这里插入图片描述
配置文件application.properties

server.port=8150
spring.application.name=config-server


spring.cloud.config.server.svn.uri=https://java-pc:1443/svn/springcloudconfig
spring.cloud.config.server.svn.username=testsvn
spring.cloud.config.server.svn.password=testsvn
spring.cloud.config.server.svn.default-label=/
spring.profiles.active=subversion

security.user.name=test
security.user.password=test

eureka.client.service-url.defaultZone=http://localhost:8110/eureka

启动类

package com.gf.eureka_order;

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;

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

在这里插入图片描述

二、创建Eureka注册中心

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gf</groupId>
  <artifactId>eureka-server</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>eureka-server Maven Webapp</name>
  <url>http://maven.apache.org</url>
	<!-- 
		SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
	 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>

	<properties>
	  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<!-- 
		Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
		版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
		比如最早的Release版本号为Angel,第二个Release版本为Brixton
	 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
	  <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	  </dependency>
	  <!-- 
	  	Eureka 服务依赖
	   -->
	  <dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	  </dependency>
	  
	</dependencies>

	<!-- 
	 根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
	 mvn  package  spring-boot:repackage
	 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

配置文件application.properties

server.port=8110
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8110/eureka/


启动类

package com.test.eureka_server;

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

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

三、创建Config-Client
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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gf</groupId>
  <artifactId>eureka-order</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>eureka-order Maven Webapp</name>
  <url>http://maven.apache.org</url>
	<!-- 
	SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
	-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-sleuth</artifactId>
		</dependency>
		 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
	</dependencies>
  
	<!-- 
		Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
		版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
		比如最早的Release版本号为Angel,第二个Release版本为Brixton
	 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<!-- 
	 根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
	 mvn  package  spring-boot:repackage
	 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

配置文件bootstrap.properties

server.port=8250
spring.application.name=order

spring.cloud.config.profile=test
#spring.cloud.config.uri=http://localhost:8150/

spring.cloud.config.username=test
spring.cloud.config.password=test

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server

eureka.client.service-url.defaultZone=http://localhost:8110/eureka

在这里插入图片描述
启动类

package com.gf.eureka_order;

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

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

Controller

package com.gf.eureka_order;

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

@RestController
@RequestMapping("/configclient")
public class ConfigCtrl {
	@Value("${name}")
	String name = null;
	
	@RequestMapping("/showname")
	public String showname()
	{
		return name;
	}
}

测试效果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qixiang_chen/article/details/88379480