SpringCloud从入门到精通教程(四)- 服务消费者,实现方式二(feign)

需求背景

服务消费者,实现方式二:feign(推荐此方式)

Feign是什么?

Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。

Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。使用@EnableFeignClients即可开启Feign。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

Tips技术点

1. @EnableFeignClients注解

  • 开启Feign

2. 引入openfeign组件包

代码演示

1. 项目目录结构

2. pom.xml依赖组件

<?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>

	<artifactId>microservice-consumer-movie-feign</artifactId>
	<packaging>jar</packaging>

	<parent>
		<groupId>com.minbo.cloud</groupId>
		<artifactId>spring-cloud-microservice-study</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
		</dependency>
	</dependencies>
</project>

3. application.yml配置文件

server:
   port: 8020
spring:
   application:
      name: microservice-consumer-movie-feign
eureka:
   client:
      serviceUrl:
         defaultZone: http://localhost:8761/eureka/
   instance:
      preferIpAddress: true

4. 消费接口类

MyFeignClient.java

package com.minbo.cloud.study.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 使用@FeignClient("microservice-provider-user")注解绑定microservice-provider-user服务,还可以使用url参数指定一个URL。
 */
@FeignClient(name = "microservice-provider-user")
public interface MyFeignClient {

	//定义代码调用对方服务的"/hi"接口
	@RequestMapping(value = "/hi", method = RequestMethod.GET)
	String sayHiFromClientOne(@RequestParam(value = "name") String name);

}

FeignController.java

package com.minbo.cloud.study.controller;

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;

import com.minbo.cloud.study.feign.MyFeignClient;
import com.minbo.cloud.study.feign.MyFeignClient2;

@RestController
public class FeignController {

	@Autowired
	private MyFeignClient feignClient;

	@GetMapping(value = "/hi")
	public String sayHi(@RequestParam String name) {
		String result = this.feignClient.sayHiFromClientOne(name);
		return result;
	}
}

5. 启动类

package com.minbo.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。
 * 创建接口,为接口添加注解,即可使用Feign。
 * Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。
 * Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。
 * 使用@EnableFeignClients开启Feign
 * 
 * 简而言之:
 * a. Feign 采用的是基于接口的注解
 * b. Feign 整合了ribbon,具有负载均衡的能力
 * c. 整合了Hystrix,具有熔断的能力
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class MovieFeignApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(MovieFeignApplication.class, args);
	}
	
}

6. 是否消费接口成功

启动服务后,可以查看Eureka界面注册信息

访问本服务接口:http://localhost:8020/hi?name=minbo

表示已经成功消费了服务提供者的接口(服务提供者监听端口是8000)

完整源码下载

我的Github源码地址:

https://github.com/hemin1003/spring-cloud-study/tree/master/spring-cloud-greenwich/microservice-consumer-movie-feign

上一章教程

SpringCloud从入门到精通教程(三)- 服务消费者,实现方式一(ribbon)

该系列教程

SpringCloud从入门到精通教程

我的专栏

 

 

至此,全部介绍就结束了

 

 

-------------------------------

-------------------------------

 

我的CSDN主页

关于我(个人域名)

我的开源项目集Github

 

期望和大家一起学习,一起成长,共勉,O(∩_∩)O谢谢

欢迎交流问题,可加个人QQ 469580884,

或者,加我的群号 751925591,一起探讨交流问题

不讲虚的,只做实干家

Talk is cheap,show me the code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贺佬湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值