前面的话】书接上文,本文的某些知识依赖我的第一篇SpringCLoud的文章:SpringCloud之Eureka,如果没有看过可以先移步去看一下。另外在微服务架构中,业务都会被拆分成一个个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。上一篇文章已经讲过ribbon+rest这种方式了,这一片博文主要讲feign的应用。


壹、Feign的简介

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

简而言之:

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

贰、准备工作

新建一个feign子工程lovin-feign-client,用于后面的操作。下面是主要的pom依赖:

<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>lovin-feign-client</artifactId>
<version>0.0.1</version>
<name>lovinfeignclient</name>
<description>feignclient测试</description> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
  • 这里为了安全,我这里还是添加spring-boot-starter-security
server:
port: 8806 # 服务端口号
spring:
application:
name: lovinfeignclient # 服务名称
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注册到的eureka服务地址
feign:
hystrix:
enabled: true
  • 配置spring-boot-starter-security,这里为了方便我这里放开所有请求
package com.eelve.lovin.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /**
* @ClassName SecurityConfig
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/16 14:13
* @Version 1.0
**/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
  • 在主类上添加@EnableFeignClients@EnableHystrix ,当然也需要注册到注册中心:
package com.eelve.lovin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients; /**
* @ClassName LovinFeignClientApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 17:17
* @Version 1.0
**/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableHystrix
public class LovinFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(LovinFeignClientApplication.class,args);
}
}
  • 添加一个远程调用的服务端FeignRemoteService,并且配置feign调用信息:
package com.eelve.lovin.service;

import com.eelve.lovin.hystrix.FeignRemoteServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* @ClassName FeignRemoteService
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 17:18
* @Version 1.0
**/
@FeignClient(value = "lovineurkaclient",fallback = FeignRemoteServiceImpl.class)
public interface FeignRemoteService { @RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello();
}
  • 添加熔断器调用方法:新建FeignRemoteServiceImpl实现FeignRemoteService接口:
package com.eelve.lovin.hystrix;

import com.eelve.lovin.service.FeignRemoteService;
import org.springframework.stereotype.Component; /**
* @ClassName FeignRemoteServiceImpl
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 17:31
* @Version 1.0
**/
@Component
public class FeignRemoteServiceImpl implements FeignRemoteService {
@Override
public String hello() {
return "hystrix起作用了";
}
}
  • 最后新建FeignController,来消费服务:
package com.eelve.lovin.controller;

import com.eelve.lovin.service.FeignRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @ClassName FeignController
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 17:21
* @Version 1.0
**/
@RestController
public class FeignController { @Autowired
FeignRemoteService feignRemoteService; @GetMapping(value = "/getHello")
public String getHello() {
return feignRemoteService.hello();
}
}

叁、启动测试

  • 依次启动eureka的服务端和两个客户端,以及新建的lovin-feign-client



    我们可以看到服务已经全部启动成功
  • 然后访问http://localhost:8806/getHello



    我们可以看到已经可以通过feign调到我们建立的eureka客户端了
  • 再次请求接口观察返回



    我们可以看到我们调到了通过feign调用ribbon负载的另外一个接口了,到这里我们就已经弄好了一个简单的ribbon负载。

肆、添加Hystrix Dashboard断路器监控

  • 添加需要的pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
  • 在主类上添加@EnableHystrixDashboard,开启断路器监控,并且配置HystrixMetricsStreamServlet
package com.eelve.lovin;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; /**
* @ClassName LovinFeignClientApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 17:17
* @Version 1.0
**/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class LovinFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(LovinFeignClientApplication.class,args);
} @Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
  • 访问http://localhost:8806/hystrix



    这里我们通过首页可以看到:
默认的集群监控:通过URL http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
指定的集群监控:通过URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName的监控。
单体应用监控:通过URL http://hystrix-app:port/hystrix.stream开启,实现对某个具体的服务监控
  • 添加监控模式查看详情,这里选择第三个单体应用





这样我们就完成了熔断器的监控,当然具体含义有待下一步深究。

伍、网络架构

  • 我们可以看到我们调用的服务不再是像再上一篇文章中的直接访问对应的服务,而是通过feign的Ribbon的负载均衡的去调用的,而且这里说明一点,Ribbon的默认机制是轮询。


