【从零基础到】学习入门微服务之Springcloud 【OpenFeign篇】

一. OpenFeign简介
微服务免不了服务的调用,在上一章Ribbon篇时用RestTemplate来进行服务调用。具体请参考:
微服务之Springcloud 从零基础到入门——Ribbon篇
但在使用时可以发现一个问题,那就是用RestTemplate调用服务时参数的传递过多,代码相对冗余复杂。Feign是一个声明式的REST客户端,它的目的就是让REST调用更加简单。OpenFeign是Spring Cloud 在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

二. 前置环境介绍
本例仍然使用Eureka集群,在注册中心已经部署了两个相同的服务CLOUD-PAYMENT-SERVICE,实现了服务的高可用。其中父项目已经对版本进行了统一的管理,spring cloud版本为Hoxton.SR1版,eureka和openfeign都为2.2.1版

三. 基于OpenFeign的服务消费者的创建
引入依赖,因为要从服务注册中心拉取服务,所以必须引入Eureka的依赖
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
编写application.yml文件,此处未将服务消费者注册到服务器上,因为它不需要被其他服务发现,只需要让OpenFeign去调用相应的服务即可。
server:
  port: 8604

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
1
2
3
4
5
6
7
8
编写主启动类
@SpringBootApplication
@EnableFeignClients    //开启feign的功能
public class Order8604Application {
    public static void main(String[] args) {
        SpringApplication.run(Order8604Application.class,args);
    }
}
1
2
3
4
5
6
7
编写Service接口来映射服务提供者的方法,只有加了@EnableFeignClients注解才能使用OpenFeign的相关功能,此处必须添加@Component注解,将其加入容器,否则会产生错误。该处代码表示将CLOUD-PAYMENT-SERVICE服务中提供的方法映射到本接口当中,其中访问路径、返回值类型、函数名、参数类型和个数都必须保持和服务提供者中的方法相同。(最好直接copy过来,然后删除方法体)
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface OrderService {
    @GetMapping("payment/findById/{id}")
    CommonResult<Payment> findById(@PathVariable("id") long id);
}
1
2
3
4
5
6
编写controller调用service服务,该处即为常规的controller代码,访问路径可以自己随意修改,只需要将刚刚的service接口注入进来即可。
@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("consumer/payment/findById/{id}")
    public CommonResult<Payment> findById(@PathVariable("id") long id){
        return orderService.findById(id);
    }
}
1
2
3
4
5
6
7
8
9
10
四. OpenFeign的日志打印功能
编写Openfeign的配置类
@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;  //日志类别为详细日志
    }
}
1
2
3
4
5
6
7
修改yml配置文件,将日志级别修改为自己需要的级别
logging:
  level:
    com.lk.springcloud.service.OrderService: debug
1
2
3
日志打印结果

五. 下一篇介绍
本文介绍了OpenFeign的基本使用,如对日志功能感兴趣的小伙伴可以自行参考官网进行学习,接下来将对微服务当中的熔断框架Hystrix进行介绍,感谢大家的阅读!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

