springCloud学习笔记(五)——OpenFeign服务调⽤映射

一、OpenFeign的作用和含义

      在使用restTemplate访问远程接口的时候,我们难以将接口管理起来,当接口变动的时候我们可能会修改多处。Spring Cloud 提供OpenFeign来解决这个问题。本文将通过配置OpenFeign来访问远程服务。OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。  



二、OpenFeign的使用



1.引入库


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



2.application配置文件内容

server:
  port: 82


eureka:
  instance:
    hostname: eureka7001.com   #eureka服务端的实例名称
    instance-id: clientOpenFeign82  #服务中心中具体区分的名称
    prefer-ip-address: true  #访问路径可以显示IP地址
    lease-renewal-interval-in-seconds: 1  #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除
  client:
    register-with-eureka: true   #false表示不向注册中心注册自己
    fetch-registry: true   #false表示自己端就是注册中心
    service-url:
      #      defaultZone: http://eureka7001.com:7001/eureka/   #单机
      defaultZone:  http://eureka7001.com:7001/eureka/  #集群
  server:
    # 关闭自我保护机制,保证不可用服务被及时剔除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

# 连接超时时间设置为3s
ribbon:
  ReadTimeout: 6000
  ConnectionTimeout: 6000
#注册服务中心得名字
spring:
  application:
    name: cloud-eureka-openfeign
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://101.34.49.127:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: 数据库用户名
    password: 数据库密码

mybatis:
  mapper-locations: classpath:mapper/*.xml

3.main方法声明使用feign组件

@SpringBootApplication
//声明使用feign组件
@EnableFeignClients
public class CloudEurekaOpenfeignApplication82 {

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

}

4.Controller控制层代码

@RestController
public class PaymentController {

    @Resource
    Payment82Service payment82Service;

    /***
     * description: 通过id查询Payment数据
     * version: 1.0 ->
     * date: 2021/11/17 13:55
     * author: xiaYZ
     * iteration: 迭代说明
     * @param id
     * @return com.example.comment.entity.CommonResult<com.example.comment.entity.Payment>
     */
    @GetMapping("findPaymentById/{id}")
    public CommonResult<Payment> findPaymentById(@PathVariable("id") Long id){
        CommonResult<Payment> commonResult = new CommonResult<>();
        try{
            commonResult = payment82Service.findPaymentById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return commonResult;
    }
}

5.service层代码

/**
 * @description: 连接CLOUD-EUREKA-SERVICE注册服务
 * @author: xiaYZ
 * @createDate: 2021/11/17
 * @version: 1.0
 */
@Component
@FeignClient(value = "CLOUD-EUREKA-CLIENT",path = "paymentController")
public interface Payment82Service {

    /**
     * description: 通过id查询Payment数据
     * version: 1.0
     * date: 2021/11/17 14:38
     * author: xiaYZ
     * iteration: 迭代说明
     * @param id
     * @return 
     */
    @GetMapping(value = "findPaymentById/{id}")
    CommonResult<Payment> findPaymentById(@PathVariable("id") Long id);
}
  1. @FeignClient注解说明连接服务的名称,服务名称为CLOUD-EUREKA-CLIENT
  2. path代表统一前缀,即提供服务的路径为paymentController/findPaymentById/id
  3. 若无CommonResult类可用实体类,或String

6.测试结果

 

如图调用82端口返回数据,此数据其实是 CLOUD-EUREKA-CLIENT服务提供的

项目地址: https://gitee.com/xyz1041221997/springcloud.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值