深入浅出SpringCloud

发布时间:2023-05-23

SpringCloud是一组微服务架构的框架,由Spring官方提供,它基于SpringBoot提供了一系列微服务治理工具,如服务注册与发现、配置中心、断路器、网关等等,使得微服务架构开发变得更加容易。下面将从不同的角度来介绍SpringCloud的一些关键概念和用法。

一、服务注册与发现

服务注册与发现是微服务最基本的功能之一,SpringCloud提供了Eureka和Consul两个注册中心。下面以Eureka为例,更深入地介绍如何使用服务注册和发现机制。

1. 注册Eureka Server

第一步是注册一个Eureka Server,我们可以在pom.xml中加入下面的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

然后,我们可以通过@Configuration来启用Eureka Server。下面是一个简单的实现:

@Configuration
@EnableEurekaServer
public class EurekaServerConfig {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerConfig.class, args);
    }
}

启动程序后,访问 http://localhost:8761 可以看到Eureka Server的控制台。

2. 注册Eureka Client

有了Eureka Server,我们可以在各自的服务中注册Eureka Client。下面是示例代码:

<!-- 添加Eureka Client 依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
// 在程序启动类上添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

启动Eureka Client程序后,我们可以在Eureka Server的控制台上看到它的注册信息。

二、配置中心

在微服务架构中,配置中心起着至关重要的作用。SpringCloud提供了Config Server与Config Client实现了分布式集中式配置的功能,下面我们来看一下如何使用。

1. 注册Config Server

第一步是还是加入SpringCloud Config Server的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

然后,我们可以通过@EnableConfigServer注解启用Config Server,下面是一个简单的实现:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

Config Server需要配置一个Git或SVN仓库。我们可以在application.properties或者application.yml文件中配置,如下面所示:

spring.cloud.config.server.git.uri=https://git.example.com/config-repo
spring.cloud.config.server.git.username=<username>
spring.cloud.config.server.git.password=<password>
spring.cloud.config.server.git.search-paths=config
spring.cloud.config.server.git.clone-on-start=true

2. 注册Config Client

接下来,我们需要在各个服务中注册Config Client。同样,我们先添加依赖,在程序启动类上添加@EnableConfigClient注解,示例代码如下:

<!-- 添加依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// 在程序启动类上添加@EnableConfigClient注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

接着,在bootstrap.properties或者bootstrap.yml文件中添加关于Config Server的配置,如下面所示:

server.port=8080
spring.cloud.config.uri=http://config-server.example.com
spring.application.name=myapp
spring.profiles.active=dev
spring.cloud.config.label=master

启动Config Client程序后,它会自动从Config Server中获取配置文件,并将配置文件中的属性注入到应用程序中。

三、断路器

断路器是为了处理微服务中的容错和弹性而出现,SpringCloud提供了Hystrix来实现断路器的功能。

1. 注册Hystrix Dashboard

在使用Hystrix之前,我们可以先搭建一个Hystrix Dashboard用于监控各个服务的运行情况。示例代码如下:

<!-- 添加Hystrix Dashboard依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
// 在程序启动类上添加@EnableHystrixDashboard注解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }
}

启动Hystrix Dashboard后,我们可以访问 http://localhost:8080/hystrix 查看Dashboard。

2. 使用断路器

在服务中使用Hystrix非常简单,我们只需要使用@EnableCircuitBreaker注解和使用@HystrixCommand注解即可实现,示例代码如下:

<!-- 添加Hystrix依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
// 在程序启动类上添加@EnableCircuitBreaker注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
    @Autowired
    private ServiceClient serviceClient;
    @HystrixCommand(fallbackMethod = "fallback")
    public String hello() {
        return serviceClient.hello();
    }
    public String fallback() {
        return "fallback message";
    }
}

在上面代码中,@HystrixCommand注解代表使用断路器的功能。如果ServiceClienthello方法发生异常,将会调用fallback方法返回fallback message

四、网关

在微服务架构中,网关类似于路由器的作用,它可以映射和重定向请求、调用相应服务,并将结果返回给客户端。

1. 注册Zuul Gateway

SpringCloud提供了Zuul来实现微服务的网关,我们可以在程序启动类上添加@EnableZuulProxy注解来启用Zuul Gateway,示例代码如下:

<!-- 添加Zuul依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
// 在程序启动类上添加@EnableZuulProxy注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

Zuul Gateway通过配置路由实现请求的转发和调用,可以在application.properties或者application.yml文件中配置路由,如下面所示:

zuul.routes.book.path=/app/**
zuul.routes.book.url=http://localhost:9000

上面代码中,/app/**路径的请求将被转发到 http://localhost:9000 服务。

2. 使用Zuul Redirect

使用Zuul重定向请求也非常简单,我们只需要在目标服务中使用@RestController注解和@RequestMapping注解,然后返回重定向地址即可,示例代码如下:

@RestController
@RequestMapping("/redirect")
public class RedirectController {
    @GetMapping
    public String redirect() {
        return "redirect:https://www.baidu.com";
    }
}

上面代码中,当我们访问 /redirect 路径时,将会被重定向到 https://www.baidu.com