SpringCloud之Feign的更多相关文章

  1. SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer);

    SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer); 第一种方法: 如果你 ...

  2. SpringCloud 在Feign上使用Hystrix(断路由)

    SpringCloud  在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...

  3. SpringCloud(5)---Feign服务调用

    SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...

  4. springcloud 实战 feign使用中遇到的相关问题

    springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...

  5. springcloud 之 feign的重复性调用 优化

    最近有一个springcloud的feign请求,用于获取坐标经纬度的信息,返回结果永远是固定不变的,所以考虑优化一下,不然每次转换几个坐标都要去请求feign,返回的所有坐标信息,数据量太大导致耗时 ...

  6. SpringCloud之Feign负载均衡(四)

    整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...

  7. 解决SpringCloud使用Feign跨服调用时header请求头中的信息丢失

    在使用SpringCloud进行Feign跨服调用时header请求头中的信息会丢失,是因为Feign是不会带上当前请求的Cookie信息和头信息的,这个时候就需要重写请求拦截. 1.需要重写Requ ...

  8. SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]

    目录 前提:本篇是基于 SpringCloud+Eureka+Feign+Ribbon的简化搭建流程和CRUD练习[1] 的修改与拓展 1.修改consumer的CenterFeign.java,把返 ...

  9. SpringCloud系列——Feign 服务调用

    前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...

  10. 31.【微服务架构】SpringCloud之Feign(五)

    Feign简介 Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Fei ...

随机推荐

  1. 逆向MFC程序

    目录 @ 1 MFC执行流程 1.1 环境支持 1.2 分析 1.3 实践探索 1.3.1 创建一个MFC程序 1.3.2 下关键断点并调试 1.4 转向MFC库源文件中观测 2 逆向 2.1 特征码 ...

  2. 学习LayUI时自研的表单参数校验框架

    开发背景&痛点:每次写前端的表单的时候需要对表单里用户填写的内容进行校验,减少服务器压力,提前对已知错误对用户提示.每次会要写很多的if else等等对输入框中的内容进行判断,并对为空.格式不 ...

  3. WebGL着色器32位浮点数精度损失问题

    问题 WebGL浮点数精度最大的问题是就是因为js是64位精度的,js往着色器里面穿的时候只能是32位浮点数,有效数是8位,精度丢失比较严重. 这篇文章里讲了一些处理方式,但是视坐标这种方式放在我们的 ...

  4. spring+mybatis最简多数据源配置

    作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权所有,欢迎保留原文链接进行转载:) 说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持 ...

  5. Linux基本操作及安装(部分)

    1.分别用cat \tac\nl三个命令查看文件/etc/ssh/sshd_config文件中的内容,   并用自己的话总计出这三个文档操作命令的不同之处? [root@localhost ~]# c ...

  6. HttpsUtils

    package io.renren.modules.jqr.util; import java.io.BufferedReader; import java.io.InputStream; impor ...

  7. 二、Markdown基本语法

    目录 2.1 标题 一级标题 二级标题 三级标题 2.2 加粗 2.3倾斜 2.4 高亮 2.5 上标 2.6 下标 2.7 代码引用(>式) 2.8 代码引用(```式) 2.9 代码引入(` ...

  8. 调测Onvif事件总结解决办法

    主要在调测事件用例的过程中,发现了大量的信息,和未曾碰到的场景和非法错误等信息,先总结解决办法如下: (1)测试过程中发现以前的一个难题解决了,原先在生成soap空间命名的文件中有部分需要下载,离线生 ...

  9. sharding demo 读写分离 U (分库分表 & 不分库只分表)

    application-sharding.yml sharding: jdbc: datasource: names: ds0,ds1,dsx,dsy ds0: type: com.zaxxer.hi ...

  10. 【Java例题】7.3 线程题3-素数线程

    3.素数线程.设计一个线程子类,依次随机产生10000个随机整数(100-999):再设计另一个线程子类,依次对每一个随机整数判断是不是素数,是则显示:然后编写主类,在主函数中定义这两个线程类的线程对 ...