www.mendeley.com/catalogue/e665245d-99ba-3dd6-a886-addc0df6cbc9/
www.mendeley.com/catalogue/db917dd7-b139-34e3-93cd-eaa05fa8341f/
www.mendeley.com/catalogue/7d7f871d-cdba-39a9-9fa5-cb4feef33926/
www.mendeley.com/catalogue/65b86c5b-be15-3e22-9df4-7cd7ec10160d/
www.mendeley.com/catalogue/caa0440d-81a8-3b4c-b616-ed4ad486f095/
www.mendeley.com/catalogue/71bc5c69-e783-385e-bc25-823d7654c26a/
www.mendeley.com/catalogue/5ea2f679-b0d7-3467-87e7-0caea23d49d6/
www.mendeley.com/catalogue/29986c41-e314-3da9-8491-1815c88885bb/
www.mendeley.com/catalogue/226806c8-31a4-3179-b504-eb7b0af2ec92/
www.mendeley.com/catalogue/2f877dc4-d833-330a-a6e5-33079eb07ba3/
www.mendeley.com/catalogue/3a7d56c9-d783-302f-9daa-d9d8982d9d2d/
www.mendeley.com/catalogue/ca027c01-2e93-3587-84e7-6084d2803df1/
www.mendeley.com/catalogue/f4fb1a34-c020-319c-bb28-2c7682a23ea8/
www.mendeley.com/catalogue/8a302f9d-ea3a-3282-8056-d5bf1fef62fa/
www.mendeley.com/catalogue/958f7f14-cc2d-3de3-887f-84421d2812a1/
www.mendeley.com/catalogue/98381728-e31c-3cf4-8424-18221c38986f/
www.mendeley.com/catalogue/0994aac0-6b33-3dfe-b4fb-f4a97e8b903d/
www.mendeley.com/catalogue/33ef06fe-e4e9-3bb6-a5a1-04a308105b80/
www.mendeley.com/catalogue/6a5ce199-4479-360d-84e6-7aaeb551e854/
www.mendeley.com/catalogue/bd4d6c00-8918-339e-8fe7-32fecb2e3989/
www.mendeley.com/catalogue/961a718e-0705-390c-b578-b449c851511d/
www.mendeley.com/catalogue/4eb9c132-104c-3775-9caa-53c1401e3d11/
www.mendeley.com/catalogue/601a1cf6-1a95-33c2-999b-34fd247286eb/
www.mendeley.com/catalogue/c4a084d0-bc65-33de-9ed1-e156c77dd2b4/
www.mendeley.com/catalogue/0be220aa-8c82-34e4-95dc-340fc0ea204c/
www.mendeley.com/catalogue/37f55154-da51-31fd-acd5-bb9ce8e26cf9/
www.mendeley.com/catalogue/8bbe83f9-9704-3162-bdfa-3c837c0cf31b/
www.mendeley.com/catalogue/4c389bf3-fad6-362b-9128-ec0a455aa4f5/
www.mendeley.com/catalogue/35409843-ad88-3864-82b6-d245a580aa02/
www.mendeley.com/catalogue/6c38cf24-2f74-3057-82e9-d12853e1cc0f/
www.mendeley.com/catalogue/020a3b87-4ec3-3de7-be04-1e225f5a32d0/
www.mendeley.com/catalogue/a210ef4d-d664-3360-8cff-efa8614407fe/
www.mendeley.com/catalogue/5ca2220b-8903-3678-a5b0-e40cc1975406/
www.mendeley.com/catalogue/183cc9cf-4e4a-3317-afc4-8e9eab878fed/
www.mendeley.com/catalogue/5869a6ec-3fe0-3766-b9ed-07e4077f5ca5/
www.mendeley.com/catalogue/3f4dc10b-1130-3208-b3dc-870e6dd40d96/
www.mendeley.com/catalogue/6b80bd67-3a48-31d7-bd73-c1effa4288bc/
www.mendeley.com/catalogue/fc9f591b-d54b-31a8-aacd-c87876020473/
www.mendeley.com/catalogue/5c5d9314-1289-311e-9127-650cc168efeb/
www.mendeley.com/catalogue/a16b2bee-d6cb-32e5-be60-5c02c377da5a/
www.mendeley.com/catalogue/94a6c7a1-182c-3a87-b88a-67aea5a78c2a/
www.mendeley.com/catalogue/9b315d2b-01fc-391a-a1d7-1274dc618e25/
www.mendeley.com/catalogue/e10c47a4-f7f1-3c0b-b034-f5510c14d73b/
www.mendeley.com/catalogue/62c9df4c-4517-30cb-90d9-ecb677615c3c/
www.mendeley.com/catalogue/7c4e55b4-9422-3527-a12f-fa3e957ff058/
www.mendeley.com/catalogue/66d7288d-929d-3ce8-b8e8-3e43e58c89f8/
www.mendeley.com/catalogue/ed4324b9-fb61-3fef-b76c-eab3d090e52e/
www.mendeley.com/catalogue/e5aa9c3e-cd3d-3c40-b8cc-1f13fa51f569/
www.mendeley.com/catalogue/aa21fa42-3029-3e21-8828-7a5c1c9a8f3a/
www.mendeley.com/catalogue/40a058f4-9295-32f7-8f55-0a5a570af065/
www.mendeley.com/catalogue/ba28f8a6-e623-399f-a156-0d91a2e451a7/
www.mendeley.com/catalogue/be29c836-9a85-36dc-ae4d-3e769666c33a/
www.mendeley.com/catalogue/1e78fc47-d9d4-30c5-b0f8-f40c3e1d3230/
www.mendeley.com/catalogue/6f224b2e-8ef7-3af1-a96c-faa330196afc/
www.mendeley.com/catalogue/91b14979-f357-36ea-8e2e-41be5e636c52/
www.mendeley.com/catalogue/6bcc9d6d-f434-3606-b1b5-fadd358aca1d/
www.mendeley.com/catalogue/579466c5-8641-3549-aa2b-4e267f935d65/
www.mendeley.com/catalogue/1ac1c190-cec8-3b49-8b62-653dcb8b76d2/
www.mendeley.com/catalogue/ae544437-540e-3e27-9a6c-38c4c82cab76/
www.mendeley.com/catalogue/bc99aa09-4d7f-32f5-8453-e1050eddbae5/
www.mendeley.com/catalogue/6c0e16ac-ff30-3235-9f12-dcfa1338254c/
www.mendeley.com/catalogue/17fd2fe0-5e03-3181-8789-d73a919ea117/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值