许久之前便听到了springcloud如雷贯耳的大名,但是不曾谋面,其主要应用于微服务的相关架构。笔者对微服务并不是很了解,但其既然比较出众,遂也稍微接触研究下
springcloud特性
springcloud作为spring团队的微服务架构,其有如下的特性(摘自官方文档)
- Distributed/versioned configuration(分布式/版本化配置)
- Service registration and discovery(服务注册与发现)
- Routing(路由)
- Service-to-service calls(服务间远程调用)
- Load balancing(负载均衡)
- Circuit Breakers(熔断器)
- Distributed messaging(分布式消息)
应该就是微服务的相关特性,笔者不对上述的概念进行阐述,读者可相应的查阅相关文档
springcloud config
springcloud板块下有很多的分支,根本看不过来,就挑笔者比较感兴趣的配置管理springcloud config作为分析的入口把。具体的使用以及概念本文就不展开了,在官方文档上都有,笔者喜欢从源码角度看下springcloud是如何整合springboot进行扩展的
MAVEN依赖
根据官方的例子,笔者最后还是通过IDEA开发工具引入Spring Initializr构建工具来生成springcloud config的相关maven配置
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<!--server也依赖client板块-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--client板块引入,如果只需要客户端,则只引入此依赖即可-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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/milestone</url>
</repository>
</repositories>
</project>
其依托于spring-boot-starter-parent板块进行构建,上述的简单配置其实引入了spring/springboot/springcloud三大板块及其相应依赖,具体就不展示了,读者可自行查看
config启动方式
可以分几种情况来启动相应的服务
linux方式启动
$ cd cloud-demo
$ ./mvnw spring-boot:run
window方式启动
> cd cloud-demo
> call mvnw.cmd spring-boot:run
IDEA方式启动,写一个启动类携带上**@EnableConfigServer**注解即可,例如
package com.example.cloud.app;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author nanco
* -------------
* cloud-demo
* -------------
* @create
* @descrption
**/
@SpringBootApplication
@EnableConfigServer
public class CloudConfigApp {
public static void main(String[] args) {
SpringApplicationBuilder appBuilder = new SpringApplicationBuilder(CloudConfigApp.class);
// load certain properties
appBuilder.properties("spring.config.name=configapp").run(args);
}
}
小结
笔者以此篇作为springcloud的开篇,并且也把springcloud config板块作为后续的了解突破口。但在深入应用服务之前,笔者觉得还是很有必要对cloud的context模块作下详细的分析,以方便后续的讲解。
来源:oschina
链接:https://my.oschina.net/u/4329213/blog/3690001