SpringCloud微服务实战之Feign

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

一、使用Feign实现HelloService服务消费
1、创建Maven项目SpringCloud-Feign,目录结构如下
SpringCloud微服务实战之Feign_第1张图片

2、pom中添加feign依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        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接口
    SpringCloud微服务实战之Feign_第2张图片
    模拟阻塞,调用fallback方法
    SpringCloud微服务实战之Feign_第3张图片

  • 访问helloservice的user相关接口
    SpringCloud微服务实战之Feign_第4张图片

你可能感兴趣的:(springcloud,架构,SpringCloud实战)