Spring Cloud(3)——服务消费者

一、简介

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

二、项目实例

创建maven工程microservice-consumer-user

1、在pom.xml中添加依赖

 
    4.0.0
    com.baibei.comsumer
    microservice-consumer-user
    war
    0.0.1-SNAPSHOT
    microservice-consumer-user Maven Webapp
    http://maven.apache.org
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.4.RELEASE
         
    
    

        
            org.springframework.cloud
            spring-cloud-starter-feign
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Camden.SR5
                pom
                import
            
        
    
    
        microservice-consumer-user
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、创建UserConsumerApplication.java

package com.baibei.consumer.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author: 会跳舞的机器人
 * @email:[email protected]
 * @date: 17/2/17 上午11:45
 * @description:用户服务消费者
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserConsumerApplication.class, args);
    }
}
  • EnableDiscoveryClient注解用来标识开启服务发现功能
  • EnableFeignClients注解用来开启Feign功能

3、创建application.properties文件

spring.application.name=microservice-consumer-user
server.port=8005

# 注册中心地址
eureka.client.serviceUrl.defaultZone=http://eureka-server-peer1:8761/eureka/,http://eureka-server-peer2:8762/eureka/,http://eureka-server-peer3:8763/eureka/

# Indicates whether this client should fetch eureka registry information from eureka server
# 客户端是否要从eureka server获取注册信息,默认为true
eureka.client.fetchRegistry=true

# Indicates how often(in seconds) to fetch the registry information from the eureka server
# 从eureka server获取注册信息的频率,默认为30秒,缩短配置时间可以缓解服务上线时间过长的问题
eureka.client.registryFetchIntervalSeconds=10


# 自定义负载均衡策略,microservice-provider-user为服务应用名
microservice-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

4、创建UserFeignClient

package com.baibei.consumer.user.service;

import com.baibei.common.core.api.ApiResult;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 会跳舞的机器人
 * @date: 2017/3/29 16:54
 * @description: 用户服务调用端
 */
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {

    /**
     * 根据ID查找用户
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    ApiResult findUserById(@PathVariable("id") Integer id);


}
  • FeignClient注解用来绑定对应的服务

5、web层的调用

package com.baibei.consumer.user.controller;

import com.baibei.common.core.api.ApiResult;
import com.baibei.consumer.user.service.UserFeignClient;
import org.apache.log4j.Logger;
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: 会跳舞的机器人
 * @email:[email protected]
 * @date: 17/2/17 上午11:51
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserFeignClient userFeignClient;


    /**
     * 根据ID查找用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ApiResult getUser(@PathVariable Integer id) {

        return userFeignClient.findUserById(id);
    }
}

通过注入UserFeignClient即可实现像调用本地方法一样去调用远程服务方法

附录:项目目录截图

Spring Cloud(3)——服务消费者_第1张图片
image

你可能感兴趣的:(Spring Cloud(3)——服务消费者)