《springcloud — Finchley.RELEASE版》第六篇 分布式配置中心Config

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunqingzhong44/article/details/83782272

spring cloud config简介

在分布式系统中,每一个功能模块都能拆分成一个独立的服务,一次请求的完成,可能会调用很多个服务协调来完成,为了方便服务配置文件统一管理,更易于部署、维护,所以就需要分布式配置中心组件了,在spring cloud中,提供了分布式配置中心组件spring cloud config,它支持配置文件放在在配置服务的内存中,也支持放在远程Git/SVN仓库里。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

config服务

1.config server服务

在micro-parent下新建module,micro-config-server服务,pom加入 config server依赖,pom文件如下:

<?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>
	<artifactId>micro-config-server</artifactId>
	<packaging>jar</packaging>
	<name>micro-config-server</name>
	<description>微服务实战</description>	
	
	<parent>
		<groupId>com.sun</groupId>
		<artifactId>micro-parent</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<dependencies>

	  <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	  </dependency>
	  
	  <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
	  </dependency>
	  
	  <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	  </dependency>
	
	</dependencies>
	
</project>

配置文件application.yml如下:

server:
  port: 7070
spring:
  application:
    name: micro-config-server
  cloud:
    config:
      label: master  #git分支,我这里是master
      server:
        git:
          uri: https://gitee.com/sunqingzhong/spring-cloud-config-repo  #git仓库地址这里使用码云
          search-paths: micro  #仓库路径
          username:            #用户名密码没有就不用
          password: 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka

启动类需要添加EnableConfigServer注解如下:

package com.sun.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {

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

码云:

启动项目,访问http://localhost:7070/god/dev,可以查看god-dev.properties文件,显示如下:

http请求地址和资源文件映射如下:,可参考

·        /{application}/{profile}[/{label}]

·        /{application}-{profile}.yml

·        /{label}/{application}-{profile}.yml

·        /{application}-{profile}.properties

·        /{label}/{application}-{profile}.properties
2.config client服务

在micro-parent下新建module,micro-config-client 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>
	<artifactId>micro-config-client</artifactId>
	<packaging>jar</packaging>
	<name>micro-config-client</name>
	<description>微服务实战</description>	
	
	<parent>
		<groupId>com.sun</groupId>
		<artifactId>micro-parent</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<dependencies>

	  <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.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	  </dependency>

	</dependencies>
	
</project>

配置文件bootstrap.yml文件(这里不能是application.yml

server:
  port: 7071
spring:
  application:
    name: micro-config-client  #git上的配置名一定要匹配服务名称
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:7070/  #这里可以不同过eureka直接连接config-server
      discovery:
        enabled: true
        service-id: micro-config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/

启动类:

package com.sun.eureka;

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

@SpringBootApplication
@EnableEurekaClient
public class ConfigApplication {

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

写一个对外提供服务的controller:

package com.sun.eureka.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 ConfigController {
	
	@Value("${name}")
	String name;
	
	@RequestMapping(value = "/name")
	public String name(){
		return name;
	}

}

码云spring-cloud-config-repo/micro上增加文件micro-config-client-dev.properties,内容:

name=wangwu

启动访问http://localhost:7071/name,显示如下可见git上的配置文件被读取到了:

源码下载地址:https://gitee.com/sunqingzhong/springcloud_learning_course/tree/master/%E7%AC%AC%E5%85%AD%E7%AF%87%20%E5%88%86%E5%B8%83%E5%BC%8F%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83Config

展开阅读全文

没有更多推荐了,返回首页