springcloud实战:服务间通信——OpenFeign - Go语言中文社区

springcloud实战:服务间通信——OpenFeign


Spring Cloud OpenFeign

Spring Cloud OpenFeign是一个声明式的 HTTP客户端,它简化了HTTP客户端的开发,使编写Web服务的客户端变得更容易。使用Spring Cloud OpenFeign,只需要创建一个接口并注解,就能很容易地调用各服务提供的HTTP接口。Spring Cloud OpenFeign基于OpenFeign实现,它除了提供声明式的 HTTP客户端外,还整合了Spring Cloud Hystrix,能够轻松实现熔断器模型。

springcloud实战:服务间通信——OpenFeign

 

Spring Cloud对OpenFeign进行了增强,使得Spring Cloud OpenFeign支持Spring MVC注解。同时,Spring Cloud整合了Ribbon和 Eureka,这让 Spring Cloud OpenFeign的使用更加方便。

Spring Cloud OpenFeign能够帮助我们定义和实现依赖服务接口。在Spring Cloud OpenFeign的帮助下,只需要创建一个接口并用注解方式配置它,就可以完成服务提供方的接口绑定,减少在使用Spring Cloud Ribbon时自行封装服务调用客户端的开发量。

springcloud实战:服务间通信——OpenFeign

 

下面介绍如何在应用中集成Spring Cloud OpenFeign。

(1)在common工程中添加如下依赖:

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

(2)在public工程的启动类PublicApplication.java中加入@EnableFeignclients注解,启用OpenFeign功能:

@MapperScan(basePackages = "com. lynn.blog.pub.mapper")
@EnableFeignclients(basePackages = "com. lynn.blog")
public class PublicApplication extends Application{
}

由于每个工程的基础包名都不一致,如user 工程包名为com.lynn.blog.user,public工程包名为com.lynn.blog.pub,这里需要指定basePackages为 com. lynn.blog,以保证Spring容器可以扫描到OpenFeign注入的类(如果不指定basePackages,则默认扫描加入该注解的类所在包及其子包)。

(3)创建TestserviceFeign接口,并编写以下代码:

@FeignClient(value = "test")
public interface TestServiceFeign {
@RequestMapping("/test")
String test();
}

其中,@Feignclient("test")表示该接口是一个OpenFeign的HTTP客户端,注解内指定服务名,本示例指定test,即 test工程下spring.application.name配置的值。接口定义只需和 test工程的控制器提供的接口一致(参数名、返回值和接口地址)即可,需要注意的是,@RequestMapping指定的地址为接口地址全路径。

(4)创建控制器验证 OpenFeign的正确性。

@RequestMapping("feign")
@RestController
public class TestControllerFeign {
@Autowired
TestServiceFeign testServiceFeign;@RequestMapping( "test")
private String test(){
return testServiceFeign.test();
}
}

(5)重启 comment工程,浏览器多次访问地址 http://localhost:8203/feign/test,可以看到分别打印了9999和9998端口。

通过上述示例,我们发现 OpenFeign 使代码变得更加优雅,无须使用 RestTemplate 显式地调用HTTP服务,只需要指定想要调用的服务名即可。由于OpenFeign 内部集成了Ribbon,所以它也默认拥有了负载均衡能力。

springcloud实战:服务间通信——OpenFeign

 

自定义OpenFeign 配置

OpenFeign提供了默认的配置类FeignclientsConfiguration,该类使用了默认的编码器( encoder )、解码器( decoder )、合约( contract)等。因为OpenFeign 的核心是HTTP客户端,HTTP传输是通过数据包(流)进行的,所以在发送请求、接受响应的过程中,有必要对数据进行编码和解码。而OpenFeign默认使用的合约是SpringMvcContrace,它表示OpenFeign可以使用Spring MVC的注解,即上一节代码编写的@RequestMapping。

Spring Cloud OpenFeign 允许通过@Feignclient注解的configuration属性编写自定义配置,自定义配置会覆盖默认的配置。

接下来,我们以覆盖合约为例,讲解自定义配置的编写。

创建一个 OpenFeign的配置类MyFeignconfiguration,其代码如下:

@SpringBootConfiguration
public class MyFeignConfiguration {
@Bean
public Contract feigncontract()i
return new feign.contract.Default();
}
}

上述代码定义了一个Bean方法,返回Contract,即 OpenFeign合约,该合约返回的是OpenFeign的默认合约,这样我们就可以使用OpenFeign的注解而不用Spring MVC注解。

修改TestServiceFeign接口,将@RequestMapping 注解替换成@RequestLine 注解,并通过configuration属性指定自定义配置,如:

@Feignclient(value = "test",fallback = TestserviceErrorFeign.class,configuration =
MyFeignConfiguration.class)
public interface TestServiceFeign {
@RequestLine("GET /test")
String test();
}

需要注意的是,@RequestLine的属性值需要指明请求方法,比如上述代码指定请求方法为GET,如果不指定启动工程将报错。在@Feignclient注解的configuration属性中指定MyFeignConfiguration类,这样OpenFeign就会覆盖其默认配置而使用我们自定义的配置。

本文给大家讲解的内容是springcloud实战:服务间通信,SpringCloudOpenFeign

  1. 下篇文章给大家讲解的是springcloud实战:服务间通信,OpenFeign熔断;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/python8989/article/details/113705268
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-30 22:02:35
  • 阅读 ( 841 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