21.Spring Cloud之分布式配置中心Spring Cloud Config注册中心版

前面的博客已经演示无注册中心版,那么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>2.spring-cloud-service-provide</groupId>
<artifactId>service-provide</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--springboot采用1.5.x 对应springcloud版本为 Dalston -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</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>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!--将微服务注册到注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--@SpringCloudApplication注解需要依赖的  -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<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>
<!-- 这样变成可执行的jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

 

  • application.properties
#指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
spring.application.name=config-server
server.port=8779
# 配置服务注册中心
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/,http://testhost2:8001/eureka/
#指定本地仓库路径,默认是随机生成
spring.cloud.config.server.git.basedir=/home/springcloud/configrepos
# git仓库配置 主分支路径
spring.cloud.config.server.git.uri=https://gitee.com/niugangxy/springcloud
#很多场景下,可能把配置文件放在git仓库子目录下,可通过searchPaths配置,searchPaths也支持占位符
#如下配置,Config server会在git目录spring-cloud-config-respo,及其子目录下查找配置文件
spring.cloud.config.server.git.searchPaths=spring-cloud-config-respo
#git用户名
spring.cloud.config.server.git.username=username
#git用户密码
spring.cloud.config.server.git.password=password

 

  • 启动类
package com.niugang;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * config server配置
 * 
 * @author niugang
 * @SpringBootApplication 
 * @EnableDiscoveryClient   
 * @EnableCircuitBreaker
 */
@SpringCloudApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

 



 

客户端(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>2.spring-cloud-service-provide</groupId>
<artifactId>service-provide</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--springboot采用1.5.x 对应springcloud版本为 Dalston -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</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>Dalston.RELEASE</spring-cloud.version>
</properties>


<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--@SpringCloudApplication注解需要依赖的 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- Could not resolve placeholder 'from' in value "${from}" -->
<!-- <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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
        <dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<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>
<!-- 这样变成可执行的jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

 

  • appplication.properties
#配置端口
server.port=8780

 

 
  • bootstrap.properties
#指定注册中心
eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/,http://testhost2:8001/eureka/
#开起通过服务来访问Config Server
spring.cloud.config.discovery.enabled=true
#指定配置分布式配置中心服务名
spring.cloud.config.discovery.serviceId=config-server
#指定git仓库的分支,对应config server所获取的配置文件的{label}
spring.cloud.config.label=master
#失败快速响应和重试
spring.cloud.config.failFast=true
#对应 config server所获取的配置文件的{application}
spring.application.name=niugang
#对应config server所获取的配置文件的{profile}
spring.cloud.config.profile=dev

 

 
  • 启动类
package com.niugang;


import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;


/**
 * @author niugang
 *
 */
@SpringCloudApplication
public class Application {
public static void main(String[] args) {                                                                                                                                                                                                                                                                                                                                                         SpringApplication.run(Application.class, args);
}
}

 

 
  • controller测试
package com.niugang.controller;


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

@RestController
public class ConfigClientController {


/**
* 从niugang-dev.properties中获取配置的from值
*/
@Value("${from}")
private String from;


@RequestMapping("/profile")
public String profile() {
return this.from;
}


}

 

 
 

 

 微信公众号

 

 

posted @ 2020-01-14 16:24  盲目的拾荒者  阅读(333)  评论(0编辑  收藏  举报