31.nacos集成Feign和Gateway实例(springcloud)

news2024/9/20 21:36:06

一、项目nacos-client-a

 

1.pom.xml文件

新增了springcloud的依赖

新增了springcloud的依赖管理

 新增了feign依赖

 

<?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>
    <groupId>com.it</groupId>
    <artifactId>nacos-client-a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-client-a</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.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-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.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>
            <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>

    <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>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.it.NacosClientAApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.application.yml

3. feign目录下的TestFeign接口

 4.controller目录下的TestController

package com.it.controller;

import com.it.feigin.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class TestController {

    @Autowired
    public DiscoveryClient discoveryClient;

    @Autowired
    public TestFeign testFeign;



    @GetMapping("test")
    public String test(){
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos-client-b");
        System.out.println(instances);
        return testFeign.info();
    }

}

5.主函数启动类

二、项目nacos-client-b

 

1.pom.xml文件

不需要额外添加springcloud的依赖,使用springcloudalibaba即可

<?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>
    <groupId>com.it</groupId>
    <artifactId>nacos-client-b</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-client-b</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.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>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.it.NacosClientBApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.application.yml

3.BController

 4.主函数启动类

 

分别启动项目一和项目二进行测试

只有在同一个命名空间,同一个分组下的项目模块才能互相调用,测试其他情况均不能调用

 

三、新建gateway模块

 1.pom.xml文件

新增springcloud的依赖

2.application.yml

3.主函数启动类

同时运行三个项目

如果不运行动态路由模块,使用localhost/nacos-client-a会导致页面报错,无法正常访问

 同时启动三个项目

再次访问该路径

 至此,nacos 做注册中心,服务发现,以及远程调用都完成了

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/22048.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ArcMap10.6以上版本添加天地图底图

文章目录 申请天地图服务Key在ArcMap10.7中添加天地图服务注意点 申请天地图服务Key 天地图API&#xff1a;http://lbs.tianditu.gov.cn/server/MapService.html 需要登录后进入控制台&#xff0c;申请免费的Key&#xff1a; 在ArcMap10.7中添加天地图服务 天地图API提供…

(十)Spring之回顾反射机制

文章目录反射机制四要素Spring反射机制底层原理上一篇&#xff1a;&#xff08;九&#xff09;Spring之Bean的循环依赖问题反射机制四要素 反射机制调用方法&#xff0c;一般涉及到4个要素&#xff1a; 调用哪个对象的哪个方法传什么参数返回什么值 一般分为这几个步骤&…

Mysql语法二:表的增删改查(简单查询)

目录 1.新增&#xff08;Create) C 1.1 单行数据全列插入 1.2&#xff1a;多行新增指定列插入 1.3&#xff1a;思考题 2.查询&#xff08;Retrieve&#xff09;R 简单查询 2.1&#xff1a;指定列查询 2.2&#xff1a;查询字段为表达式 2.3&#xff1a;别名 as 2.4&…

计算机专业毕业设计演示视频(论文+系统)_kaic

演示链接https://ssm2.oss-cn-beijing.aliyuncs.com/jspSSM201%E5%A4%A7%E5%AD%A6%E7%94%9F%E7%AC%AC%E4%BA%8C%E8%AF%BE%E5%A0%82%E5%AD%A6%E5%88%86%E6%88%90%E7%BB%A9%E6%B4%BB%E5%8A%A8%E6%8A%A5%E5%90%8Dvue.mp4https://ssm2.oss-cn-beijing.aliyuncs.com/jspSSM205%E6%97…

操作系统之进程

操作系统 操作系统图解 这个图详细说明了计算机整个框架&#xff0c;系统调用&#xff0c;操作系统内核和驱动程序三个统称为操作系统&#xff0c;应用程序通过操作系统提供的api来调用硬件设备&#xff0c;而对于硬件设别来说&#xff0c;每个计算机的硬件设备的种类和厂家不…

RNA-seq 保姆教程:差异表达分析(一)

介绍 RNA-seq 目前是测量细胞反应的最突出的方法之一。RNA-seq 不仅能够分析样本之间基因表达的差异&#xff0c;还可以发现新的亚型并分析 SNP 变异。本教程[1]将涵盖处理和分析差异基因表达数据的基本工作流程&#xff0c;旨在提供设置环境和运行比对工具的通用方法。请注意&…

L2搭载率连续两个月站上30%大关,车企加速产业链整合

进入新的行业发展周期&#xff0c;车企的智能化挑战越来越大&#xff0c;也催生新一轮整合热潮。对于全球数百家中小型智能汽车技术公司来说&#xff0c;「上岸」时机已经到来。 本周&#xff0c;全球第四大汽车制造商Stellantis宣布&#xff0c;收购总部位于匈牙利的人工智能…

在 Solidity 中 ++i 为什么比 i++ 更省 Gas?

