惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
Spread Privacy
Spread Privacy
月光博客
月光博客
L
LINUX DO - 热门话题
C
Cisco Blogs
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿
B
Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
The Cloudflare Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
J
Java Code Geeks
罗磊的独立博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
Simon Willison's Weblog
Simon Willison's Weblog
量子位
H
Help Net Security
The Register - Security
The Register - Security
L
LangChain Blog
GbyAI
GbyAI
Vercel News
Vercel News
S
Schneier on Security
T
Threatpost
T
Threat Research - Cisco Blogs
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
Recorded Future
Recorded Future
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog

博客园 - smileNicky

魔珐星云SDK实战测评:重构数字人交互的底层逻辑 Ling Studio 深度体验:当万亿参数大模型遇上AI原生IDE,编程范式正在重构 魔珐星云SDK实战测评:从0到1搭建会“思考+互动”的智能数字人客服应用 云电脑玩转 CANN 全攻略:从环境搭建到创新应用落地 SpringBoot系列之集成EasyExcel实现百万级别的数据导入导出实践 分布式ID生成方案总结整理 并发编程系列之如何正确使用线程池? Spring Cloud Alibaba系列之分布式服务组件Dubbo Spring5.0源码学习系列之事务管理概述 Spring5.0源码学习系列之Spring AOP简述 利用ADB命令强制卸载oppo自带浏览器 SpringBoot系列之从入门到精通系列教程 SpringCloud系列之API网关(Gateway)服务Zuul SpringCloud系列之服务容错保护Netflix Hystrix SpringCloud系列之客户端负载均衡Netflix Ribbon SpringCloud系列使用Eureka进行服务治理 Spring Security系列之极速入门与实践教程 SpringBoot系列之actuator监控管理极速入门与实践 SpringBoot系列之IDEA项目中设置热部署教程 SpringBoot系列之发送邮件极速入门与实践
SpringCloud系列之使用Feign进行服务调用
smileNicky · 2020-07-28 · via 博客园 - smileNicky

上一章的学习中,我们知道了微服务的基本概念,知道怎么基于Ribbon+restTemplate的方式实现服务调用,接着上篇博客,我们学习怎么基于Feign实现服务调用,请先学习上篇博客,然后再学习本篇博客

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

环境准备:

  • JDK 1.8
  • SpringBoot2.2.3
  • SpringCloud(Hoxton.SR6)
  • Maven 3.2+
  • 开发工具
    • IntelliJ IDEA
    • smartGit

创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200728115035940.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200727152558705.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200728130335828.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)

  port: 8083
spring:
  application:
    name: feign-service-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    healthcheck:
      enabled: false
  instance:
    status-page-url-path: http://localhost:8761/actuator/info
    health-check-url-path: http://localhost:8761/actuator//health
    prefer-ip-address: true
    instance-id: feign-service-consumer8083

@FeignClient指定服务名称,@RequestMapping指定要调用的接口

package com.example.springcloud.client.service;

import com.example.springcloud.client.bean.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * <pre>
 *  UserService
 * </pre>
 *
 * <pre>
 * @author mazq
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2020/07/28 13:09  修改内容:
 * </pre>
 */
@FeignClient(value = "EUREKA-SERVICE-PROVIDER")
@Service
public interface UserService {
    @RequestMapping(value = "/api/users/{username}",method = RequestMethod.GET)
    User findGithubUser(@PathVariable("username")String username);

}

加上@EnableEurekaClient@EnableFeignClients,写个接口进行测试

package com.example.springcloud.client;

import com.example.springcloud.client.bean.User;
import com.example.springcloud.client.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
public class SpringcloudFeignClientApplication {

    @Autowired
    UserService userService;

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

    @GetMapping("/findUser/{username}")
    public User index(@PathVariable("username")String username){
        return userService.findGithubUser(username);
    }
}

要运行eureka服务端,eureka服务提供者,代码请参考上一章博客

补充:IDEA中多实例运行方法

step1:如图,不要加上勾选
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200728135146506.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)

step2:指定不同的server端口和实例id,如图:
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200728141342694.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)

服务注册,可以看到两个实例
![在这里插入图片描述]( https://img-blog.csdnimg.cn/2020072814131056.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)
ok,启动eureka server(服务注册中心),eureka服务提供者端,和feign服务消费者端
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200728141443532.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)
http://localhost:8083/findUser/mojombo
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200728134055243.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70)
附录:

ok,本博客参考官方教程进行实践,仅仅作为入门的学习参考资料,详情可以参考Spring Cloud官方文档https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.0.RELEASE/reference/html/

代码例子下载:code download

优质学习资料参考: