Nacos服务调用(基于Openfeign)
在<<Nacos服务注册>>这篇文章里,我搭建了一个nacos服务中心,并且注册了一个服务,下面我们来看在上一篇文章的基础上,怎样用Openfeign来调用这个服务。
0.同上篇,启动nacos
1.搭建alibaba spring cloud脚手架
访问https://start.aliyun.com/bootstrap.html,GroupID: com.alibaba.cloud,Artifact:nocos-discovery-consumer-sample。,选择依赖:Nacos Service Discovery,Spring Web,Cloud Bootstrap.
2.在pom文件中引入spring-cloud-starter-loadbalancer,注意一定要引入loadbalancer,否则用openfeign调用会报错。
完整的pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>nocos-discovery-consumer-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nocos-discovery-consumer-sample</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<spring-cloud.version>2021.0.4</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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>3.0.1</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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3.配置application.properties
spring.application.name=nocos-discovery-consumer-sample
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.namespace=public
spring.main.allow-circular-references=true
server.port=9091
注意这一行:spring.cloud.nacos.discovery.server-addr=localhost:8848 ,表明这是nacos服务中心的地址。
4.新建一个Openfeign调用类:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; @FeignClient("nocos-discovery-provider-sample")
@LoadBalancerClient("nocos-discovery-provider-sample")
public interface EchoService { @GetMapping("/echo/{message}")
public String callEcho(@PathVariable String message); }
这里要注意的是标注@LoadBalancerClient("nocos-discovery-provider-sample"),nocos-discovery-provider-sample就是上一篇中服务提供方注册到nacos的名称.
5.新建一个Controller:
@RestController
public class RestTemplateController { @Autowired
private EchoService echoService; @GetMapping("/call/echo/{message}")
public String callEcho(@PathVariable String message) { //openFeign test
return "From openFeign :"+echoService.callEcho(message); }
}
6.SpringBoot启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NocosDiscoveryConsumerSampleApplication { public static void main(String[] args) {
SpringApplication.run(NocosDiscoveryConsumerSampleApplication.class, args);
} }
记得要加标注@EnableFeignClients
7.访问http://localhost:9091/call/echo/hello
Nacos服务调用(基于Openfeign)的更多相关文章
- 配置中心Nacos(服务发现)
服务演变之路 单体应用架构 在刚开始的时候,企业的用户量.数据量规模都⽐较⼩,项⽬所有的功能模块都放在⼀个⼯程中编码.编译.打包并且部署在⼀个Tomcat容器中的架构模式就是单体应用架构,这样的架构既 ...
- 【Day01】Spring Cloud入门-架构演进、注册中心Nacos、负载均衡Ribbon、服务调用RestTemplate与OpenFeign
〇.课程内容 课程规划 Day1 介绍及应用场景 Day2 组件介绍及 广度 Day3 设计思想.原理和源码 Day4 与容器化的容器(服务迁移.容器编排) 一.业务架构的演进 1.单体架构时代 缺陷 ...
- 实战二:nacos服务注册与发现,openfeign服务调用
一,参照上一篇创建好微服务结构后,按业务需求编写各微服务逻辑 二,服务注册 1,安装nacos:下载,解压,运行startup.cmd 2,访问 http://localhost:8848/nacos ...
- 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- 【Day02】Spring Cloud组件的使用--Nacos配置中心、sentinel流量控制、服务网关Gateway、RocketMQ、服务调用链路(Sleuth、zipkin)
今日内容 一.配置中心 1.遗留问题 yml配置,每一次都需要重启项目 需要不重启项目拿到更新的结果 引出:配置中心 选择:Spring Cloud Config组件 / Alibaba的Nacos( ...
- SpringCloud使用Nacos服务发现实现远程调用
本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...
- spring boot2X整合nacos一使用Feign实现服务调用
服务调用有两种方式: A.使用RestTemplate 进行服务调用 查看 B.使用Feign 进行声明式服务调用 上一次写了使用RestTemplate的方式,这次使用Feign的方式实现 服务注册 ...
- SpringCloud Alibaba实战(8:使用OpenFeign服务调用)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一个章节,我们已经成功地将服务注册到了Nacos注册中心,实现了服务注册和服务发 ...
- SpringCloud微服务服务间调用之OpenFeign介绍
开发微服务,免不了需要服务间调用.Spring Cloud框架提供了RestTemplate和FeignClient两个方式完成服务间调用,本文简要介绍如何使用OpenFeign完成服务间调用. Op ...
- SpringCloud Nacos + Ribbon 调用服务的 2 种方法!
在 Nacos 中,服务调用主要是通过 RestTemplate + Ribbon 实现的,RestTemplate 是 Spring 提供的 Restful 请求实现类,而 Ribbon 是客户端负 ...
随机推荐
- Spring04:JdbcTemplate及事务控制(AOP、XML、注解)
今日内容 Spring中的JdbcTemplate 作业:Spring基于AOP的事务控制 Spring中的事务控制 基于XML的 基于注解的 一.JdbcTemplate 1.JdbcTemplat ...
- 【离线数仓】Day02-用户行为数据仓库:分层介绍、环境搭建(hive、tez)、LZO压缩、建表查询导入加索引、编写脚本
一.数仓分层概念 1.为什么要分层 ODS:原始数据层 DWD层:明细数据层 DWS:服务数据层 ADS:数据应用层 2.数仓分层 3.数据集市与数据仓库概念 4.数仓命名规范 ODS层命名为odsD ...
- 【每日一题】2021年12月11日-69. Sqrt(x)/x的平方根
给你一个非负整数 x ,计算并返回 x 的 算术平方根 . 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 . 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或 ...
- Hexo+next主题美化
前言 需要在Hexo下配置next主题 Hexo配置next主题教程:https://www.cnblogs.com/xuande/p/16641543.html 更改配置以后使用素质三连:hexo ...
- Apache Kafka 移除 ZK Proposals
Zookeeper 和 KRaft 这里有一篇 Kafka 功能改进的 proposal 原文.要了解移除 ZK 的原因,可以仔细看看该文章.以下是对该文章的翻译. 动机 目前,Kafka 使用 Zo ...
- Kubernetes的垂直和水平扩缩容的性能评估
Kubernetes的垂直和水平扩缩容的性能评估 译自:Performance evaluation of the autoscaling strategies vertical and horizo ...
- [OpenCV实战]21 使用OpenCV的Eigenface
目录 1 PCA 1.1 方差是什么 1.2 什么是PCA 1.3 什么是矩阵的特征向量和特征值? 1.4 如何计算PCA 2 什么是EigenFaces? 2.1 将图像作为向量 2.2 如何计算如 ...
- Git操作不规范,战友提刀来相见!
年终奖都没了,还要扣我绩效,门都没有,哈哈. 这波骚Git操作我也是第一次用,担心闪了腰,所以不仅做了备份,也做了笔记,分享给大家. 文末留言,聊聊你的年终奖. 问题描述 小A和我在同时开发一个功能模 ...
- [Java]内存回收机制框架图
具体解释下面这篇博客总结的已经非常好了,我就不复制了: http://www.cnblogs.com/cielosun/p/6674431.html#12-%E5%8F%AF%E8%BE%BE%E6% ...
- 常用的SQL命令:
丢弃指定的数据库,如果存在的话 DROP DATABASE IF EXISTS xuezi; 创建新的数据库 CREATE DATABASE xuezi; 进入数据库xuezi USE xue ...