深入浅出SpringCloud

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=
spring.cloud.config.server.git.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注解代表使用断路器的功能。如果ServiceClient的hello方法发生异常,将会调用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。

原创文章,作者:UDDLQ,如若转载,请注明出处:https://www.506064.com/n/370171.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
UDDLQUDDLQ
上一篇 2025-04-18 13:40
下一篇 2025-04-18 13:40

相关推荐

  • 深入浅出统计学

    统计学是一门关于收集、分析、解释和呈现数据的学科。它在各行各业都有广泛应用,包括社会科学、医学、自然科学、商业、经济学、政治学等等。深入浅出统计学是指想要学习统计学的人能够理解统计…

    编程 2025-04-25
  • 深入浅出torch.autograd

    一、介绍autograd torch.autograd 模块是 PyTorch 中的自动微分引擎。它支持任意数量的计算图,可以自动执行前向传递、后向传递和计算梯度,同时提供很多有用…

    编程 2025-04-24
  • 深入浅出SQL占位符

    一、什么是SQL占位符 SQL占位符是一种占用SQL语句中某些值的标记或占位符。当执行SQL时,将使用该标记替换为实际的值,并将这些值传递给查询。SQL占位符使查询更加安全,防止S…

    编程 2025-04-24
  • 深入浅出:理解nginx unknown directive

    一、概述 nginx是目前使用非常广泛的Web服务器之一,它可以运行在Linux、Windows等不同的操作系统平台上,支持高并发、高扩展性等特性。然而,在使用nginx时,有时候…

    编程 2025-04-24
  • 深入浅出ThinkPHP框架

    一、简介 ThinkPHP是一款开源的PHP框架,它遵循Apache2开源协议发布。ThinkPHP具有快速的开发速度、简便的使用方式、良好的扩展性和丰富的功能特性。它的核心思想是…

    编程 2025-04-24
  • 深入浅出arthas火焰图

    arthas是一个非常方便的Java诊断工具,包括很多功能,例如JVM诊断、应用诊断、Spring应用诊断等。arthas使诊断问题变得更加容易和准确,因此被广泛地使用。artha…

    编程 2025-04-24
  • 深入浅出AWK -v参数

    一、功能介绍 AWK是一种强大的文本处理工具,它可以用于数据分析、报告生成、日志分析等多个领域。其中,-v参数是AWK中一个非常有用的参数,它用于定义一个变量并赋值。下面让我们详细…

    编程 2025-04-24
  • 深入浅出Markdown文字颜色

    一、Markdown文字颜色的背景 Markdown是一种轻量级标记语言,由于其简单易学、易读易写,被广泛应用于博客、文档、代码注释等场景。Markdown支持使用HTML标签,因…

    编程 2025-04-23
  • 深入浅出runafter——异步任务调度器的实现

    一、runafter是什么? runafter是一个基于JavaScript实现的异步任务调度器,可以帮助开发人员高效地管理异步任务。利用runafter,开发人员可以轻松地定义和…

    编程 2025-04-23
  • 深入浅出TermQuery

    一、TermQuery概述 TermQuery是Lucene中最基本、最简单、最常见的查询方法之一。它完全符合其名字,意味着只能对一个单词进行查询。 TermQuery可以用于搜索…

    编程 2025-04-23

发表回复

登录后才能评论