31.nacos集成Feign和Gatewayspringcloud
一、项目nacos-client-a
1.pom.xml文件
新增了springcloud的依赖
新增了springcloud的依赖管理
新增了feign依赖
-
-
<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;
-
-
public class TestController {
-
-
-
public DiscoveryClient discoveryClient;
-
-
-
public TestFeign testFeign;
-
-
-
-
-
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即可
-
-
<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 做注册中心,服务发现,以及远程调用都完成了
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfikiif
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13