【快速使用Feign调用】

0、概述

必备知识:【快速搭建Eureka注册中心】
两个服务:

  • feign-eureka-hello-server:服务提供者,以下简称 hello-server
  • feign-eureka-hello-client:服务调用者,以下简称 hello-client

达到的效果:

  • hello-client 调用 hello-server 的函数接口,输出被调用的 hello-server 的信息。

1、创建 hello-server


和正常创建普通服务一样,但是需要向 Eureka 注册中心注册服务,让hello-client 能够找到该服务

@SpringBootApplication
@EnableDiscoveryClient //注册服务
@RestController
public class HelloServerApplication {
	@Autowired
	DiscoveryClient client;

	@RequestMapping("/")
	public String hello() {
		// 获取所有的 HelloServer 服务
		List<ServiceInstance> instances = client.getInstances("HelloServer");
		// 随机选择一个 HelloServer 服务
		ServiceInstance selectedInstance = instances
				.get(new Random().nextInt(instances.size()));
		// 返回选中的 HelloServer 服务实例信息
		return "Hello World: " + selectedInstance.getServiceId() + ":" + selectedInstance
				.getHost() + ":" + selectedInstance.getPort();
	}

	public static void main(String[] args) {
		SpringApplication.run(HelloServerApplication.class, args);
	}
}

配置文件 application.yaml

spring:
  application:
    name: HelloServer

server:
  port: 7111

eureka:
  password: password
  client:
    serviceUrl:
      defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}

2、创建 hello-client


相比普通的Eureka Client, 仅仅增加了 @EnableFeignClients 注解,其他的配置没有变化。

在 Eureka Client 的基础上,引入 OpenFeign 依赖:

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


编写接口 @RequestMapping("/") 调用 HelloServer 对应的接口:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class HelloClientApplication {
	@Autowired
	HelloClient client;

	@RequestMapping("/")
	public String hello() {
		// 调用 HelloServer 对应路径的接口
		return client.hello();
	}

	public static void main(String[] args) {
		SpringApplication.run(HelloClientApplication.class, args);
	}

	/**
	 * 绑定服务 HelloServer
	 */
	@FeignClient("HelloServer")
	interface HelloClient {
		/**
		 * 该请求路径对应 HelloServer 中的请求路径
		 * @return
		 */
		@RequestMapping(value = "/", method = GET)
		String hello();
	}
}

配置文件 application.yaml

spring:
  application:
    name: HelloClient

server:
  port: 7211

eureka:
  password: password
  client:
    serviceUrl:
      defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}

endpoints:
  restart:
    enabled: true

3、验证

访问 http://localhost:7211 , 向 hello-client 发送请求,hello-client 再向 hello-server 发送请求,调用对应的方法。
最后页面响应:Hello World: HELLOSERVER:192.168.43.17:7111,输出 hello-server 的信息。

4、参考资料

[1] 项目地址:https://github.com/spring-cloud-samples/feign-eureka

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Feign进行服务之间的调用时,有时会遇到调用超时的情况。调用超时通常是由于网络延迟、目标服务负载过高或者调用方设置的超时时间过短所引起的。然而,即使发生调用超时,仍然可以通过一些处理方式来保证调用的成功。 首先,可以通过适当调整Feign的超时设置来解决调用超时的问题。可以增加Feign的超时时间,以便给目标服务更多的时间来响应请求。这样,就有更多的机会使调用在超时之前完成,并成功处理返回结果。通过调整超时时间,可以适应不同场景下的调用耗时需求,以确保调用的成功与稳定。 其次,可以通过使用断路器来处理调用超时的情况。断路器可以将调用超时的请求直接快速失败,而不是一直等待超时结束。通过断路器的配置,可以设置一个适当的超时时间阈值,当调用超过这个时间后就会触发断路器的降级操作。在断路器触发后,可以通过提供默认结果或者进一步处理异常来保证调用的成功。 最后,可以结合重试策略来处理调用超时。当一个调用发生超时时,可以通过重新发起调用的方式进行重试。通过合理设置重试次数和间隔时间,可以增加调用成功的机会。同时,可以结合断路器来避免无限制的重试,以保护系统的稳定性。 总结而言,当Feign调用超时但是处理成功时,可以通过调整超时时间、使用断路器和结合重试策略等方式来保证调用的成功。这些处理方式可以根据实际情况进行灵活选择,以提高系统的稳定性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值