• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

31.nacos集成Feign和Gatewayspringcloud

武飞扬头像
做一道光
帮助1

一、项目nacos-client-a

学新通

1.pom.xml文件

新增了springcloud的依赖

学新通

新增了springcloud的依赖管理

学新通

 新增了feign依赖

学新通

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
    <groupId>com.it</groupId>
  6.  
    <artifactId>nacos-client-a</artifactId>
  7.  
    <version>0.0.1-SNAPSHOT</version>
  8.  
    <name>nacos-client-a</name>
  9.  
    <description>Demo project for Spring Boot</description>
  10.  
     
  11.  
    <properties>
  12.  
    <java.version>1.8</java.version>
  13.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15.  
    <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
  16.  
    <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
  17.  
    <spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.version>
  18.  
    </properties>
  19.  
     
  20.  
    <dependencies>
  21.  
    <dependency>
  22.  
    <groupId>org.springframework.boot</groupId>
  23.  
    <artifactId>spring-boot-starter-web</artifactId>
  24.  
    </dependency>
  25.  
    <dependency>
  26.  
    <groupId>com.alibaba.cloud</groupId>
  27.  
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  28.  
    </dependency>
  29.  
     
  30.  
    <dependency>
  31.  
    <groupId>org.springframework.cloud</groupId>
  32.  
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  33.  
    </dependency>
  34.  
     
  35.  
    <dependency>
  36.  
    <groupId>org.springframework.boot</groupId>
  37.  
    <artifactId>spring-boot-starter-test</artifactId>
  38.  
    <scope>test</scope>
  39.  
    <exclusions>
  40.  
    <exclusion>
  41.  
    <groupId>org.junit.vintage</groupId>
  42.  
    <artifactId>junit-vintage-engine</artifactId>
  43.  
    </exclusion>
  44.  
    </exclusions>
  45.  
    </dependency>
  46.  
    </dependencies>
  47.  
     
  48.  
    <dependencyManagement>
  49.  
    <dependencies>
  50.  
    <dependency>
  51.  
    <groupId>org.springframework.boot</groupId>
  52.  
    <artifactId>spring-boot-dependencies</artifactId>
  53.  
    <version>${spring-boot.version}</version>
  54.  
    <type>pom</type>
  55.  
    <scope>import</scope>
  56.  
    </dependency>
  57.  
    <dependency>
  58.  
    <groupId>com.alibaba.cloud</groupId>
  59.  
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  60.  
    <version>${spring-cloud-alibaba.version}</version>
  61.  
    <type>pom</type>
  62.  
    <scope>import</scope>
  63.  
    </dependency>
  64.  
    <dependency>
  65.  
    <groupId>org.springframework.cloud</groupId>
  66.  
    <artifactId>spring-cloud-dependencies</artifactId>
  67.  
    <version>${spring-cloud.version}</version>
  68.  
    <type>pom</type>
  69.  
    <scope>import</scope>
  70.  
    </dependency>
  71.  
    </dependencies>
  72.  
    </dependencyManagement>
  73.  
     
  74.  
    <build>
  75.  
    <plugins>
  76.  
    <plugin>
  77.  
    <groupId>org.apache.maven.plugins</groupId>
  78.  
    <artifactId>maven-compiler-plugin</artifactId>
  79.  
    <version>3.8.1</version>
  80.  
    <configuration>
  81.  
    <source>1.8</source>
  82.  
    <target>1.8</target>
  83.  
    <encoding>UTF-8</encoding>
  84.  
    </configuration>
  85.  
    </plugin>
  86.  
    <plugin>
  87.  
    <groupId>org.springframework.boot</groupId>
  88.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  89.  
    <version>2.3.7.RELEASE</version>
  90.  
    <configuration>
  91.  
    <mainClass>com.it.NacosClientAApplication</mainClass>
  92.  
    </configuration>
  93.  
    <executions>
  94.  
    <execution>
  95.  
    <id>repackage</id>
  96.  
    <goals>
  97.  
    <goal>repackage</goal>
  98.  
    </goals>
  99.  
    </execution>
  100.  
    </executions>
  101.  
    </plugin>
  102.  
    </plugins>
  103.  
    </build>
  104.  
     
  105.  
    </project>
学新通

2.application.yml

学新通

3. feign目录下的TestFeign接口

