一 基础知识
spring cloud用于微服务的配置统一管理,能够集中管理多个应用的外部配置,项目中使用清晰明了,可对配置进行版本控制或集成spring cloud bus进行热更新。多环境配置文件如下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
application:应用名称;profile:环境;lable:版本控制(对应git的分支)。
github上新建项目spring-cloud-learn,具体步骤百度之。
git clone到本地,在此目录中新建项目,ms-parent为所有服务的parent,放服务共有配置和代码等。
ms-parent的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>
<groupId>com.hjb.spring.cloud</groupId>
<artifactId>ms-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../ms-discoveryServer</module>
<module>../ms-configClient-base</module>
<module>../ms-configClient-update</module>
<module>../ms-configServer-base</module>
<module>../ms-configServer-update</module>
<module>../ms-configServer-auth-security</module>
<module>../ms-configServer-jdbc</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<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>
</project>
学习性质,尽量高版本,使用spring boot的2.0.1版本,spring cloud的Finchley.RC1版本。
spring cloud config可不依赖于服务注册和发现,通过在配置中心客户端配置文件中指明配置中心服务端的uri进行寻址和通信,但一般微服务项目中,服务注册和发现是必不可少的,因此这里我们新建一个最简单的module ms-discoveryServer作为服务注册中心(eureka方式,还有其他的方式,如consul,后续章节会一一研究学习),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">
<parent>
<artifactId>ms-parent</artifactId>
<groupId>com.hjb.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../ms-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ms-discoveryServer</artifactId>
<dependencies>
<!--####################################spring cloud 服务发现注册服务#########################################-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
之前使用过Brixton.SR5版本,服务注册中心依赖如下(这里和Finchley不同):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
配置文件application.properties如下:
#定义服务端口
server.port=8761
#定义服务名称
spring.application.name=discovery
#关闭自注册
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#服务注册中心对外地址(其他服务可通过配置文件中配置此地址进行服务注册)
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
启动类如下(运行即可作为一个服务注册中心,是不是超级简单,这就是spring cloud开箱即用的特性,但想想这种方式也可能导致越来越多的码农们变成cv程序猿,懒得去思考内部原理,总之,这里先学会使用,然后再去深入):
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerMain {
public static void main(String[] args){
SpringApplication.run(DiscoveryServerMain.class,args);
}
}
二 服务端
新建module ms-configServer-base,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">
<parent>
<artifactId>ms-parent</artifactId>
<groupId>com.hjb.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../ms-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ms-configServer</artifactId>
<dependencies>
<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.properties如下:
#改变默认端口,默认为8080
server.port=8088
#配置中心名称(用于服务注册发现,客户端可通过服务名称寻找配置中心服务端)
spring.application.name=config-server
#git配置仓库地址
spring.cloud.config.server.git.uri=https://github.com/hjbbjh/spring-cloud-learn/
#如果没有配置search path则默认在git项目的根目录下找相应的配置文件
#添加search path后在search path中找配置文件(指定目录)
spring.cloud.config.server.git.search-paths=config-client
#占位符方式,可实现一个应用一个配置目录
#spring.cloud.config.server.git.search-paths={application}
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
程序入口类:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerMain {
public static void main(String[] args){
SpringApplication.run(ConfigServerMain.class,args);
}
}
简单的配置中心实现起来也超级简单,步骤如下:
一: pom中加入spring-cloud-config-server即可(如果客户端通过uri进行寻找服务端,仅添加此依赖即可,如果需要通过服务端名称寻址,则需添加spring-cloud-starter-netflix-eureka-client依赖,注册到服务注册中心,Brixton.SR5版本对应依赖为spring-cloud-starter-eureka)
二:application.properties配置文件中配置端口、名称、git相关信息及注册中心uri即可。
git uri及search path均可以使用占位符({application},{profile},{label})以及通配符,根据项目需求进行使用。一般微服务架构中,为了方便管理,会在一个git仓库中集中管理多个服务的配置。因此在search path中使用{application}占位符就可以实现一个应用一个配置目录,而profile一般不会有太多,用到application级别即可。
yaml文件中{application}要用单引号引起来,properties不用。
这里仅仅使用git方式,还可以使用svn方式及jdbc方式,后续会研究jdbc方式。
三:程序入口添加注解@EnableConfigServer
启动ConfigServerMain即可实现一个简单的配置中心。
三 客户端
新建客户端ms-configClient-base进行测试,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">
<parent>
<artifactId>ms-parent</artifactId>
<groupId>com.hjb.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../ms-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ms-configServer</artifactId>
<dependencies>
<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.properties如下:
#改变默认端口,默认为8080
server.port=8089
spring.application.name=config-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
bootstrap.properties如下:
#方式一:默认为http://localhost:8888,此种方式直接通过url进行寻址,无需进行服务注册及发现
#spring.cloud.config.uri=http://localhost:8088
spring.profiles.active=test
#方式二:服务注册发现,自动发现配置中心服务端
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
新建controller进行测试,如下:
package com.hjb.spring.cloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author haojingbin
* @Description:配置中心客户端controller
* @Date 2018/5/16
* @Modified By:
*/
@RestController
public class ConfigClientController {
@Value("${info.profile}")
private String profile;
@RequestMapping(value = "/getProfile",method = RequestMethod.GET)
public String getProfile(){
return profile;
}
}
新建目录config-client目录,新建如下文件:
加载顺序如下:application.properties--》config-client.properties--》config-client-profile.properties
如果有相同配置则后面覆盖前面的配置。spring.profile.active可配置多个,如下:
spring.profiles.active[0]=test spring.profiles.active[1]=dev
则后面dev会覆盖前面的test。
运行客户端后,请求http://localhost:8089/getProfile,就会得到相应的info.profile值。
具体代码参考:github