手把手搭建SpringCloud,第三篇——使用Feign实现声明式REST调用

本文通过实战演示如何利用Feign实现声明式HTTP客户端调用。介绍了Feign的基本概念及如何集成到Spring Cloud项目中,包括配置Maven依赖、application.yml设置、启动类注解及控制器接口定义。

开发工具:STS

新建项目前,请确认工作环境已经配置好了Maven。

本章提及的Eureka Server 请阅读上一篇博客这里 https://blog.csdn.net/Lee_SmallNorth/article/details/95975546


 

简单说一下Feign

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。

Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

Feign 采用的是基于接口的注解

我这个人真是有强迫症啊,非要想等把这一专题得博客写完,才写别的技术总结,可能是为了以后自己年纪大了顺着看笔记方便。哈哈哈哈,废话不多说了,实战开始,ShowTime.......

新建项目1

pom.xml 添加Feign依赖,STS新建项目时候没有找到这个依赖,手动添加,Maven--Update Maven一下就可以了。


            <!-- Eureka Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<!-- Feign -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>

 application.yml

server:
  port: 2001
  
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:dev-feign-server:2001
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1
    
spring:
  application:
    name: dev-feign-server 

启动类

package com.portal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient //开启服务注册
@EnableFeignClients//开启Feign功能
public class SpringcloudFeignServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudFeignServerApplication.class, args);
	}

}

测试,FeignController.java

package com.portal.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {
	
	@Value("${server.port}")
	String port;
	
	@Value("${spring.application.name}")
	String name;
	
	@GetMapping("/feign/getMap")
	public Map<String, Object> MapWhereParam()
	{
		Map<String, Object> map = new HashMap<>();
		map.put("apple", "苹果");
		map.put("water", "水");
		map.put("rice", "大米饭");
		return map;
	}
	
	/**
	 * 负载均衡
	 * @return
	 */
	@GetMapping("/feign/getPort")
	public String getPort()
	{
		return "我的name=" + name + "port=" + port;
	}

}

先启动Eureka Server服务,访问 http://localhost:1001/ ,再启动此项目,

 Eureka服务注册已经成功,测试接口,也是OK的。


新建项目2

为了省事,把项目1直接拷贝就行,你们随意哈. 改下配置文件的端口,然后启动,访问Eureka

新建项目3

application.yml

server:
  port: 2003
  
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:dev-feign-server-test:2003
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1
    
spring:
  application:
    name: dev-feign-server-test

 启动类

package com.portal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient //开启服务注册
@EnableFeignClients//开启Feign功能
public class SpringcloudFeignServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudFeignServerApplication.class, args);
	}

}

注:feign的消费服务方式,是基于接口的,@FeignClient中的value值是一个任意的客户端名称,用于创建Ribbon负载均衡器,如果不想使用Eureka中的服务实例名称,还可以使用url属性指定请求的URL,如下图中

另外一个知识点,@FeignClient所在的接口中,不支持@GetMapping等组合注解

 FeignMapper.java

package com.portal.mapper;

import java.util.Map;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "dev-feign-server") //方法一
//@FeignClient(value ="dev-feign-server",url="http://localhost:2001/") //方法二
public interface FeignMapper {
	
	/**
	 * Feign实现声明式调用
	 * @return
	 */
	@RequestMapping("/feign/getMap") //这里的地址必须是和调用的 “dev-feign-server”中接口地址一致
	public Map<String, Object> getMap2();
	
	/**
	 * 实现负载均衡
	 * @return
	 */
	@RequestMapping("/feign/getPort")//这里的地址必须是和调用的 “dev-feign-server”中接口地址一致
	public String getPort();

}

测试类 FeignController.java

package com.portal.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.portal.mapper.FeignMapper;

@RestController
public class FeignController {
	
	@Autowired
	FeignMapper feignMapper;
	
	/**
	 * Feign 声明式调用
	 * @return
	 */
	@GetMapping("/feign/getMap2")
	public Map<String, Object> MapWhereParam()
	{
		return feignMapper.getMap2();
	}
	
	/**
	 * 实现负载均衡
	 * @return
	 */
	@GetMapping("/feign/getPort")
	public String getPort()
	{
		return feignMapper.getPort();
	}

}

启动项目开始测试,多刷新几次页面,就能看出来区别啦


 

成功,礼花大炮哄起来,哈哈哈


总结:笔者写博客不仅仅是为了分享,也是为了给自己做笔记。

以上方法亲测有效,如果问题可下方回复,

不当之处,请多指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值