SpringCloud 微服务实战笔记

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 这是很早以前在我的博客上写的关于 SpringCloud 的一些实战笔记,现在我把这些实战笔记集合起来贴到这里,可能会对一些刚刚接触 SpringCloud 微服务的小伙伴有帮助。

这是很早以前在我的博客上写的关于 SpringCloud 的一些实战笔记,现在我把这些实战笔记集合起来贴到这里,可能会对一些刚刚接触 SpringCloud 微服务的小伙伴有帮助。


SpringBoot 构建项目



在我们使用传统的 spring 开发一个 web 应用程序通常会想到一些基本的需要:


  • web.xml 文件(配置 springMVC 的 DispatcherServlet,各种过滤器等等);
  • 启用了 springMVC 的 spring 配置文件;
  • mybatis 等数据库配置文件等。


以上的这些仅仅只是基本的需求,无论是开发一个大型项目或者只是一个 hello word 程序,都需要配置几乎同等的配置文件,既然这些都是通用的东西,那有什么东西可以把这些给自动配置了呢?这时候 springboot 的自动配置功能就派上用场了,

springboot 会为这些常用的配置进行自动配置。这些自动配置涉及很多方面,比如:java 持久化 api,各种 web 模板,springMVC 等等。


1. 起步依赖


平时我们使用 maven 创建一个 web 项目的时候,常常需要想项目需要哪些包,以及包的版本。但是在 springboot 创建 web 应用的时候,你只需你只需添加 springboot 的 Web 起步依赖(org.springframework.boot:spring-boot-starter-web)。它会根据依赖传递把其他所需依赖引入项目里面。


而其它你需要的功能,你只需要引入相关的的起步依赖即可。


2. 内嵌 Servlet 容器


其实 springboot 并不是一个应用服务器,它之所以可以运行 web 应用程序,是因为其内部已经内嵌了一个 Servlet 容器(Tomcat、Jetty 或 Undertow),其运行原理是把 web 应用直接打包成为一个 jar/war,然后这个 jar/war 是可以直接启动的,不需要另外配置一个 Web Server。相关的 embed 类就是它的依赖包。


3. Spring Initializr 构建 springboot 应用程序


本文使用的是 intellij idea 中的 Spring Initializr 工具创建 springboot 应用程序。

菜单栏中选择File=>New=>Project..,步骤大概是选择构建的工程类型,如:maven,Gradle;language 的选择;选择 Spring Boot 版本和起步依赖包等等。具体创建步骤这里就省略了。


640.png


spring boot 项目结构如图所示,整个项目结构遵循了 maven 项目的布局,主要的应用程序代码位于 src/main/java 目录里,资源都在 src/main/resources 目录里,测试代码则在 src/test/java 目录里。不同的是,web 页面模板移到 templates 了,我的项目现在主要用 thymeleaf 模板作为 web 页面。


在结构图你会发现一些与 springboot 密切项目的文件:


  • WebGatewayApplication.java:应用程序的启动引导类(bootstrap class),也是主要的 Spring 配置类;
  • application.properties:用于配置应用程序和 Spring Boot 的属性;
  • ReadingListApplicationTests.java:一个基本的集成测试类。
  • banner.txt:spring boot 应用程序启动时加载的文件。


3.1 启动引导 Spring


前面我们看到的 WebGatewayApplication.java 在 springboot 应用程序中主要有两个作用:配置和启动引导。而也是 Spring 的主要配置类。虽然 springboot 的自动配置免除了很多 Spring 配置,但你还需要进行少量配置来启用自动配置。


程序清单:

package com.crm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication  // 开启组件扫描和自动配置
public class WebGatewayApplication {
    public static void main(String[] args) {
      SpringApplication.run(WebGatewayApplication.class, args);// 启动引导应用程序
    }
}


3.2 配置应用程序属性


用 Spring Initializr 生成的 application.properties 文件只是一个空文件,它可以删除完全不影响应用程序的运行,但是,如果你想修改应用程序的属性,你就得在里面配置相关属性了,比如你在里面配置了 server.port=9010,嵌入式的 tomcat 服务器的监听端口就不是默认的 8080 了,变成了 9010。而且这个属性文件是自动被加载的。


这是我的项目 application.properties 属性配置:

