基于 Spring Cloud 的微服务架构实践指南(下)

show me the code and talk to me,做的出来更要说的明白
本文源码,请点击 learnSpringCloud
我是布尔bl,你的支持是我分享的动力!

一、引入

上回 基于 Spring Cloud 的微服务架构实践指南(上) 介绍了 Spring Cloud 的常见组件,我们接着继续进入 Spring Cloud 的实战教程,撸起袖子,真枪实弹干一场。在实战演练中感受一下 Spring Cloud 的魅力所在。在教程中,我会继续将 Spring Cloud 常见组件进行整合。整个过程就像搭积木一样,一点一点地完成一个微服务工程的搭建。实战演练是比较繁琐的,但是只要我们真正地去做了,就会收获很多。

二、hystrix 组件( 服务熔断 )

hystrix 组件主要作用是服务熔断以及服务降级。可以在我们犯错的时候,再给我们一次机会。他就像家里的短路开关,对程序起到保护作用。另一方面其实觉得和 java的异常机制相似。当项目发生未知异常, hystrix 组件就会挺身而出,作为项目的贴身保镖,为项目保驾护航。当然,你认我的代码没有bug,那么你可以把他放在一边。另外hystrix 组件提供了一个监控功能,但是没有图形化,我们可以使用相关依赖引入图像化界面。

2.1 pom 文件

我们引入 hystrix的依赖。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>

2.2 yml 文件

引入必要模块后,我们就要去配置 yml 文件了。

server:
  port: 8010 # 端口
  
spring:
  application:
    name: microservicloud-hystrix # 给模块起一个名字
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka  # 注册中心地址
  instance:
    instance-id: microservicloud-hystrix-8010
    prefer-ip-address: true

2.3 启动类

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker  //启动断路器
@EnableDiscoveryClient // 启动图像化监控
@RestController
public class AppApllcation8005 {
    public static void main( String[] args ) {
        SpringApplication.run(AppApllcation8005.class, args);
    }
    @GetMapping("/hellohystrix")
    @HystrixCommand(fallbackMethod = "fallback") // 发生异常执行响应方法
    public String hystrix() {
        int i = 1 / 0;
        return "hellohystrix";
    }

    public String fallback() {
        return "出错了";
    }

}

2.4 启动效果

启动注册中以及 hystrix 组件项目。

访问 http://localhost:8010/hellohystrix 接口:

访问 http://localhost:8010/hystrix 接口:

三、zuul组件(服务网关)

zuul 组件主要是提供路由与过滤器功能。一般作为项目的大门守卫,对所有进入项目的接口进行检查。就像地铁的安保人员一样,会对每一个进入地铁的人员进行一一 的检查,发现不符合地铁管理条例的人员不予进入。这就是过滤功能,同时当你迷路的时候,你可以询问安保人,他会为你指导方向,这就是路由功能。

加入服务网关的项目架构

3.1 pom 文件

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

3.2 yml 文件

server:
  port: 8005 # 端口

spring:
  application:
    name: microservicloud-zuul-gateway # 项目名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka # 注册中心

3.3 启动类

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy // 启动zuul 组件
public class AppApllcation8005 {
    public static void main( String[] args ) {
         SpringApplication.run(AppApllcation8005.class, args);
    }
}

3.4 启动效果

访问 http://localhost:8005/microservicloud-dept/list 接口:

可以看到我们通过网关地址就访问到了服务端接口。这就是服务网关的路由功能。

四、config组件(配置中心)

当项目越来越多,伴随的配置文件也越来越多。我们是否可以将这次杂乱无章的文件统一进行管理呢,答案是可以的。这就是 config 组件的作用。config 组件主要是利用 git 作为配置服务站,实现文件统一管理。当配置文件更新的时候,我们就可以拉去最新的文件。

五、github

https://github.com/buerbl/learnSpringCloud

六、关注微信公众号,随时移动端阅读

公众号.jpg

原文地址:https://www.cnblogs.com/chenzhuantou/p/12696222.html