SpringCloud搭建_2.Feign

Feign

一、搭建提供者模块(普通EurekaClient)

  1. 右击父工程,在父工程下创建Feign模块

SpringCloud搭建_2.Feign
SpringCloud搭建_2.Feign
SpringCloud搭建_2.Feign

  1. 在启动类上添加@EnableEurekaClient

SpringCloud搭建_2.Feign
3. 配置application.yml

server:
  port: 8001

#指定当前eureka客户端注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka

# 指定应用名称
spring:
  application:
    name: provider
  1. 写一个Controller(对外提供接口服务)

SpringCloud搭建_2.Feign

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description TODO
 * @author cuiyingfan
 * @date 2021/8/4 16:38
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Value("${server.port}")
    String port;

    @GetMapping("/sayHello")
    public String sayhello() {
        return "I`m provider " + port + " ,Hello consumer!";
    }

    @GetMapping("/sayHi")
    public String sayHi() {
        return "I`m provider " + port + " ,Hi consumer!";
    }

    @GetMapping("/sayHaha")
    public String sayHaha() {
        return "I`m provider " + port + " ,Haha consumer!";
    }
}
  1. 服务提供者已经搭建完成,启动项目测试一下

SpringCloud搭建_2.Feign

访问 http://localhost:8001/user/sayHi 查看
SpringCloud搭建_2.Feign

二、搭建消费者模块(Feign)

  1. 依旧父工程下新建模块
    SpringCloud搭建_2.Feign
    SpringCloud搭建_2.Feign
  2. 启动类上添加@EnableEurekaClient 和 @EnableFeignClients
    SpringCloud搭建_2.Feign
  3. 修改application.yml
server:
  port: 8003

#指定当前eureka客户端注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka

# 指定应用名称
spring:
  application:
    name: consumer
  1. 写一个跟服务提供者模块相同url的接口类
    SpringCloud搭建_2.Feign
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @ClassName UserApi
 * @Description TODO
 * @author cuiyingfan
 * @date 2021/8/4 16:55
 * @Version 1.0
 */
@FeignClient("provider")
public interface UserApi {
    @GetMapping("user/sayHello")
    String sayhello();

    @GetMapping("user/sayHi")
    String sayHi();

    @GetMapping("user/sayHaha")
    String sayHaha();
}
  1. 写一个消费者自己的controller去调用提供者提供的接口
    SpringCloud搭建_2.Feign
import com.ivan.user.api.UserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description TODO
 * @author cuiyingfan
 * @date 2021/8/4 17:01
 * @Version 1.0
 */
@RestController
public class UserController {
    @Autowired
    private UserApi userApi;

    @GetMapping("user")
    public String show() {
        return userApi.sayHaha();
    }
}
  1. 再复制出一个服务提供者模块(记得修改端口 -Dserver.port=8002),并启动所有模块
    SpringCloud搭建_2.Feign

启动后共4个模块,Eureka注册中心、2个服务提供者、1个消费者

SpringCloud搭建_2.Feign
7. 访问消费者对外提供的接口 http://localhost:8003/user ,发现消费者通过Ribbon负载均衡轮询调用2个服务提供者
SpringCloud搭建_2.Feign

上一篇:字节跳动+阿里+华为+腾讯等大厂Java面试题,通用流行框架大全


下一篇:Nacos服务注册中心(微服务)