微服务架构之spring cloud feign

在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是这篇文章要讲解的spring cloud feign,feign内部已经集成了ribbon,所以不用再单独引用,只需要引用spring cloud feign即可。

(一) 版本说明

a) Spring boot 2.0.6.RELEASE

b) Spring cloud Finchley.SR2

c) Java version 1.8

(二) 项目配置

1. 服务端项目配置

a) 服务端主要是提供服务功能,这里是把当前的端口返回给调用者,同时把自己注册到服务中心,调用者通过服务中心调用。

b) POM设置

复制代码
<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>
复制代码

c) application.yml配置文件

复制代码
eureka:

datacenter: ctm

environment: dev

instance:

hostname: 192.168.1.78

prefer-ip-address: true

ip-address: 192.168.1.129

lease-renewal-interval-in-seconds: 10

lease-expiration-duration-in-seconds: 30

instance-id: ${eureka.instance.ip-address}:${server.port}

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

fetch-registry: true

register-with-eureka: true

healthcheck:

enabled: true
复制代码

d) 主要参数说明

i. eureka.instance.prefer-ip-address 使用IP显示注册信息

ii. eureka.instance.ip-address 实例IP地址,

iii. eureka.instance.instance-id 自定义实例id,服务之间调用就是使用该配置,多个实例必须保证唯一性

iv. eureka.client.service-url.defaultZone 注册中心地址

e) 服务提供者API

复制代码
@Value("${server.port}")

private String getPort;

@GetMapping(name = "index", value = "/index")

public String Index() {

return getPort;

}
复制代码

2. feign消费端项目配置

a) POM设置

复制代码
<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>
复制代码

i. 这里把服务消费端也注册到了服务治理中心,消费者同时也是其它服务的提供者。

b) application.yml配置文件

复制代码
eureka:

datacenter: ctm

environment: dev

instance:

hostname: 192.168.1.78

prefer-ip-address: true

ip-address: 192.168.1.129

lease-renewal-interval-in-seconds: 10

lease-expiration-duration-in-seconds: 30

instance-id: ${eureka.instance.ip-address}:${server.port}

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

fetch-registry: true

register-with-eureka: true

healthcheck:

enabled: true
复制代码

c) feign服务消费者API

复制代码
@Configuration

public class FeignConfig {

@Bean

public Retryer feignRetryer(){

return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1),3);

}

}

@Component

public class FallBackService implements FeignService {

@Override

public String Index() {

return "hi,feign,error!";

}

}

@Component

@FeignClient(name = "DEMOSERVICEIMPL",configuration = FeignConfig.class,fallback = FallBackService.class)

public interface FeignService {

@GetMapping(value = "/index")

String Index();

}
复制代码

i. FeignClient feign客户端配置,这里配置了服务名称、重试规则、失败回调。

3. 重点提示

a) 为了演示负载,3个服务提供者必须设置3个不同的端口,并且其它相同,不然会被认为是不同的服务,起不到均衡的作用,这里跟ribbon一样

4. 项目运行

a) 运行服务提供者

i. 运行3个服务者实例后,会在服务中心看到如下效果,服务提供者已经注册成功

image

b) feign运行服务消费者

i. 运行消费者,如下图所示

clip_image004

c) 打开PostMan,输入消费者地址,多刷新几次,即可看到负载效果

clip_image006

clip_image007clip_image008

  这样spring cloud feign就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。

posted @   架构师聊技术  阅读(1645)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示