<<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)的更多相关文章

  1. 配置中心Nacos(服务发现)

    服务演变之路 单体应用架构 在刚开始的时候,企业的用户量.数据量规模都⽐较⼩,项⽬所有的功能模块都放在⼀个⼯程中编码.编译.打包并且部署在⼀个Tomcat容器中的架构模式就是单体应用架构,这样的架构既 ...

  2. 【Day01】Spring Cloud入门-架构演进、注册中心Nacos、负载均衡Ribbon、服务调用RestTemplate与OpenFeign

    〇.课程内容 课程规划 Day1 介绍及应用场景 Day2 组件介绍及 广度 Day3 设计思想.原理和源码 Day4 与容器化的容器(服务迁移.容器编排) 一.业务架构的演进 1.单体架构时代 缺陷 ...

  3. 实战二:nacos服务注册与发现,openfeign服务调用

    一,参照上一篇创建好微服务结构后,按业务需求编写各微服务逻辑 二,服务注册 1,安装nacos:下载,解压,运行startup.cmd 2,访问 http://localhost:8848/nacos ...

  4. 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  5. 【Day02】Spring Cloud组件的使用--Nacos配置中心、sentinel流量控制、服务网关Gateway、RocketMQ、服务调用链路(Sleuth、zipkin)

    今日内容 一.配置中心 1.遗留问题 yml配置,每一次都需要重启项目 需要不重启项目拿到更新的结果 引出:配置中心 选择:Spring Cloud Config组件 / Alibaba的Nacos( ...

  6. SpringCloud使用Nacos服务发现实现远程调用

    本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...

  7. spring boot2X整合nacos一使用Feign实现服务调用

    服务调用有两种方式: A.使用RestTemplate 进行服务调用 查看 B.使用Feign 进行声明式服务调用 上一次写了使用RestTemplate的方式,这次使用Feign的方式实现 服务注册 ...

  8. SpringCloud Alibaba实战(8:使用OpenFeign服务调用)

    源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一个章节,我们已经成功地将服务注册到了Nacos注册中心,实现了服务注册和服务发 ...

  9. SpringCloud微服务服务间调用之OpenFeign介绍

    开发微服务,免不了需要服务间调用.Spring Cloud框架提供了RestTemplate和FeignClient两个方式完成服务间调用,本文简要介绍如何使用OpenFeign完成服务间调用. Op ...

  10. SpringCloud Nacos + Ribbon 调用服务的 2 种方法!

    在 Nacos 中,服务调用主要是通过 RestTemplate + Ribbon 实现的,RestTemplate 是 Spring 提供的 Restful 请求实现类,而 Ribbon 是客户端负 ...

随机推荐

  1. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

    当我们拿到一天别人用的电脑,里面是上一位前辈的git ssh key,这时候我们要自己改,改完之后上传代码到远程repository时, 可能会报git@github.com: Permission ...

  2. java 常用的jar包下载地址

    Eclipse: http://www.eclipse.org/downloads/packages/all Spring: http://Framework: http://repo.spring. ...

  3. 真正“搞”懂HTTP协议06之body的玩法(理论篇)

    本来啊,本来,本来我在准备完善这个鸽了四年的系列的时候,是打算按照时间的顺序来完成的,好吧.我承认那个时候考虑的稍稍稍稍稍微有些不足,就是我忽略了HTTP协议的"模块性".因为虽然 ...

  4. CTF中RSA常见类型解法

    Python脚本 #十六进制转ASCII编码 import binascii print(binascii.unhexlify(hex(m)[2:])) #rsa import gmpy2 phi = ...

  5. 解决笔记本安装centos7后无法调节屏幕亮度

    起因:本人有台老古董笔记本,大约是10年前左右了,三星rv411,配置较低无法安装win7以上的系统.装个CentOS7正好可以拿来学习Linux系统. 但是遇到一个特别恶心的情况,笔记本上调节屏幕亮 ...

  6. History模式的配置细节

    旧文从语雀迁移过来,原日期:2021-09-13 前言 我们知道,vue 单页面应用打包出来是静态资源,一般需要 nginx 或者其他服务器访问:当如果 Vue Router 是采用 History ...

  7. .Net 7 的AOT的程序比托管代码更容易破解?

    楔子 .Net 7的一个重要功能是把托管的源码编译成Native Code,也就是二进制文件.此举看似增加了程序反编译难度,实际上是减少了程序的破解难度.本篇在不触及整个程序架构的前提下,以简单的例子 ...

  8. Java的深拷贝和浅拷贝的区别

    一.拷贝的引入 (1).引用拷贝 创建一个指向对象的引用变量的拷贝. Teacher teacher = new Teacher("Taylor",26); Teacher oth ...

  9. CVE-2020-13933

    漏洞名称 Apache Shiro 身份验证绕过漏洞复现CVE-2020-13933 利用条件 Apache Shiro < 1.6.0 漏洞原理 Apache Shiro是一个强大且易用的Ja ...

  10. 万字干货! 使用docker部署jenkins和gitlab

    阅读本文, 需要有基础的Git, Linux, Docker, Java, Maven, shell知识, 并最少有一台内存16G以上并已经安装好了Docker的机器. 1. 概述 2. 容器互联 3 ...