前言:

在上一节里,我们学习了ribbon的使用。

我们了解到ribbon是一个客户端负载均衡机制。

而我们今天要讲的Feign呢,也是一款客户端负载均衡机制。

或者这样说,Feign封装了ribbon的负载均衡,实现了面向接口调用服务编程取缔面向服务编程。

ribbon面向服务编程:

@GetMapping("/hello")
public List<String> sayHello() {
List<String> list = new ArrayList<>();
for(int i=0;i<30;i++) {
list.add(restTemplate.getForObject("http://CL-HELLO-PRODUCER/hello", String.class));
}
return list;
}

feign面向接口编程:

@FeignClient(value="CL-HELLO-PRODUCER")
public interface HelloService { @GetMapping("/hello")
public String sayHello(); }

新建一个服务消费者(cl_hello_consumer_feign):

1.添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<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.xm.cloud</groupId>
<artifactId>cl_hello_consumer_feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>cl_hello_consumer_feign</name>
<description>This is a Web about springcloud</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</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>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>${spring-cloud.version}</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> </project>

2.修改配置

eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/

eureka.client.register-with-eureka=false

3.开启注解

package com.xm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan; @EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ClHelloConsumerFeignApplication { public static void main(String[] args) {
SpringApplication.run(ClHelloConsumerFeignApplication.class, args);
}
}

4.添加Service

package com.xm.cloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; @FeignClient(value="CL-HELLO-PRODUCER")
public interface HelloService { @GetMapping("/hello")
public String sayHello(); }

5.添加Controller

package com.xm.cloud.controller;

import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.xm.cloud.service.HelloService; @RestController
public class HelloController {
@Autowired
private HelloService helloService; @GetMapping("/hello")
public List<String> sayHello() {
List<String> list = new ArrayList<String>();
for(int i=0;i<10;i++) {
list.add(helloService.sayHello());
}
return list;
} }

6.测试

访问:localhost:8080/helo

0 "Hello Spring Cloud! 001号机器"
1 "Hello Spring Cloud! 003号机器"
2 "Hello Spring Cloud! 002号机器"
3 "Hello Spring Cloud! 001号机器"
4 "Hello Spring Cloud! 003号机器"
5 "Hello Spring Cloud! 002号机器"
6 "Hello Spring Cloud! 001号机器"
7 "Hello Spring Cloud! 003号机器"
8 "Hello Spring Cloud! 002号机器"
9 "Hello Spring Cloud! 001号机器"

4.Spring Cloud初相识--------Feign负载均衡的更多相关文章

  1. 3.Spring Cloud初相识--------Ribbon客户端负载均衡

    前言: 在生产环境中,未避免单点故障,每个微服务都会做高可用部署. 通白的说,就是每一个一模一样的服务会根据需求提供多分在多台机器上. 那么在大并发的情况下,如何分配服务可以快速得到响应,就成为了我们 ...

  2. Spring Cloud ---- 服务消费与负载均衡(feign)

    feign是一个声明式的伪客户端,只需要创建一个接口并且注解,它具有可插拔的特性.feign集合了Ribbon,再与Eurake结合实现服务的注册发现与负载均衡.结合Hystrix,具有熔断功能. 1 ...

  3. Spring Cloud - 切换Ribbon的负载均衡模式

    Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模 ...

  4. 【Spring Cloud学习之三】负载均衡

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 主流的负载均衡技术有nginx.LVS.HAproxy.F5,Spring Clou ...

  5. Spring Cloud 客服端负载均衡 Ribbon

    一.简介   Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署, ...

  6. 撸一撸Spring Cloud Ribbon的原理-负载均衡策略

    在前两篇<撸一撸Spring Cloud Ribbon的原理>,<撸一撸Spring Cloud Ribbon的原理-负载均衡器>中,整理了Ribbon如何通过负载均衡拦截器植 ...

  7. Spring Cloud微服务Ribbon负载均衡/Zuul网关使用

    客户端负载均衡,当服务节点出现问题时进行调节或是在正常情况下进行 服务调度.所谓的负载均衡,就是当服务提供的数量和调用方对服务进行 取舍的调节问题,在spring cloud中是通过Ribbon来解决 ...

  8. Spring Cloud Gateway Ribbon 自定义负载均衡

    在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...

  9. 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)

    在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...

随机推荐

  1. jquery 操作 checkbox select

    1.prop方法获取.设置checked属性 <input type="checkbox" name="checkboxMain" onclick=&qu ...

  2. SourceTree 关于 .gitignore使用/下载

    # =============== # # Unity generated # # =============== # Temp/ Obj/ UnityGenerated/ Library/ Asse ...

  3. Murano Weekly Meeting 2015.11.04

    Meeting time: 2015.November.4th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting sum ...

  4. vue-cli生成的重要代码详解

    安装好vue-cli之后,我们可以在package.json中看到下面所示: { // 项目名称 "name": "myvue", // 项目版本 " ...

  5. One By One扑克牌游戏(C++)

    用我们方言说就是类似“骡子冲”的游戏,游戏双方各拿一定数目的扑克牌,每次每个人打一张牌,排成一列.如果打出的牌有一样的,那么这两张牌(包括这两张牌),全部按顺序拿到打出第二张相同牌的玩家手中,且放在手 ...

  6. mysql主从数据库错误处理

    方法一:忽略错误后,继续同步 该方法适用于主从库数据相差不大,或者要求数据可以不完全统一的情况,数据要求不严格的情况 解决: stop slave; #表示跳过一步错误,后面的数字可变set glob ...

  7. 百度网页分享js代码

    1.小图标 <div class="bdsharebuttonbox"> <a href="#" class="bds_qzone& ...

  8. 使用ajax获取用户所在地的天气

    1.要获取用户归属地的天气,首先得获取用户所在的市区, 这里先获取用户的IP,通过IP获取IP的归属地,从而得到用户 地址. 获取客户端ip: js: <scripttype="tex ...

  9. jsp---》》》新闻发布系统的项目跟踪+++++++文件上传

    先来一个分层架构图: WeebRoot目录下的页面: 现在,此项目以实现登录,注销,新闻列表,编辑主题>>>> 先来登录部分的关键代码 index.jsp中的代码 userIn ...

  10. 对MVVM思想的在认识

    如果说MVP是对MVC的进一步改进,那么MVVM则是思想的完全变革.它是将“数据模型数据双向绑定”的思想作为核心,因此在View和Model之间没有联系,通过ViewModel进行交互,而且Model ...