SpringCloud之Feign服务调用

SprinCloud之Feign服务调用

一、简介

Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign.

二、依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-openfeignartifactId>
dependency>

三、配置文件

server:
  # 配置服务的端口号
  port: 10067
spring:
  main:
    # 当遇到同样名字的时候,是否允许覆盖注册
    allow-bean-definition-overriding: true
  application:
    name: Consumer-Service
eureka:
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
  client:
    service-url:
      defaultZone: http://127.0.0.1:9090/eureka/
hystrix:
  command:
    default:  #default全局有效,service id指定应用有效
      execution:
        isolation:
          thread:
            #断路器超时时间,默认1000ms
            timeoutInMilliseconds: 3000
feign:
  hystrix:
    # 开启hystrix
    enabled: true
ribbon:
  # 读取操作
  ReadTimeout: 5000
  # 连接超时
  ConnectionTimeout: 3000

四、修改启动类,开启Feign功能

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author molong
 * 服务的调用者
 */
@EnableFeignClients
@SpringCloudApplication
public class ConsumerApplication {

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

五、Feign的客户端

import com.cloud.consumer.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author molong
 */
// UserClientImpl 错误返回的类
@FeignClient(value = "User-Service", fallback = UserClientImpl.class)
public interface UserClient {

    /**
     * 饭就
     * @param id
     * @return
     */
    @GetMapping("user/{id}")
    User queryById(@PathVariable("id") Integer id);
}
import com.cloud.consumer.entity.User;
import org.springframework.stereotype.Component;

@Component
public class UserClientImpl implements UserClient {

    @Override
    public User queryById(Integer id) {
        User user = new User();
        user.setUsername("未知错误");
        return user;
    }
}

六、控制器类

import com.cloud.consumer.client.UserClient;
import com.cloud.consumer.utils.ResultUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author molong
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {

    @Autowired
    private UserClient userClient;

    @GetMapping("/{id}")
    @HystrixCommand
    public ResultUtils queryById(@PathVariable("id") Integer id) {
        return ResultUtils.ok.data(userClient.queryById(id)).code(200).msg("success");
    }
}

你可能感兴趣的:(springcloud)