SpringCloud微服务实战之Feign

Spring Cloud Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,它除了提供这两者的强大功能之外,还提供了一种声明式的web服务客户端定义方式。

一、使用Feign实现HelloService服务消费
1、创建Maven项目SpringCloud-Feign,目录结构如下
这里写图片描述

2、pom中添加feign依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- add feign depend -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>

3、在主类中添加注解@EnableFeignClients开启对Feign的支持。
4、定义HelloService接口,通过@FeignClient注解来绑定HelloService服务,name代表服务名,fallback代表Hystrix熔断器服务降级处理逻辑类。实现UserService接口同理。

package com.spring.springcloud.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.springcloud.service.fb.HelloServiceFallback;

@FeignClient(name="helloservice",fallback=HelloServiceFallback.class)
public interface HelloService {

    @RequestMapping("/hello")
    public String hello();
}
package com.spring.springcloud.service.fb;

import org.springframework.stereotype.Component;

import com.spring.springcloud.service.HelloService;

@Component
public class HelloServiceFallback implements HelloService {

    @Override
    public String hello() {
        return "Error";
    }

}

package com.spring.springcloud.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.springcloud.service.fb.UserServiceFallback;
import com.spring.springcloud.vo.User;

@FeignClient(name="helloservice",fallback=UserServiceFallback.class)
public interface UserService {

    @RequestMapping(value="/findUser/{id}",method=RequestMethod.GET)
    public User findUserById(@PathVariable("id") Long id);

    @RequestMapping(value="/addUser",method=RequestMethod.POST)
    public User addUser(@RequestBody User user);
}
package com.spring.springcloud.service.fb;

import org.springframework.stereotype.Component;

import com.spring.springcloud.service.UserService;
import com.spring.springcloud.vo.User;

@Component
public class UserServiceFallback implements UserService {

    @Override
    public User findUserById(Long id) {
        User user = new User();
        user.setId(new Long(5555));
        user.setUserName("defalutConsumerForFind");
        return null;
    }

    @Override
    public User addUser(User user) {
        user.setId(new Long(6666));
        user.setUserName("defalutConsumerForAdd");
        return null;
    }

}

5、增加application.properties

server.port=8666

spring.application.name=feignconsumer
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

测试:

  • 访问helloService的hello接口
    这里写图片描述
    模拟阻塞,调用fallback方法
    这里写图片描述

  • 访问helloservice的user相关接口
    这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值