Spring Cloud Feign 服务消费者

服务消费者(Feign)

什么是Feign

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon

创建service-order-feign工程

Maven依赖



    4.0.0

    com.cc.feign.order
    service-order-feign
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.RC1
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    


application.yml配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8765
spring:
  application:
    name: service-order-feign

编写service,调用service-member

package com.cc.feign.order;

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

import java.util.List;

@FeignClient("service-member")
public interface MemberFeign {
	@RequestMapping("/getUserList")
	public List getOrderByUserList();
}

@FeignClient 需要调用服务名称,@RequestMapping服务请求名称

对应会员服务(service-member):

package com.cc.member;

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

import java.util.ArrayList;
import java.util.List;

@RestController
public class MemberController {

    @Value("${server.port}")
    private String port;

    @RequestMapping("/getUserList")
    public List getUserList() {
        List listUser = new ArrayList();
        listUser.add("zhangsan");
        listUser.add("lisi");
        listUser.add("yushengjun");
        listUser.add(port);
        return listUser;
    }

}

order服务controller: 

package com.cc.feign.order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MemberFeignController {

    @Autowired
    private MemberFeign memberFeign;

    @RequestMapping("/getUserList")
    public List getUserList(){
        return memberFeign.getOrderByUserList();
    }
}

启动类 

package com.cc.feign.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderFeignApp {

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

}

Spring Cloud Feign 服务消费者_第1张图片

Spring Cloud Feign 服务消费者_第2张图片 

 

Spring Cloud Feign 服务消费者_第3张图片

你可能感兴趣的:(Spring,Cloud)