学新通

 4.controller目录下的TestController

  1.  
    package com.it.controller;
  2.  
     
  3.  
    import com.it.feigin.TestFeign;
  4.  
    import org.springframework.beans.factory.annotation.Autowired;
  5.  
    import org.springframework.cloud.client.ServiceInstance;
  6.  
    import org.springframework.cloud.client.discovery.DiscoveryClient;
  7.  
    import org.springframework.web.bind.annotation.GetMapping;
  8.  
    import org.springframework.web.bind.annotation.RestController;
  9.  
     
  10.  
    import java.util.List;
  11.  
    @RestController
  12.  
    public class TestController {
  13.  
     
  14.  
    @Autowired
  15.  
    public DiscoveryClient discoveryClient;
  16.  
     
  17.  
    @Autowired
  18.  
    public TestFeign testFeign;
  19.  
     
  20.  
     
  21.  
     
  22.  
    @GetMapping("test")
  23.  
    public String test(){
  24.  
    List<ServiceInstance> instances = discoveryClient.getInstances("nacos-client-b");
  25.  
    System.out.println(instances);
  26.  
    return testFeign.info();
  27.  
    }
  28.  
     
  29.  
    }
学新通

5.主函数启动类

学新通

二、项目nacos-client-b

学新通

1.pom.xml文件

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

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
    <groupId>com.it</groupId>
  6.  
    <artifactId>nacos-client-b</artifactId>
  7.  
    <version>0.0.1-SNAPSHOT</version>
  8.  
    <name>nacos-client-b</name>
  9.  
    <description>Demo project for Spring Boot</description>
  10.  
     
  11.  
    <properties>
  12.  
    <java.version>1.8</java.version>
  13.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15.  
    <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
  16.  
    <spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.version>
  17.  
    </properties>
  18.  
     
  19.  
    <dependencies>
  20.  
    <dependency>
  21.  
    <groupId>org.springframework.boot</groupId>
  22.  
    <artifactId>spring-boot-starter-web</artifactId>
  23.  
    </dependency>
  24.  
    <dependency>
  25.  
    <groupId>com.alibaba.cloud</groupId>
  26.  
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  27.  
    </dependency>
  28.  
     
  29.  
    <dependency>
  30.  
    <groupId>org.springframework.boot</groupId>
  31.  
    <artifactId>spring-boot-starter-test</artifactId>
  32.  
    <scope>test</scope>
  33.  
    <exclusions>
  34.  
    <exclusion>
  35.  
    <groupId>org.junit.vintage</groupId>
  36.  
    <artifactId>junit-vintage-engine</artifactId>
  37.  
    </exclusion>
  38.  
    </exclusions>
  39.  
    </dependency>
  40.  
    </dependencies>
  41.  
     
  42.  
    <dependencyManagement>
  43.  
    <dependencies>
  44.  
    <dependency>
  45.  
    <groupId>org.springframework.boot</groupId>
  46.  
    <artifactId>spring-boot-dependencies</artifactId>
  47.  
    <version>${spring-boot.version}</version>
  48.  
    <type>pom</type>
  49.  
    <scope>import</scope>
  50.  
    </dependency>
  51.  
    <dependency>
  52.  
    <groupId>com.alibaba.cloud</groupId>
  53.  
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  54.  
    <version>${spring-cloud-alibaba.version}</version>
  55.  
    <type>pom</type>
  56.  
    <scope>import</scope>
  57.  
    </dependency>
  58.  
    </dependencies>
  59.  
    </dependencyManagement>
  60.  
     
  61.  
    <build>
  62.  
    <plugins>
  63.  
    <plugin>
  64.  
    <groupId>org.apache.maven.plugins</groupId>
  65.  
    <artifactId>maven-compiler-plugin</artifactId>
  66.  
    <version>3.8.1</version>
  67.  
    <configuration>
  68.  
    <source>1.8</source>
  69.  
    <target>1.8</target>
  70.  
    <encoding>UTF-8</encoding>
  71.  
    </configuration>
  72.  
    </plugin>
  73.  
    <plugin>
  74.  
    <groupId>org.springframework.boot</groupId>
  75.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  76.  
    <version>2.3.7.RELEASE</version>
  77.  
    <configuration>
  78.  
    <mainClass>com.it.NacosClientBApplication</mainClass>
  79.  
    </configuration>
  80.  
    <executions>
  81.  
    <execution>
  82.  
    <id>repackage</id>
  83.  
    <goals>
  84.  
    <goal>repackage</goal>
  85.  
    </goals>
  86.  
    </execution>
  87.  
    </executions>
  88.  
    </plugin>
  89.  
    </plugins>
  90.  
    </build>
  91.  
     
  92.  
    </project>
学新通

2.application.yml

学新通

3.BController

学新通

 4.主函数启动类

学新通

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

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

学新通

学新通

三、新建gateway模块

学新通

学新通

 1.pom.xml文件

新增springcloud的依赖

学新通

2.application.yml

学新通

3.主函数启动类

学新通

同时运行三个项目

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

学新通

 同时启动三个项目

学新通

再次访问该路径

学新通

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfikiif
系列文章
更多 icon
同类精品
更多 icon
继续加载