SpringCloud无废话入门03:Feign声明式服务调用

1.Feign概述

在上一篇的HelloService这个类中,我们有这样一行代码:

return restTemplate.getForObject("http://hello-service/hello",String.class);

对于代码有一定洁癖的你来说,一定感觉到了,这个url应该是可以被配置的。既然说到配置,那我们首先想到的就是使用java注解的方式。Feign就是这样一个注解框架,它也是netflix为我们提供的,方便我们整合ribbon和hystrix(后面会学习)。

使用Feign,我们能很方便的通过编写接口并插入注解,来定义和代理HTTP请求。Feign主要具备如下特性:

支持Feign注解;

支持Ribbon负载均衡;

支持HTTP编码器和解码器;

提供了熔断器Hystrix;

2.Feign引入

让我们修改ribbon这个项目,使之支持feign。

首先,pom引入,

<?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/maven-v4_0_0.xsd">

<parent>

<artifactId>springcloud.parent</artifactId>

<groupId>com.zuikc</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<packaging>war</packaging>

<name>ribbon</name>

<artifactId>ribbon</artifactId>

<dependencyManagement>

<dependencies>

<dependency>

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

<artifactId>spring-cloud-dependencies</artifactId>

<version>Greenwich.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

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

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

<version>1.4.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

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

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

<version>1.4.6.RELEASE</version>

</dependency>

</dependencies>

</project>

注意pom中的粗体部分。引入Feign依赖,会自动引入Hystrix依赖。

appcation.yml并不需要变动。

3.@EnableFeignClients

修改ServiceRibbonApplication,加入注解@EnableFeignClients,这一步很重要,说明项目对于feign的支持,

package com.zuikc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.cloud.openfeign.EnableFeignClients;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

/**

* @ClassName ServiceRibbonApplication

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

public class ServiceRibbonApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceRibbonApplication.class, args);

}

@Bean

@LoadBalanced

RestTemplate restTemplate() {

return new RestTemplate();

}

}

4.服务接口

创建一个接口,

package com.zuikc;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "hello-service")

public interface HelloService {

//服务中方法的映射路径

@RequestMapping("/hello")

String hello();

}

这个接口就比较重要了。

注解@FeignClient说明了,我们需要去eureka服务上去调用“hello-service”这个服务。

注解@RequestMapping指的是我们需要调用的路径。

5.控制器

现在我们还需要最后一步,就是创建一个控制器来接受请求,然后让robbin去分发,

package com.zuikc;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName ConsumerController

* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

* @Author 码农星球

**/

@RestController

public class HelloController {

@Autowired

HelloService helloService;

@RequestMapping("/hello")

public String helloConsumer(){

return helloService.hello();

}

}

当一切处理完成,让我们启动这个项目,我们仍旧看到ribbon的这个服务,

然后,http://localhost:9291/hello吧,可以看到LB非常的成功。

现在,假设其中一个服务提供者因为某种原因(比如异常、断网)停掉了,看看结果会是怎么样的。为了模拟这个现象,让我们关闭provider2,发现我们无论怎么刷新上面的url,LB永远分配到provider1。这也是分布式系统容错能力更强的一种保证!

感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。

原文地址:https://www.cnblogs.com/luminji/p/10608573.html

时间: 2024-08-26 08:44:24

SpringCloud无废话入门03:Feign声明式服务调用的相关文章

Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现

介绍 本示例主要介绍 Spring Cloud 系列中的 Eureka,使你能快速上手负载均衡.声明式服务.服务注册中心等 Eureka Server Eureka 是 Netflix 的子模块,它是一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 服务注册和发现对于微服务架构而言,是非常重要的.有了服务发现和注册,只需要使用服务的标识符就可以访问到服务,而不需要修改服务调用的配置文件.该功能类似于 Dubbo 的注册中心,比如 Zookeeper. Eureka

005声明式服务调用Feign

1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix.Spring Boot Starter Actuator依赖和Spring Cloud依赖管理 <dependencies> <!--Eureka Client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>sprin

干货分享微服务spring-cloud(5.声明式服务调用feign)

Spring cloud feign基于Netflix feign实现,整合了spring cloud ribbon与spring cloud hystrix,除了提供这两者的强大功能之外,它还提供了一种声明式的web服务客户端定义方式 新建spring boot工程并命名为demo-springcloud-feign-consumer,新建启动类FeignApplication,通过@ EnableFeignClients注解来开启spring cloud feign的功能支持 定义YhqSe

【第三章】声明式服务调用(Feign)

当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻.那么有没有更好的解决方案呢?答案是确定的有,Netflix已经为我们提供了一个框架:Feign. Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数.格

Spring Cloud 声明式服务调用 Feign

一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声明式调用服务.而不会在客服端和服务端存储2份相同的model和api定义.Feign在RestTemplate的基础上对其封装,由它来帮助我们定义和实现依赖服务接口的定义.Spring Cloud Feign 基于Netflix Feign 实现的,整理Spring Cloud Ribbon 与 S

Spring Cloud第七篇 | 声明式服务调用Feign

本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud第二篇 | 使用并认识Eureka注册中心 Spring Cloud第三篇 | 搭建高可用Eureka注册中心 Spring Cloud第四篇 | 客户端负载均衡Ribbon Spring Cloud第五篇 | 服务熔断Hystrix Spring Cloud第六篇 | Hystrix仪表盘监控Hy

OSGI传统注册式服务与声明式服务

上一篇博文中我们编写了第二个简单的osgi的example,并编写了一个接口DictionaryService,并在Activator这个Bundle中实现了这个interface,并在start启动方法中进行了osgi服务的注册,但并没有使用这个服务,这一篇文章中并不讲解怎么使用这个已经注册的服务,但是会讲解服务的使用方式,一种为声明式服务,一种为传统注册式服务,以下就是开始讲解何为osgi的注册式服务与声明式服务. 传统注册式服务 传统方式下,我们注册服务都是在bundle的激活器(Acti

[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用

上一节我们使用了Ribbon(基于Http/Tcp)进行微服务的调用,Ribbon的调用比较简单,通过Ribbon组件对请求的服务进行拦截,通过Eureka Server 获取到服务实例的IP:Port,然后再去调用API.本节课我们使用更简单的方式来实现,使用声明式的Web服务客户端Feign,我们只需要使用Feign来声明接口,利用注解来进行配置就可以使用了,是不是很简单?实际工作中,我们也只会用到Feign来进行服务之间的调用(大多数).接下来,我们来实例操作一把. 为了代码的重用性,我们

springCloud(9):使用Feign实现声明式REST调用

一.简介 前面我们是使用RestTemplate实现rest api调用的,代码如下: @GetMapping("/user/{id}") public User findById(@PathVariable Long id) throws Exception {     return  this.restTemplate.getForObject("http://spring-ribbon-eureka-client2/" + id, User.class); }