Spring Cloud 微服务实践(二)
发布日期:2021-05-08 01:51:19 浏览次数:22 分类:精选文章

本文共 3352 字,大约阅读时间需要 11 分钟。

Spring Cloud 学习指南


第七章:费恩 - 声明式服务消费 Feign

7-1. 费恩是什么?

费恩(Feign)是Netflix公司开发的一个声明式的REST调用客户端。它基于Spring Cloud的Ribbon和Hystrix组件,简化了微服务开发中的负载均衡和服务熔断配置。费恩的目标是为Spring Boot应用提供一个简洁的、声明式的Web服务客户端定义方式,使开发者无需手动管理RESTful服务的配置。

费恩的核心优势在于其与Ribbon和Hystrix的无缝集成,以及对配置的极大简化。通过@FeignClient注解,开发者可以轻松定义远程服务接口,并自动获得负载均衡和服务熔断功能。


7-2. 使用费恩实现消费者

1. 创建Spring Boot项目

创建一个名为05-springcloud-service-feign的普通Spring Boot项目。

2. 添加依赖

在项目的pom.xml中添加以下依赖:

org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-feign
1.4.5.RELEASE
org.springframework.cloud
spring-cloud-starter-hystrix
1.4.5.RELEASE

3. 配置入口类

在入口类上添加@EnableFeignClients注解,启用费恩的支持功能。

4. 定义服务接口

创建一个HelloService接口,通过@FeignClient注解指定服务名称并绑定服务提供者:

package com.z.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("01-springcloud-service-provider")
public interface HelloService {
@RequestMapping("/s/hello")
public String hello();
}

5. 初始化服务消费者

application.properties中配置服务注册中心地址和端口:

server.port=8082
eureka.client.service-url.defaultZone=http://eureka8761:8761/eureka/,http://eureka8762:8762/eureka/

6. 测试服务消费

启动注册中心、服务提供者和费恩实现的服务消费者,通过浏览器访问http://localhost:8082/web/hello验证服务是否正常调用的。


第八章:API网关 Zuul

8-1. Spring Cloud Zuul是什么?

Spring Cloud Zuul是基于Netflix Zuul实现的API网关解决方案。它提供了路由、过滤、负载均衡和权限验证等功能,使开发者能够轻松构建和管理分布式系统的API接口。

Zuul作为API网关,起到了类似于“安检站”的作用,负责接收并转发外部请求。它能够统一管理多个服务的接口,实现请求的分发和调度,同时支持过滤器链进行业务逻辑处理。


8-2. 使用Zuul构建API

1. 创建Spring Boot项目

创建一个名为06-springcloud-api-gateway的普通Spring Boot项目。

2. 添加依赖

在项目的pom.xml中添加以下依赖:

org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client

3. 配置入口类

在入口类上添加@EnableZuulProxy注解,启用Zuul的API网关功能。

4. 配置路由规则

application.properties中配置路由规则:

server.port=8080
zuul.routes.api-wkcto.path=/api-wkcto/**
zuul.routes.api-wkcto.serviceId=05-springcloud-service-feign
eureka.client.service-url.defaultZone=http://eureka8761:8761/eureka/,http://eaura8762:8762/eureka/

第九章:Spring Cloud Config

9-1. Spring Cloud Config是什么?

Spring Cloud Config是一个分布式的配置管理中心,提供了对多个微服务应用的统一配置文件管理功能。它支持通过Git仓库存储配置文件,并通过API接口提供动态配置内容。

Spring Cloud Config由Server和Client两部分组成。Server部分负责存储和管理配置文件,Client部分通过接口从Server获取配置信息,并应用于应用程序。


9-2. 搭建Spring Cloud Config配置中心

1. 创建配置中心服务端

创建一个名为07-springcloud-config-server的Spring Boot项目,并添加以下依赖:

org.springframework.cloud
spring-cloud-config-server

application.properties中配置Git仓库信息:

server.port=3721
spring.application.name=07-springcloud-config-server
spring.cloud.config.server.git.uri=https://github.com/myspring/spring-cloud-config.git
spring.cloud.config.server.git.search-paths=config-center
spring.cloud.config.server.git.username=your_username
spring.cloud.config.server.git.password=your_password

2. 创建配置文件

在GitHub上创建一个名为spring-cloud-config的仓库,并在config-center目录下添加四个配置文件:

  • application.properties
  • application-dev.properties
  • application-test.properties
  • application-online.properties

总结

通过以上内容,可以看到Spring Cloud框架在微服务开发中的核心功能。从服务消费者到API网关,再到配置中心,每个组件都为分布式系统的构建提供了强有力的支持。

上一篇:Mybatis解析(一)
下一篇:RestTemplate的使用

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月01日 06时16分49秒