Spring Cloud 学习第三章:服务消费者Feign

参考博客地址:https://blog.csdn.net/forezp/article/details/81040965

前言:上一章学习了 restTemplate + Ribbon 消费服务,本章学习 Feign 来进行消费服务

一、Feign 简介

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

Feign 默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
Feign 采用的是基于接口的注解
Feign 整合了 ribbon,具有负载均衡的能力
Feign 整合了 Hystrix,具有熔断的能力

二、准备工程

1、继续使用上一章学习的工程,eureka-server 、eureka-client 、 eureka-client2 ,分别启动起来

三、创建一个 Feign 消费服务

1、创建一个 springboot 工程,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.wxw</groupId>
		<artifactId>springcloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<groupId>com.wxw</groupId>
	<artifactId>eureka-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eureka-feign</name>
	<description>Demo project for Spring Boot</description>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<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.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2、 启动类上加上以下注解
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients //开启feign 的功能

3、配置文件中添加以下内容

server:
  port: 8765

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: eureka-feign

4、创建一个 service 接口
接口上加入注解:@FeignClient , value 就是调用服务注册中心的 服务名字,指定调用哪个服务
方法上加入注解:@RequestMapping,value 就是服务中的接口名字。


@Service
@FeignClient(value = "EUREKA-CLIENT")
public interface HelloService {

    
    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}

5、创建一个 controller 调用此接口
这里注入 helloService 可能会有红色下划线的警告,但是问题不大,如果需要,可以在第四步的接口中加入@Service注解,这样就不会警告了。

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello(@RequestParam(value = "name",defaultValue = "wxw") String name){
        return helloService.hello(name);
    }
}

6、启动工程,访问 http://localhost:8765/hello
此处端口就会在 8762 和 8763 之间来回切换了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值