Feign的优点:面向接口,完全不用管实现,传入规定格式的数据就可以了

搭建消费者项目(FeignDemo)

1、创建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.cppdy</groupId>
<artifactId>FeignDemo</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>

2、创建application.yml配置文件

eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka/
server:
port: 9004
spring:
application:
name: cppdy-feign

3、创建HelloService接口

package com.cppdy.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "cppdy-hello")
public interface HelloService { @RequestMapping("hello")
String hello(@RequestParam("name") String name);
}

4、创建HelloController类

package com.cppdy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.cppdy.service.HelloService; @RestController
public class HelloController { @Autowired
private HelloService helloService; @RequestMapping("hello")
public String hello(String name) { return helloService.hello(name);
} }

5、创建启动类(FeignApp)

package com.cppdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication
//开启服务发现
@EnableDiscoveryClient
//开启feign
@EnableFeignClients
public class FeignApp { public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
} }

6、先启动EurekaDemo(注册中心项目),再启动2个ProduceDemo(服务提供者项目)端口号分别设置为9001、9002,再启动FeignDemo(消费者项目)端口号设置为9004,一直访问http://localhost:9004/hello,会自动轮流调用9001和9002的hello方法(负载均衡)

Feign服务消费者的更多相关文章

  1. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  2. springcloud干活之服务消费者(feign)

    springcloud系列文章的第三篇 本章将继续讲述springcloud的消费者(feign) Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端. ...

  3. SpringCloud的服务消费者 (二):(rest+feign/ribbon)声明式访问注册的微服务

    采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...

  4. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  5. 玩转SpringCloud(F版本) 二.服务消费者(2)feign

    上一篇博客讲解了服务消费者的ribbon+restTemplate模式的搭建,此篇文章将要讲解服务消费者feign模式的搭建,这里是为了普及知识 平时的项目中两种消费模式选择其一即可 本篇博客基于博客 ...

  6. 【SpringCloud】第三篇: 服务消费者(Feign)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  7. 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)

    前言 上两章节,介绍了下关于注册中心-Eureka的使用及高可用的配置示例,本章节开始,来介绍下服务和服务之间如何进行服务调用的,同时会讲解下几种不同方式的服务调用. 一点知识 何为负载均衡 实现的方 ...

  8. 创建服务消费者(Feign)

    概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...

  9. SpringCloud-创建服务消费者-Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

随机推荐

  1. Educational Codeforces Round 47 (Rated for Div. 2)E.Intercity Travelling

    题目链接 大意:一段旅途长度N,中间可能存在N-1个休息站,连续走k长度时,疲劳值为a1+a2+...+aka_1+a_2+...+a_ka1​+a2​+...+ak​,休息后a1a_1a1​开始计, ...

  2. 【tmos】spring data jpa 创建方法名进行简单查询

    参考链接 spring data jpa 创建方法名进行简单查询:http://www.cnblogs.com/toSeeMyDream/p/6170790.html

  3. django 学习手册 - ORM 报错集(随时更新)

    报错问题: 问题一:(1050代码) django.db.utils.InternalError: (1050, "Table 'app01_group' already exists&qu ...

  4. 关于setInterval返回值问题

    oBtn1.onclick = function(){ clearInterval(timer); timer = setInterval(cwidth,10); alert(timer); } oB ...

  5. 编写Python脚本进行ARP欺骗

    1.系统环境:Ubuntu 16.04 Python版本:2.7 2.攻击机器:Ubuntu(192.16.0.14) 目标机器:Windows 7(192.168.0.9) 网关:(192.168. ...

  6. 1-OSI七层模型详解

    1.网络协议种类 市面上存在4,5,7层协议. 1.1 国际标准化组织ISO发布的OSI 7层协议模型(即OSI开放式互联参考模型),是概念性模型. 1.2 TCP/IP是一种实践类的模型,已成为行业 ...

  7. date命令以及date -d使用

    date -d '2 days ago' //显示2天前的时间,2019年 02月 10日 星期日 08:53:28 CST date -d "2 days ago" +%Y%m% ...

  8. FAT文件系统规范v1.03学习笔记---1.保留区之启动扇区与BPB

    1.前言 本文主要是对Microsoft Extensible Firmware Initiative FAT32 File System Specification中文翻译版的学习笔记. 每个FAT ...

  9. LwIP Application Developers Manual9---LwIP and multithreading

    1.前言 lwIP的内核并不是线程安全的.如果我们必须在多线程环境里使用lwIP,那么我们必须使用“upper”API层的函数(netconn或sockets).当使用raw API时,你需要自己保护 ...

  10. MySQL全备+binlog恢复方法之伪装master【原创】

    利用mysql全备 +binlog server恢复方法之伪装master 单实例试验 一.试验环境 10.72.7.40 实例 mysql3306为要恢复的对象,mysql3306的全备+binlo ...