2.初识springcloud
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性简化了分布式系统的开发,如服务发现、服务配置、负载均衡、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁等。
Spring Cloud包含的组件:
- Netflix Eureka:服务发现
- Netflix Ribbon:客户端负载均衡
- Netflix Hystrix:断路器
- Netflix Feign:基于接口的注解的 REST 客户端
- Spring Cloud Config:分布式配置管理
- Spring Cloud Bus:消息总线
- Spring Cloud Sleuth:日志收集
- Spring Cloud Stream:消息驱动的微服务
- Spring Cloud Task:短暂微服务
以下是一个简单的Spring Cloud示例,使用Eureka作为服务发现。
- 创建一个Maven项目作为服务注册中心(Eureka Server):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- application.properties配置:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
- 启动Eureka Server。
- 创建一个Spring Boot服务,并将其注册到Eureka Server:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.di
评论已关闭