服务调用有两种方式:

  A.使用RestTemplate 进行服务调用 查看

  B.使用Feign 进行声明式服务调用

上一次写了使用RestTemplate的方式,这次使用Feign的方式实现

服务注册发现中心使用nacos

启动nacos

spring boot 版本 2.2.1.RELEASE

1.服务端

provider

(1)添加依赖

    <properties>
<java.version>1.8</java.version>
<nacos.version>2.1..RELEASE</nacos.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)修改配置

server.port=

spring.application.name=service-provider

spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:
spring.cloud.nacos.discovery.service=${spring.application.name} management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

(3)测试方法

package com.xyz.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
} }

provider1

修改端口为8011

修改测试方法

package com.xyz.provider1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,another provider";
} }

启动provider和provider1

2.客户端

customer

(1)添加依赖

   <properties>
<java.version>1.8</java.version>
<nacos.version>2.1..RELEASE</nacos.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)配置

server.port=
spring.application.name=service-comsumer
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:
spring.cloud.nacos.discovery.service=${spring.application.name}

(3)修改启动类

添加注解 @EnableFeignClients,开启扫描Spring Cloud Feign客户端的功能

package com.xyz.comsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableFeignClients
@SpringBootApplication
public class ComsumerApplication { public static void main(String[] args) {
SpringApplication.run(ComsumerApplication.class, args);
} }

(4)添加Feign接口

创建RemoteHelloService接口,来定义OpenFeign要调用的远程服务接口

通过@FeginClient注解指定被调用方的服务名

通过fallback属性指定RemoteHelloServiceFallbackFactory类,来进行远程调用的熔断和降级处理。

provider是要调用的服务名

说明:

  添加跟调用目标方法一样的方法声明,必须跟目标方法的定义一致

RemoteHelloService

package com.xyz.comsumer.feign;

import com.xyz.comsumer.feign.factory.RemoteHelloServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; @FeignClient(contextId = "remotehelloService", value = "service-provider", fallbackFactory = RemoteHelloServiceFallbackFactory.class)
public interface RemoteHelloService { @GetMapping("/hello")
String hello(); }

RemoteHelloServiceFallbackImpl

package com.xyz.comsumer.feign.fallback;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; @Component
public class RemoteHelloServiceFallbackImpl implements RemoteHelloService {
private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class);
private Throwable cause; @Override
public String hello() {
logger.error("feign 查询信息失败:{}", cause);
return null;
} public Throwable getCause() {
return cause;
} public void setCause(Throwable cause) {
this.cause = cause;
} }

RemoteHelloServiceFallbackFactory

package com.xyz.comsumer.feign.factory;

import com.xyz.comsumer.feign.RemoteHelloService;
import com.xyz.comsumer.feign.fallback.RemoteHelloServiceFallbackImpl;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component; @Component
public class RemoteHelloServiceFallbackFactory implements FallbackFactory<RemoteHelloService> { @Override
public RemoteHelloService create(Throwable throwable) {
RemoteHelloServiceFallbackImpl remoteFallback = new RemoteHelloServiceFallbackImpl();
remoteFallback.setCause(throwable);
return remoteFallback;
}
}

要使用Feign的fallback机制,需要开启Feign的Hystrix的功能

增加配置

feign.hystrix.enabled=true

(4)服务调用

注入刚才声明的ProviderService,就可以像本地方法一样进行调用了

package com.xyz.comsumer.controller;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class FeignController {
@Autowired
RemoteHelloService remoteHelloService;
@RequestMapping("feignTest")
public String feignTest(){
return remoteHelloService.hello();
}
}

启动customer

访问http://localhost:8015/call

交替返回结果

  hello,provider 或 hello,another provider

