010 SpringCloud 学习笔记6-----Feign

1.概述

Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。

2.入门案例

2.1 服务提供方

user-service模块

 

 

复制代码
server:
  port: 8201
spring:
  application:
    name: user-service
  datasource:
      url: jdbc:mysql://localhost:3306/day1?serverTimezone=UTC&useSSL=false
      username: root
      password: ******
      driver-class-name: com.mysql.cj.jdbc.Driver
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:10086/eureka/
复制代码

(1)StudentController

复制代码
package com.hztest.controller;

import com.hztest.domain.CommonResult;
import com.hztest.domain.Student;
import com.hztest.domain.User;
import com.hztest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/getId")
    public String getId(@RequestParam(value="id") int id) {
        String idStr = studentService.getStudentId(id);
        return idStr;
    }

    @PostMapping("/getInfo")
    public String getInfo() {
        String info = studentService.getTestResult();
        return info;
    }
}
复制代码

(2)StudentService(接口)

package com.hztest.service;
public interface StudentService {

    String getStudentId(int id);

    String getTestResult();
}

(3)StudentServiceImpl.java

复制代码
package com.hztest.service.impl;
import com.hztest.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

    @Override
    public String getStudentId(int id) {
        return "user-service:"+id;
    }

    @Override
    public String getTestResult() {
        return "hello feign";
    }
}
复制代码

利用postman访问服务提供方

2.2 服务消费方

(1)导入依赖

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

(2)使用注解@EnableFeignClients启用feign客户端;

复制代码
package com.hztest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignServiceApplication {

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

}
复制代码

(3)使用注解@FeignClient 定义feign客户端

示例 : 该例子定义了一个feign客户端,将远程服务http://user-service/student/getId 映射为一个本地Java方法调用。

复制代码
package com.hztest.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@FeignClient(name = "user-service", path = "/student")
@Component
public interface StoreService {
    @PostMapping("/getInfo")
    String getInfo();

    @PostMapping("/getId")
    String getId(@RequestParam(value="id") int id);
}
复制代码

(4)使用注解@Autowired使用上面所定义feign的客户端 ;

复制代码
package com.hztest.controller;

import com.hztest.api.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/store")
public class StoreController {
    @Autowired
    StoreService storeService;

    //这里的使用本地Java API的方式调用远程的Restful接口
    @PostMapping("/getStuId")
    public String getStuId(int id) {
        String info = "feign-service调用"+storeService.getId(id);
        return info;
    }

    @PostMapping("/getStoreTest")
    public String getStoreTest() {
        String info = storeService.getInfo();
        return info;
    }

}
复制代码

利用postman访问服务调用方,服务调用方(feign-service)利用feign-client调用服务提供方(user-service)

 参考文献:https://www.jianshu.com/p/62d6c4779c01

posted @   雨后观山色  阅读(184)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示