微服务的服务消费,一般是使用 feign 和 rebion 调用服务提供,进行服务的消费,本文将实战使用代码讲解服务的消费。

微服务环境的搭建

创建一个 springboot 项目,springboot 是将服务进行拆分的一个最小服务单位。

添加 maven 依赖

  • 基本的maven依赖
        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  • rebion 依赖
            <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
  • feign 依赖
           <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

feign依赖包含rebion依赖,如果想同时使用 feign 和 rebion 依赖,只需要引用feign 即可。

添加application.yml

spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

添加Appcation.java启动类

@SpringBootApplication
//将服务注册到注册中心
@EnableDiscoveryClient
//扫描和注册feign客户端bean定义
@EnableFeignClients
public class NacosConsumeApplication { public static void main(String[] args) {
SpringApplication.run(NacosConsumeApplication.class, args);
} }

其中 @EnableDiscoveryClient 将服务注册到注册中心,@EnableFeignClients 扫描和注册feign客户端bean定义。fegin bean定义是 @FeignClient。

服务调用 : feign 和 ribbon

1. feign

  • 创建feign客户端
@FeignClient(value = "service-provider")
public interface ProductClient { @GetMapping("/hello")
String product(@RequestParam("name") String name);
}

这里 @FeignClient 就是创建bean 给 @EnableFeignClients 扫描的注册。 @FeignClient 里面的配置 value 对应的是服务提供者的服务名称,@GetMapping里面的value对应服务提供者的 @GetMapping 路径。

这样做的好处是这里直接配置好路径和服务名称,不需要调用方做任何的配置。

  • 创建请求调用 feign客户端
@RestController
public class FeignController { //这个错误是编译器问题。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
@Autowired
private ProductClient productClient; @GetMapping("/feign")
public String feign(String name){
return productClient.product(name);
} }

2. rebion

  • 配置 RestTemplate bean
@Configuration
public class RibbonConfig { @LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
  • 调用服务
@RestController
public class RibbonController { @Autowired
private RestTemplate restTemplate; @GetMapping("/ribbon")
public String ribbon(String name){
String result = restTemplate.getForObject("http://service-provider/hello?name="+name,String.class);
return result;
}
}

feign 和 rebion 优缺点

  • feign 配置比较简单明了,配置好了 FeignClient,直接调用即可,不过后续有多余的配置。
  • rebion每次都需要配置请求路径,它的优点在于直接调用即可,不要单独配置客户端。

Spring Cloud Alibaba 使用 feign 和 rebion 进行服务消费的更多相关文章

  1. Spring Cloud Alibaba(四)实现Dubbo服务消费

    本项目演示如何使用 Spring Cloud Alibaba 完成 Dubbo 的RPC调用. Spring Cloud与Dubbo Spring Cloud是一套完整的微服务架构方案 Dubbo是国 ...

  2. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  4. Spring Cloud Alibaba(8)---Feign服务调用

    Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...

  5. Spring Cloud Alibaba 使用Feign进行服务消费

    为什么使用Feign? Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样.你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做. 使用Fei ...

  6. Spring Cloud下使用Feign Form实现微服务之间的文件上传

    背景 ​ Spring Cloud现在已经被越来越多的公司采用了,微服务架构比传统意义上的单服务架构从复杂度上多了很多,出现了很多复杂的场景.比如,我们的产品是个app,支持第三方登录功能,在手机端调 ...

  7. Spring Cloud与Dubbo的完美融合之手「Spring Cloud Alibaba」

    很早以前,在刚开始搞Spring Cloud基础教程的时候,写过这样一篇文章:<微服务架构的基础框架选择:Spring Cloud还是Dubbo?>,可能不少读者也都看过.之后也就一直有关 ...

  8. 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例

    gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...

  9. 0.9.0.RELEASE版本的spring cloud alibaba nacos实例

    简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...

随机推荐

  1. Deprecated: __autoload() is deprecated, use spl_autoload_register()

    Deprecated: __autoload() is deprecated, use spl_autoload_register() 解决:可能原因PHP版本过高,亲测discuz3.4版本使用ph ...

  2. [原创]OpenEuler20.03安装配置PostgreSQL13.4详细图文版

    OpenEuler安装配置PostgreSQL 编写时间:2021年9月18日 作者:liupp 邮箱:liupp@88.com 序号 更新内容 更新日期 更新人 1 完成第一至三章内容编辑: 202 ...

  3. Jmeter扩展组件开发(8) - 函数助手扩展开发demo

    前提条件 1.pom文件引用ApacheJMeter_functions包 <dependency> <groupId>org.apache.jmeter</groupI ...

  4. 使用python3中的2to3.py执行数据迁移

    1.在python默认安装的位置找到Tools\scripts 2.找到2to3.py 3.在所在文件夹shift+右键打开终端 4.执行命令python 2to3.py -w 需要做数据迁移的数据路 ...

  5. 项目部署(ubuntu+uwsgi+nginx+supervisor+django)

    一.在开发机上的准备工作 1. 确认项目没有bug. 2.设置`ALLOW_HOST`为你的域名,以及ip地址. 4.设置`DEBUG=False`,避免如果你的网站产生错误,而将错误信息暴漏给用户. ...

  6. AT2363-[AGC012C]Tautonym Puzzle【构造】

    正题 题目链接:https://www.luogu.com.cn/problem/AT2363 题目大意 给出\(n\),要求构造一个字符串\(s\),使得能够找出恰好\(n\)个子序列使得这个子序列 ...

  7. Flawfinder在Python2和Python3环境下对代码进行扫描方法

    1. Flawfinder Flawfinder是一款开源的关于C/C++静态扫描分析工具,其根据内部字典数据库进行静态搜索,匹配简单的缺陷与漏洞. 官网:https://dwheeler.com/f ...

  8. SQL Server附加数据库错误5120处理方法

    SQL Server附加数据库5120错误 当我们从另外一台服务器复制过来的数据库,可能会有如下错误: 解决方法 1.给数据库所在文件夹增加用户Everyone并赋予完全控制权限 2.以管理员身份运行 ...

  9. 智汀家庭云-开发指南Golang: 插件模块

    插件模块 当前所说的插件仅指设备类插件,插件为SA提供额外的设备发现和控制功能: 插件通过实现定义的grpc接口,以grpc服务的形式运行,提供接口给SA调用 插件同时需要http服务提供h5页面及静 ...

  10. 用Flask 实现文件服务器(包含docker版本)

    最近有了公司局域网内共享axure原型的需求,所以用Flask开发了一款文件上传/查看工具,记录一下其中的问题和解决方案 这个工具参照了一位大神的uploads工具 https://zhuanlan. ...