spring boot2X整合nacos一使用Feign实现服务调用的更多相关文章

  1. spring boot2X整合Consul一使用RestTemplate实现服务调用

    Consul可以用于实现分布式系统的服务发现与配置 服务调用有两种方式: A.使用RestTemplate 进行服务调用 负载均衡——通过Ribbon注解RestTemplate B.使用Feign ...

  2. Spring Cloud 整合 nacos 实现动态配置中心

    上一篇文章讲解了Spring Cloud 整合 nacos 实现服务注册与发现,nacos除了有服务注册与发现的功能,还有提供动态配置服务的功能.本文主要讲解Spring Cloud 整合nacos实 ...

  3. Spring Cloud系列之使用Feign进行服务调用

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

  4. spring boot2X整合Consul一服务注册与发现

    Consul 是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置. 关键特性: 服务注册/发现 数据强一致性保证 多数据中心 健康检查 key/value存储 1.下载 htt ...

  5. Spring Cloud Alibaba系列(三)使用feign进行服务调用

    什么是Feign Feign是spring cloud提供的一个声明式的伪http客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一天注解即可. Nacos很好的兼容了Fe ...

  6. Spring Cloud(十二)声名式服务调用:Feign 的使用(下)

    前言 本文是对上一篇博文的扩充,很多平时用不到的特性就开始简略一写,Spring Cloud各版本之间的差距很大的,用不到的可能下一个版本就被kill掉了.由于笔者写本文开始的时候误解了Feign的继 ...

  7. Feign实现服务调用

    上一篇博客我们使用ribbon+restTemplate实现负载均衡调用服务,接下来我们使用feign实现服务的调用,首先feign和ribbon的区别是什么呢? ribbon根据特定算法,从服务列表 ...

  8. VUE开发(一)Spring Boot整合Vue并实现前后端贯穿调用

    文章更新时间:2020/03/14 一.前言 作为一个后端程序员,前端知识多少还是要了解一些的,vue能很好的实现前后端分离,且更便于我们日常中的调试,还具备了轻量.低侵入性的特点,所以我觉得是很有必 ...

  9. feign微服务调用携带浏览器信息(header、cookie)

    import feign.RequestInterceptor; import feign.RequestTemplate; import org.apache.commons.collections ...

随机推荐

  1. codis集群搭建笔记

    一.安装Linux虚拟机 二.安装go运行环境 https://www.cnblogs.com/xmzncc/p/6218694.html wget http://mirrors.flysnow.or ...

  2. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  3. Java向MySQL新增记录时间误差问题

    参考文档 https://www.jianshu.com/p/115861aad147 https://blog.csdn.net/ai932820942/article/details/845804 ...

  4. because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checkin

    1 前言 浏览器报错误(chrome和firefox都会):because its MIME type ('text/html') is not a supported stylesheet MIME ...

  5. 转:Windows系统环境下安装dlib

    原文链接 因为今天安装Face Recognition,需要先按照 dlib .需要在windows环境下做一些图片处理,所以需要在pycharm中配置环境,而其中需要的主要是dlib的安装: 下面说 ...

  6. sql server 大数据, 统计分组查询,数据量比较大计算每秒钟执行数据执行次数

    -- 数据量比较大的情况,统计十分钟内每秒钟执行次数 ); -- 开始时间 ); -- 结束时间 declare @num int; -- 结束时间 set @begintime = '2019-08 ...

  7. 【前端_React】npm常用命令

    安装模块(包): //全局安装 $ npm install 模块名 -g //本地安装 $ npm install 模块名 //一次性安装多个 $ npm install 模块1 模块2 模块n -- ...

  8. JavaScript中数组去重汇总

    1. 简单的去重方法,利用数组的indexOf下标属性来查询 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ func ...

  9. 目标检测论文解读11——Mask R-CNN

    目的 让Faster R-CNN能做实例分割的任务. 方法 模型的结构图如下. 与Faster R-CNN相比,主要有两点变化. (1) 用RoI Align替代RoI Pool. 首先回顾一下RoI ...

  10. Java 包扫描器

    包扫描器 获取一个包下的所有类,然后使用默认的类加载器加载到内存中 public static List<Class<?>> scanByPackage(String pack ...