服务调用有两种方式:

  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. Asp.Net Core采用MailKit部署到Linux Docker连接邮件服务器报错

    前段时间看文章了解到发邮件的SmtpClient已经过时了,微软官方推荐大家用其他解决方案,例如MailKit. https://docs.microsoft.com/zh-cn/dotnet/api ...

  2. EWS:邮箱的一个开放的接口服务

    https://3gstudent.github.io/3gstudent.github.io/Exchange-Web-Service(EWS)%E5%BC%80%E5%8F%91%E6%8C%87 ...

  3. Regex 提取字符串中重复数据且格式化显示

    方法:用   $下标    提取满足项的值 /** * 餐食信息格式转换 * @早餐,1@晚餐,2 => 早餐(1份):晚餐(2份) */ convertMealInfo = (mealInfo ...

  4. vb Replace 实现

    今天改一个VB程序时发现程序自带的replace 函数不知什么原因竟然不好用了 所以就自己写了一个玩玩 记录一下 'XGZ '替换字符 Private Function Replace1(ByVal ...

  5. Jenkins的使用(三)-------Publish over SSH和Publish over FTP

    七.构建后操作 1.使用Publish over SSH 1.左边菜单栏    Manage Jenkins --->ManagePlugins--->可选插件,然后搜索 Publish ...

  6. 使用k8s容器钩子触发事件

    原文: http://yunke.science/2018/04/15/k8s-hook/ 容器生命周期的钩子 Kubernetes为容器提供了生命周期钩子.钩子能使容器感知其生命周期内的事件,并且当 ...

  7. 1-Kong文章记录

    参考: https://www.cnblogs.com/duanxz/p/9770645.html 系列博客可参考: 开源API网关系统(Kong教程)入门到精通 https://www.cnblog ...

  8. GIT篇章(一)

    git的使用 创建代码版本 cd进入到自己希望存储代码的目录路径,并创建本地仓库.git[pycharm直接打开终端就是项目根目录了.无须cd了] 新创建的本地仓库.git是个空仓库 cd 目录路径 ...

  9. 面试题:android用户注册代码 密码需要加密传输吗

    答案是肯定的,至少比明文好 客户端注册和登录的时候:一个可行的方法是,客户端提交 md5(password) 密码(如上所述,此方法只是简单保护了密码,是可能被查表获取密码的). 注册的时候:服务端数 ...

  10. Easypoi实现单模板生成多页wrod文档

        EasyPoi可以很方便的通过一个word模板,然后通过填充模板的方式生成我们想要的word文档.但是碰到了一个单模板生成多页数据的场景,比如一个订单详情信息模板,但是有很多订单,需要导入到一 ...