###### MySQL配置
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://localhost:3306/crm?characterEncoding=UTF8
spring.datasource.username=zch
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.filters=stat
spring.datasource.maxActive=20
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
###### mybatis
mybatis.typeAliasesPackage=com.joosure.integral.cloud.pojo.cloud
mybatis.mapperLocations=classpath:mapper/*.xml
####### thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.check-template-location=true
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.excluded-view-names=
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.template-resolver-order=


3.3 构建过程解释


我的项目用的是 maven 作为构建工具,因此用 Spring Initializr 会生成 pom.xml 文件,这与创建普通的 maven 项目一样,代码清单如下:


<version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>crm</name>
  <description>crm-system</description>
  <parent> <!-- 从spring-boot-starterparent继承版本号 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies><!-- 起步依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!--web及模板引擎-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--数据库-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>
    <!--测试-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build><!-- 运行spring boot插件 -->
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
  • 其中 Artifact ID 为 spring-boot-starter-xxx 的都是 spring boot 起步依赖;
  • 构建插件的主要功能是把项目打包成一个可执行的超级 JAR(uber-JAR),包括把应用程序的所有依赖打入 JAR 文件内,并为 JAR 添加一个描述文件,其中的内容能让你用 java -jar 来运行应用程序;
  • Maven 构建说明中还将 spring-boot-starter-parent 作为上一级,这样一来就能利用 Maven 的依赖管理功能,继承很多常用库的依赖版本,在你声明依赖时就不用再去指定版本号了。


服务注册与发现



现在公司的积分联盟平台系统构建于公司内部的第 4 代架构中,而第 4 代就是 基于 SpringCloud 的微服务架构,趁着项目上手,花了几天研究了一下。


SpringCloud 是一个庞大的分布式系统,它包含了众多模块,其中主要有:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。也就是说微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如 RESTful API 的方式互相调用。


1. 创建服务注册中心


  • 在搭建 SpringCloud 分布式系统前我们需要创建一个注册服务中心,以便监控其余模块的状况。这里需要在 pom.xml 中引入:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  • 并且在 SpringBoot 主程序中加入@EnableEurekaServer 注解:
@EnableEurekaServer
@SpringCloudApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  • 接下来在 SpringBoot 的属性配置文件 application.properties 中如下配置:
server.port=9100
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

server.port 就是你指定注册服务中心的端口号,在启动服务后,可以通过访问http://localhost:9100服务发现页面,如下:640.png


2. 创建服务方


我们可以发现其它系统在这里注册并显示在页面上了,想要注册到服务中心,需要在系统上做一些配置,步骤跟创建服务注册中心类似,这里 web-gateway 系统做例子:

  • 首先在 pom.xml 中加入:
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
 </dependency>
  • 在 SpringBoot 主程序中加入@EnableDiscoveryClient 注解,该注解能激活 Eureka 中的DiscoveryClient实现,才能实现 Controller 中对服务信息的输出:
@EnableDiscoveryClient
@SpringBootApplication
public class WebGatewayApplication {
  public static void main(String[] args) {
    SpringApplication.run(WebGatewayApplication.class, args);
  }
}
  • 在 SpringBoot 的属性配置文件 application.properties 中如下配置:
spring.application.name=web-gateway
server.port=9010
eureka.client.serviceUrl.defaultZone=http://localhost:9100/eureka/
eureka.instance.leaseRenewalIntervalInSeconds=5

再次启动服务中心,打开链接:http://localhost:9100/,就可以看到刚刚创建的服务了。

640.jpg


服务消费者



在系统与系统之间,如何进行相互间的调用呢?也就是说怎么去调用服务提供的接口内容呢?这里就要说一下 Ribbon 了,Ribbon 是一个基于 http 和 tcp 客户端的负载均衡器。


下面我来简单介绍如何在 SpringCloud 分布式系统下使用 Ribbon 来实现负载均衡。


  • 首先在 pom.xml 中引入一下依赖:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>


  • 然后在 spring boot 主程序中创建 RestTemplate 类,并为它加上@LoadBalanced 注解开启负载均衡的能力:
@EnableDiscoveryClient
@SpringBootApplication
public class WebGatewayApplication {
  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
    return new RestTemplate();
  }
  public static void main(String[] args) {
    SpringApplication.run(WebGatewayApplication.class, args);
  }
}


RestTemplate 类是 Spring 用于构建 Restful 服务而提供的一种 Rest 服务可客户端,RestTemplate 提供了多种便捷访问远程 Http 服务的方法。


在 apllication.properties 配置文件中配置 eureka 服务,并注册到服务中心:

spring.application.name=integral-server
server.port=9600
eureka.client.serviceUrl.defaultZone=http://localhost:9100/eureka/


  • 在公司项目中正是通过 RestTemplate 来访问各个微服务提供的接口,比如在项目中要访问积分系统 integral-server,添加积分用户:
JSONObject integralServerResult = restTemplate.postForObject("http://integral-server/shop/add", RequestHandler.getRestRawRequestEntity(integralShopJson), JSONObject.class);


这样就可以调用 integral-server 系统的添加用户的接口实现在别的系统中添加用户了。


  • 我们也可以在 application.properties 配置文件中加入:
###### Ribbon
ribbon.ReadTimeout=60000


这个是设置负载均衡的超时时间的。

640.jpg


断路器



微服务架构中,各个系统被拆分成一个个服务单元,链路调用可能包括很多个服务单元,而每个单元又会个 N 个服务单元提供服务,因此如果有一个服务单元出现故障,就可能导致其它依赖此服务的服务单元出现延迟,导致整个微服务系统出现雪崩效应。


在 SpringCloud 模块中有一个叫 Netflix Hystrix 的断路器模块,就是专门解决这个问题而生的,Hystrix 是 Netflix 开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。

下面来说一下 Hystrix 在微服务系统中的具体用法:


  • 首先还是在 pom.xml 中加入以下依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>


  • 在 spring boot 主程序中加入@EnableCircuitBreaker 注解开启断路器模式:
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class WebGatewayApplication {
  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
    return new RestTemplate();
  }
  public static void main(String[] args) {
    SpringApplication.run(WebGatewayApplication.class, args);
  }


如果在调用过程中返回类似这样的响应:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat May 13 00:10:22 CST 2017
There was an unexpected error (type=Internal Server Error, status=500).
400 null


断路器也就开启了。


  • 我们也可以在 application.properties 配置文件中加入:
## hystrix
hystrix.commond.default.execution.isolation.thread.timeoutInMilliseconds=60000


这个设置可以更改返回错误响应的超时时间。


  • 如果不想返回默认的错误响应信息,我们还可以通过自定义来更改错误响应信息,我们需要一个类中注入一个 RestTemplate 类:
@Autowired
 RestTemplate restTemplate;


这个类在上面已经通过 Spring 创建好了,这里直接注入在类中即可,接下来我们在类中写一个方法:


@HystrixCommand(fallbackMethod = "addServiceFallback")
    public String addService() {
        return restTemplate.postForObject("http://integral-server/shop/add", RequestHandler.getRestRawRequestEntity(integralShopJson), JSONObject.class);
    }
    public String addServiceFallback() {
        return "error";
    }


当调用 integral-server 系统的添加接口超出延时的时间时,就会返回“error”。


服务网关



前面我们通过 Ribbon 实现服务的消费和负载均衡,但还有些不足的地方,举个例子,服务 A 和服务 B,他们都注册到服务注册中心,这里还有个对外提供的一个服务,这个服务通过负载均衡提供调用服务 A 和服务 B 的方法,那么问题来了,每个服务都变得有状态了,即每个服务都需要维护一套校验逻辑,这样会带来对外接口有污染。而且权限等不好集中管理,整个集群处于混乱之中。


最好的方法就是把所有请求都集中在最前端的地方,这地方就是 zuul 服务网关。

服务网关是微服务架构组件中处于最外一层,通过服务网关统一,可以将链路前端集中管理起来,除了具备服务路由、均衡负载功能之外,它还需要具备权限控制等功能。Spring Cloud Netflix 中的 Zuul 就担任了这样的一个角色,为微服务披上了一层保护层,也方便了权限校验集中管理,增加了接口的通用性。


640.jpg


1. 配置服务路由


  • 要使用 zuul,就要引入它的依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>


  • 在 spring boot 主程序中加入@EnableZuulProxy 注解开启 zuul:
@EnableEurekaClient
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class WebGatewayApplication {
  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
    return new RestTemplate();
  }
  public static void main(String[] args) {
    SpringApplication.run(WebGatewayApplication.class, args);
  }
}


  • 在 application.properties 配置文件中配置 zuul 路由 url:
spring.application.name=web-gateway
server.port=9010


到这里,一个微服务 zuul 服务网关系统已经可以运行了,接下来就是如何配置访问其它微服务系统的 url,zuul 提供了两种配置方式,一种是通过 url 直接映射,另一种是利用注册到 eureka server 中的服务 id 作映射:


  • url 直接映射:
zuul.routes.api-integral.path=/api-integral-url/**
zuul.routes.api-integral.url=http://localhost:8080/

以上规则意思是 /api-integral-url/** 的访问都会被路由到 http://localhost:8080/上。


  • 但是这么做必须得知道所有的微服务的地址,才能完成配置,这时我们可以利用注册到 eureka server 中的服务 id 作映射:
###### Zuul配置
zuul.routes.api-integral.path=/integral/**
zuul.routes.api-integral.serviceId=integral-server
zuul.routes.api-member.path=/member/**
zuul.routes.api-member.serviceId=member-server

integral-server 和 member-server 是这俩微服务系统注册到微服务中心的一个 serverId,我们通过配置,访问http://localhost:9010/integual/add?a=1&b=2,该请求就会访问 integral-server 系统中的 add 服务。


2. 服务过滤


在定义 zuul 网关服务过滤只需要创建一个继承 ZuulFilter 抽象类并重写四个方法即可,下面是 ZuulFilter 的一些解释:

  • filterType:过滤类型,具体如下:
    pre:请求路由之前执行;
    routing:请求路由时执行;
    post:在 routing 和 error 过滤器之后执行;
    error:在请求发生错误的时候执行;
  • filterOrder:定义过滤器的执行顺序
  • shouldFilter:判断该过滤器是否要执行,
  • run:过滤器的具体逻辑。


标准实例程序:

public class ErrFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }
    @Override
    public int filterOrder() {
        return 0;
    }
    @Override
    public boolean shouldFilter() {
        return true;
    }
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        Object accessToken = request.getParameter("accessToken");
        if(accessToken == null) {
            log.warn("access token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            return null;
        }
        return null;
    }
}


在自定过滤器之后,我们还需要在 SpringBoot 主程序中加入@EnableZuulProxy 注解来开启 zuul 路由的服务过滤:


@EnableZuulProxy
@EnableEurekaClient
@RibbonClients
@SpringCloudApplication
public class ApiGatewayApplication {
  public static void main(String[] args) {
    SpringApplication.run(ApiGatewayApplication.class, args);
  }
  @Bean
  PosPreFilter posPreFilter(){
    return new PosPreFilter();
  }


到这里,微服务系统的 zuul 路由功能基本搭建完成。

640.png


Feign



之前说过了微服务间,我是通过 Spring 的 RestTemplate 类来相互调用的,它可通过整合 Ribbon 实现负载均衡,但发现了这样写不够优雅,且不够模板化,因此本篇介绍一下 Feign。


Feign 是一种声明式、模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign 其实就是创建一个接口类,它跟普通接口没啥两样,因此通过 Feign 调用 HTTP 请求,开发者完全感知不到这是远程方法。


2. 整合 Feign


  • 添加 Feign 依赖:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>


  • 创建 一个 Feign 接口:
@FeignClient(value = FeignConst.COUPON_PROVIDER, url = "${feign.coupon.url:}")
public interface CouponClient {
  @GetMapping(value = "/coupon/list/page", headers = LocalsEncoder.CONTENT_TYPE_LOCALS_GET)
  RestResponse couponList(@ModelAttribute CouponCriteria criteria);
}


  • 启动 Feign 类
@EnableFeignClients(basePackages = {"com.objcoding"})
@SpringCloudApplication
public class ProviderApplication {
}


2. 服务降级


当网络不稳定时,一个接口响应非常慢,就会一直占用这个连接资源,如果长时间不做处理,会导致系统雪崩,幸好,Feign 已经继承了熔断器 Hystrix


@FeignClient(value = FeignConst.COUPON_PROVIDER, url = "${feign.coupon.url:}", fallback = CouponClient.CouponClientFallBack.class)
public interface CouponClient {
  @GetMapping(value = "/coupon/list/page", headers = LocalsEncoder.CONTENT_TYPE_LOCALS_GET)
  RestResponse couponList(@ModelAttribute CouponCriteria criteria);
  @Component
  class CouponClientFallBack implements CouponClient {
    @Override
    public RestResponse couponList(CouponCriteria criteria) {
      return RestResponse.failed("网络超时");
    }
  }
}


3. 拦截器


有时候微服务间的调用,需要传递权限信息,这些信息都包含在请求头了,这时我们可以通过 Feign 拦截器实现权限穿透:

@Configuration
public class WebRequestInterceptor {
  @Bean
  public RequestInterceptor headerInterceptor() {
    return template -> {
      ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      if (attributes == null) {
        return;
      }
      HttpServletRequest request = attributes.getRequest();
      Enumeration<String> headerNames = request.getHeaderNames();
      if (headerNames != null) {
        while (headerNames.hasMoreElements()) {
          String name = headerNames.nextElement();
          String values = request.getHeader(name);
          template.header(name, values);
        }
      }
    };
  }
}



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9天前
|
负载均衡 安全 Java
【微服务系列笔记】Gateway
Gateway是Spring Cloud生态系统中的网关服务,作为微服务架构的入口,提供路由、负载均衡、限流、鉴权等功能。借助于过滤器和路由器,Gateway能够动态地管理请求流量,保障系统的安全和性能。
34 7
|
9天前
|
Linux Docker 容器
【微服务系列笔记】Docker
docker是一种容器技术,它主要是用来解决软件跨环境迁移的问题和同一环境下依赖冲突问题。 Docker可以运行在Mac, Windows, linux等操作系统上,常用于适用于构建和部署分布式应用、微服务架构。
38 0
【微服务系列笔记】Docker
|
9天前
|
监控 Java 应用服务中间件
【微服务系列笔记】Sentinel入门-微服务保护
Sentinel是一个开源的分布式系统和应用程序的运维监控平台。它提供了实时数据收集、可视化、告警和自动化响应等功能,帮助用户监控和管理复杂的IT环境。本文简单介绍了微服务保护以及常见雪崩问题,解决方案。以及利用sentinel进行入门案例。
27 3
|
9天前
|
负载均衡 Java Apache
【微服务系列笔记】Feign
Feign是一个声明式的伪HTTP客户端,它使得HTTP请求变得更简单。使用Feign,只需要创建一个接口并注解。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。 OpenFeign 是SpringCloud在Feign的基础上支持了SpringMVC的注解。
36 8
|
9天前
|
存储 负载均衡 Cloud Native
【微服务系列笔记】Nacos
Nacos 是阿里巴巴开源的项目,用于构建云原生应用的动态服务发现、配置管理和服务管理平台。它支持动态服务发现、服务配置、服务元数据和流量管理,旨在更敏捷和方便地构建、交付和管理微服务平台。可作为注册中心与配置中心。
40 5
|
9天前
|
Nacos 微服务
【微服务系列笔记】Eureka
该文档介绍了微服务注册中心的重要性和流行选项,如Eureka、Nacos、Consul和Zookeeper,强调Eureka是唯一支持跨区域调用的AP系统。接着,它提供了一个Eureka入门案例,包括设置Eureka服务器和客户端的步骤,并展示了多实例部署的效果。最后,简要总结了学习Eureka的意义,并提出了几个思考问题,如Eureka的功能、工作原理以及其他服务发现技术。
26 5
|
9天前
|
负载均衡 算法 应用服务中间件
【微服务系列笔记】负载均衡
本文介绍了负载均衡的概念和重要性,指出随着流量增长,通过垂直扩展和水平扩展来提升系统性能,其中水平扩展引入了负载均衡的需求。负载均衡的目标是将流量分布到多台服务器以提高响应速度和可用性,常见的硬件和软件负载均衡器包括F5、A10、Nginx、HAProxy和LVS等。 文章接着提到了Ribbon,这是一个客户端实现的负载均衡器,用于Spring Cloud中。Ribbon在发起REST请求时进行拦截,根据预设的负载均衡算法(如随机算法)选择服务器,并重构请求URI。文中还介绍了如何通过代码和配置文件两种方式自定义Ribbon的负载均衡策略。
37 3
|
9天前
|
存储 Java 数据库
【微服务系列笔记】微服务概述
本文对比了单体应用和微服务架构。单体应用中所有功能模块在一个工程中,而微服务则按领域模型拆分为独立服务,每个服务有明确边界,可独立开发、部署和扩展。微服务允许使用不同语言和技术栈,每个服务有自己的数据库。微服务架构的优点包括易于开发维护、技术栈开放和错误隔离,但缺点包括增加运维成本、调用链路复杂、分布式事务处理困难以及学习成本高。实现微服务通常涉及SpringCloud等开发框架和Docker等运行平台。
34 2
|
11天前
|
JSON 安全 Java
微服务Token鉴权设计:概念与实战
【4月更文挑战第29天】在微服务架构中,鉴权是确保服务安全的重要环节。由于微服务往往由多个独立的服务组成,这些服务之间的通信需要一种高效、安全的鉴权机制。Token鉴权作为一种常用的鉴权方式,为微服务架构提供了简洁而有效的解决方案。
19 0
|
12天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
27 1