许久之前便听到了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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<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三大板块及其相应依赖,具体就展示了,读者可自行查看
springcloud config 启动
官方文档上提供了命令方式的启动
linux方式启动
$ cd spring-cloud-demo
$ ./mvnw spring-boot:run
window方式启动
> cd spring-cloud-demo
> call mvnw.cmd spring-boot:run
IDEA方式启动,直接运行
ConfigServerApplication
启动类(springcloud包内的启动类,读者可下载源码来运行)
package org.springframework.cloud.config.server;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigServerApplication.class)
.properties("spring.config.name=configserver").run(args);
}
}
小结
笔者以此篇作为springcloud的开篇,后续将以springcloud config的源码作为分析的源头,慢慢去管中窥豹下它的魅力
原文地址:https://www.cnblogs.com/question-sky/p/10215903.html
时间: 2024-10-29 14:51:11