前言 作为一个初学者&#xff0c;“在 Solidity 中 i 为什么比 i 更省 Gas&#xff1f;” 这个问题始终在每个寂静的深夜困扰着我。也曾在网上搜索过相关问题&#xff0c;但没有得到根本性的解答。最终决定扒拉一下它们的字节码&#xff0c;从较为底层的层面看一下它们的差别究…

多进程编程

系列文章目录 多进程编程 VS 多线程编程_crazy_xieyi的博客-CSDN博客 文章目录 前言一、进程创建二、进程等待前言 Java对操作系统提供的多进程编程接口这些操作进行了限制&#xff0c;最终给用户只提供了两个操作&#xff1a;进程创建和进程等待。 一、进程创建 创建出一个…

Android 基础知识3-1项目目录结构

上一章我们创建了Hello Word项目&#xff0c;代码是由ADT插件自动生成的&#xff0c;我们没有对其进行编码&#xff0c;所以没有对其框架进行分析。其实每一个平台都有自己的结构框架&#xff0c;所以我们对Android项目的结构也进行分析。 与一般的Java项目一样&#xff0c;src…

Qt 学习(二) —— Qt工程基本文件详解

目录1. pro文件内容解释2. main文件内容解释3. widget.cpp/widget.h文件内容解释4. ui_widget.h文件内容解释5. widget.ui文件内容解释以Widget窗口部件项目为例&#xff0c;新建的工程目录有如下几个文件&#xff1a; QtCreator软件将他们做了如下分组&#xff0c;包含三个文件…

idea快捷搜索键

目录 1、shift shift 双击 2、Ctrl F在当前类中&#xff0c;页中进行查找相关方法等 3、CtrlShiftN按【文件名】搜索文件 4、CtrlH 查看类的继承关系 5、Alt F7 查看类在哪儿被使用 idea全局搜索的快捷键 1、shift shift 双击 可以搜索任何东西。类、资源、配置项…

运行写在字符串中的Python代码 exec(‘‘‘print(1)‘‘‘)

【小白从小学Python、C、Java】 【Python-计算机等级考试二级】 【Python-数据分析】 运行写在字符串中的Python代码 exec(print(1)) [太阳]选择题 请问对以下Python代码说法错误的是&#xff1f; print("【执行】exec(print(1))") exec(print(1)) myFuncsumab prin…

CTF秀web2

CTF秀web21.分析题目2.解题2.1信息收集3.2获取数据库3.3获取数据库表3.3获取表信息3.uinon注入语句3.1 判断注入3.2 信息收集3.3注入语句1.分析题目 如上图所示&#xff0c;可以看到是sql注入的题目&#xff0c;进入题目看看&#xff0c;题目页面如下&#xff1a; 如上图所示&a…

fastjson反序列化漏洞

1.fastjson反序列化漏洞原理 我们知道fastjson在进⾏反序列化时会调⽤⽬标对象的构造&#xff0c;setter&#xff0c;getter等⽅法&#xff0c;如果这些⽅法内部 进⾏了⼀些危险的操作时&#xff0c;那么fastjson在进⾏反序列化时就有可能会触发漏洞。 我们通过⼀个简单的案例…

kubernetes 资源管理

kubernetes 资源管理 资源管理介绍 在kubernetes中&#xff0c;所有的内容都抽象为资源&#xff0c;用户需要通过操作资源来管理kubernetes。 kubernetes的本质上就是一个集群系统&#xff0c;用户可以在集群中部署各种服务&#xff0c;所谓的部署服务&#xff0c;其实就是在…

纳睿雷达冲刺上市:产能利用率不足仍要扩产,毛利率持续下滑

上海证券交易所信息显示&#xff0c;广东纳睿雷达科技股份有限公司&#xff08;下称“纳睿雷达”&#xff09;的IPO进程已有8个月未有变化&#xff0c;上一次更新信息还是2022年3月10日。而证监会网站则显示&#xff0c;已向纳睿雷达发出了注册阶段三次问询问题&#xff0c;最新…

创建线程的几种方式

创建线程的几种方式 文章目录创建线程的几种方式一、继承 Thread 类二、实现 Runnable 接口三、实现 Callable 接口&#xff0c;并结合 Future 实现四、通过线程池创建线程五、前三种创建线程方式对比继承Thread实现Runnable接口实现Callable接口参考链接一、继承 Thread 类 通…

11.20二叉树基础题型

一.二叉树的存储 1.存储结构 存储结构:顺序存储或者是类似于链表的链式存储 二叉树的链式存储是通过一个一个的节点引用起来的&#xff0c;常见的表示方式有二叉和三叉表示方式 // 孩子表示法 class Node {int val; // 数据域Node left; // 左孩子的引用&#xff0c;常常代…

【SpringBoot项目】一文掌握文件上传和下载【业务开发day04】

文章目录前言文件上传下载文件上传介绍文件下载介绍文件上传代码实现文件下载代码实现新增菜品需求分析数据模型代码开发功能测试&#x1f315;博客x主页&#xff1a;己不由心王道长&#x1f315;! &#x1f30e;文章说明&#xff1a;SpringBoot项目-瑞吉外卖【day04】业务开发…