Feign实现生命试rest调用
cookqq ›博客列表 ›spring cloud

Feign实现生命试rest调用

2018-09-04 10:57:52.0|分类: spring cloud|浏览量: 4609

摘要: Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。


blob.png

OrderApp.java

package com.cookqq.consumer;

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

/**
 * 订单服务
 *
 */
@SpringBootApplication
@EnableFeignClients
public class OrderApp 
{
	
    public static void main( String[] args )
    {
        SpringApplication.run(OrderApp.class, args);
    }
}

@EnableFeignClients注解,声明使用Feign


user.java

package com.cookqq.consumer.bean;

import java.math.BigDecimal;

public class User {

	private Long id;
	private String username;
	private String name;
	private Integer age;
	private BigDecimal balance;
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public BigDecimal getBalance() {
		return balance;
	}
	public void setBalance(BigDecimal balance) {
		this.balance = balance;
	}
	
	
	
}

UserFeignClient.java

package com.cookqq.consumer.feign;

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

import com.cookqq.consumer.bean.User;

/**
 * provider-user项目微服务名称
 * @author conca
 *
 */
@FeignClient(name="provider-user")
public interface UserFeignClient {

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

findUserById(@PathVariable Long id)方法如果去掉@PathVariable,findUserById(Long id)默认@RequestBody注解。RequestBody一定是包含在请求体中的,GET方式无法包含。Feign在GET请求包含RequestBody时强制转成了POST请求,而不是报错。


OrderController.java

package com.cookqq.consumer.controller;

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.RestController;

import com.cookqq.consumer.bean.User;
import com.cookqq.consumer.feign.UserFeignClient;

@RestController
public class OrderController {

	@Autowired
	private UserFeignClient userFeignClient;
	
	@GetMapping("/user/{id}")
	public User findUserById(@PathVariable Long id){
//		return this.restTemplate.getForObject("http://127.0.0.1:8000/"+id, User.class);
		
		
//		List<ServiceInstance> listInfo = discoveryClient.getInstances("provider-user");
//		if(listInfo.size()>0){
//			return this.restTemplate.getForObject(
//					listInfo.get(new Random().nextInt(listInfo.size())).getUri()+"/"+id, 
//					User.class);
//		}
		
//		return this.restTemplate.getForObject("http://provider-user/"+id, User.class);
		
		return userFeignClient.findUserById(id);
	}
	
}

RequestMapping代表映射的路径,使用GET,POST,PUT,DELETE方式都可以映射到该端点。GetMapping代表get方式。


SpringMVC中常用的请求参数注解有(@RequestParam,@RequestBody,@PathVariable)等。name被默认当做@RequestParam。形参String name由框架使用字节码技术获取name这个名称,自动检测请求参数中key值为name的参数,也可以使用@RequestParam(“name”)覆盖变量本身的名称。当我们在url中携带name参数或者form表单中携带name参数时,会被获取到。


application.yml

server:
  port: 8080
spring:
  application:
    name: consumer-order
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cookqq</groupId>
  <artifactId>consumer-order-feign</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>consumer-order-feign</name>
  <url>http://maven.apache.org</url>

 <!-- 引入spring boot的依赖 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
   <!-- 引入spring cloud的依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

feign依赖内容

<dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-openfeign</artifactId>

    </dependency>



启动euraka

启动provider-user(多个)

启动consumer-order-feign


多次访问,查看provider-user访问日志

blob.png

一键分享文章

分类列表

  • • struts源码分析
  • • flink
  • • struts
  • • redis
  • • kafka
  • • ubuntu
  • • zookeeper
  • • hadoop
  • • activiti
  • • linux
  • • 成长
  • • NIO
  • • 关键词提取
  • • mysql
  • • android studio
  • • zabbix
  • • 云计算
  • • mahout
  • • jmeter
  • • hive
  • • ActiveMQ
  • • lucene
  • • MongoDB
  • • netty
  • • flume
  • • 我遇到的问题
  • • GRUB
  • • nginx
  • • 大家好的文章
  • • android
  • • tomcat
  • • Python
  • • luke
  • • android源码编译
  • • 安全
  • • MPAndroidChart
  • • swing
  • • POI
  • • powerdesigner
  • • jquery
  • • html
  • • java
  • • eclipse
  • • shell
  • • jvm
  • • highcharts
  • • 设计模式
  • • 列式数据库
  • • spring cloud
  • • docker+node.js+zookeeper构建微服务
版权所有 cookqq 感谢访问 支持开源 京ICP备15030920号
CopyRight 2015-2018 cookqq.com All Right Reserved.