springcloud费话之Eureka接口调用(feign)

目录:

springcloud费话之Eureka基础

springcloud费话之Eureka集群

springcloud费话之Eureka服务访问(restTemplate)

springcloud费话之Eureka接口调用(feign)

springcloud费话之断路器(hystrix in feign)

springcloud费话之配置中心基础(SVN)

使用eureka服务发现实现服务器之间的http访问(feign)

使用restTemplate的访问方式还是比较复杂的,需要对其中的一些内容进行解析,增加了代码

因此在服务调用之间,希望恢复单应用的调用service一样简单,于是使用feign的调用方式

因为feign底层是使用了ribbon作为负载均衡的客户端,而ribbon的负载均衡也是依赖于eureka 获得各个服务的地址,所以要引入eureka-client,实际上上述的依赖并不需要更改

具体流程如下:

1.依赖

首先在springcloud官方网站上找到feign依赖,如下图

复制进pom

2.添加启动类注解

在启动类上添加注解@EnableFeignClients,添加后代码如下:

package com.lyh.lyh_eureka_server;

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;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaClientRun {

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

}

 3.创建feign的访问接口,代码如下:

package com.lyh.lyh_eureka_server.Interface;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
//这里为请求的服务器的spring的name
@FeignClient("eureka-client")
public interface FeignService {
  //映射名
    @RequestMapping("/getInfo")
    public String getInfo();
}

4.创建上述接口的实现类,部分代码如下:

t eurekaClient;

    @Resource
    private FeignService feignService;

    // 触发的接口 by feign
    @RequestMapping("/getInfoFromClientByFeign")
    public Object getInfoFromClientByFeign() throws URISyntaxException {
        String obj = feignService.getInfo();
        System.out.println(obj.toString());
        return obj;
    }

5.将如上代码复制到client2和client3中

6.测试

原文地址:https://www.cnblogs.com/liuyuhangCastle/p/11397262.html

时间: 2024-07-29 12:16:19

springcloud费话之Eureka接口调用(feign)的相关文章

springcloud费话之Eureka集群

  目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud费话之Eureka接口调用(feign) springcloud费话之断路器(hystrix in feign) 一.容灾server集群 复制上例中的server项目两个,分别命名为x-server2和x-server3,修改yml配置 ①端口:三个服务器的端口分别为9010,9011,901

springcloud费话之Eureka服务访问(restTemplate)

目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud费话之Eureka接口调用(feign) springcloud费话之断路器(hystrix in feign) springcloud费话之配置中心基础(SVN) 完成了Eureka的服务器集群的搭建后,需要相互调用访问,该访问主要分为两种内容的请求 ①对集群中的所有客户端的注册信息请求 ②对集群

SpringCloud系列研究---Eureka服务消费Feign

一.Feign简介 Feign是一种声明式.模板化的HTTP客户端.这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释.它具有可插入注释支持,包括Feign注释和JAX-RS注释.Feign还支持可插拔编码器和解码器.Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters.Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端.这

springcloud费话之断路器(hystrix in feign)

目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud费话之Eureka接口调用(feign) springcloud费话之断路器(hystrix in feign) 使用eureka服务发现实现服务器之间的http访问(feign)并添加断路器hystrix 断路器,是springcloud中的一种熔断机制的实现方式 熔断机制,是达到了某个异常以后,

Feign - HTTP接口调用- 单独使用 - 实战

目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -26[ 博客园 总入口 ] 写在前面 ? 大家好,我是作者尼恩.目前和几个小伙伴一起,组织了一个高并发的实战社群[疯狂创客圈].正在开始高并发.亿级流程的 IM 聊天程序 学习和实战 ? 在疯狂创客圈的 亿级流程的 IM 聊天程序 学习项目中,短连接Web

springBoot使用feign实现远程接口调用和错误熔断

1.第一步,新建两个简单的springboot项目并创建rest接口 demo系统的rest接口 plus系统的调用接口 2.在项目pom文件里导入feign和hystrix的pom依赖包 <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <!

调用feign的接口的几个注意点

注意: 当我们通过接口中的接口调用到其他服务中的controller时,人家服务方的controller需要接受的参数,我们在调用的时候也要声明,所以我们在接口中的写法也要写@RequestMappping(value = "", method = )注解 和@RequestParam  或者@RequestBody  等注解 原文地址:https://www.cnblogs.com/vegetableDD/p/11664134.html

微信公众号API测试——接口调用频率限制

接口频率限制[1] 公众号调用接口并不是无限制的.为了防止公众号的程序错误而引发微信服务器负载异常,默认情况下,每个公众号调用接口都不能超过一定限制,当超过一定限制时,调用对应接口会收到如下错误返回码: {"errcode":45009,"errmsg":"api freq out of limit"} 各接口调用频率限制如下: 接口 每日限额 获取access_token 2000 自定义菜单创建 1000 自定义菜单查询 10000 自定义菜

PHP 使用 curl_* 系列函数和 curl_multi_* 系列函数进行多接口调用时的性能对比

在页面中调用的服务较多时,使用并行方式,即使用 curl_multi_* 系列函数耗时要小于 curl_* 系列函数. 测试环境 操作系统:Windows 10 x64 Server:Apache 2.4.18 PHP:5.6.19 MySQL:5.7.11 cURL:7.47.1 测试数据库选择 MySQL 官方网站的样本数据库 sakila,下载地址:http://dev.mysql.com/doc/index-other.html 测试页面需要调用 3 个 api: getActorInf