首页 > 其他分享 >springboot openfeign服务端与客户端调用演示demo

springboot openfeign服务端与客户端调用演示demo

时间:2022-11-16 14:04:19浏览次数:81  
标签:maven springboot openfeign demo boot sample org


文章目录

  • ​​server demo 演示​​
  • ​​创建server项目​​
  • ​​application.properties配置​​
  • ​​import jar [pom.xml]​​
  • ​​创建服务端的restful controller​​
  • ​​验证​​
  • ​​openfeign client demo​​
  • ​​创建client项目​​
  • ​​import jar【pom.xml】​​
  • ​​注意 springboot与springcloud版本的一个对照 参考​​
  • ​​application.properties配置​​
  • ​​调用服务端的client接口编码​​
  • ​​客户端restful controller编码​​
  • ​​客户端调用验证​​
  • ​​源码​​
  • ​​写在最后​​

server demo 演示

创建server项目


springboot openfeign服务端与客户端调用演示demo_springcloud

application.properties配置

spring.application.name=springboot-sample-openfeign-server
server.port=8086

import jar [pom.xml]

<?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.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.gaoxinfu.springboot.sample.openfeign</groupId>
<artifactId>springboot-sample-openfeign-server</artifactId>
<version>1.0.0</version>
<name>springboot-sample-openfeign-server</name>
<description>springboot-sample-openfeign-server</description>
<packaging>pom</packaging>

<properties>
<java.version>1.8</java.version>
</properties>

<repositories>
<repository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>

<!-- 发布选项: id必须与setting.xml文件中server的id相同 -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/repository/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/repository/snapshots/</url>
</snapshotRepository>
</distributionManagement>

<dependencies>
<!-- This is a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
</project>

创建服务端的restful controller

/**
* @Description:[一句话描述该类的功能]
* @Author:gaoxinfu
* @Date:2022/7/6 21:47
* @Version 1.0.0
*/
@RestController
@Controller
public class IndexController {


@PostMapping("/index")
public IndexResDto index(@RequestBody IndexReqDto indexReqDto) {
System.out.println("indexReqDto = "+indexReqDto);
return new IndexResDto(1,"gaoxinfu","man",18);
}
}

验证


springboot openfeign服务端与客户端调用演示demo_xml_02

openfeign client demo

创建client项目


springboot openfeign服务端与客户端调用演示demo_spring_03

import jar【pom.xml】

<?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.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.gaoxinfu.springboot.sample.openfeign</groupId>
<artifactId>springboot-sample-openfeign-client</artifactId>
<version>1.0.0</version>
<name>springboot-sample-openfeign-client</name>
<description>springboot-sample-openfeign-client</description>
<packaging>pom</packaging>

<properties>
<java.version>1.8</java.version>
</properties>

<repositories>
<repository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>

<!-- 发布选项: id必须与setting.xml文件中server的id相同 -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/repository/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/repository/snapshots/</url>
</snapshotRepository>
</distributionManagement>

<dependencies>
<!-- This is a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--springboot版本与springcloud版本必须按照官方的要求一直-->
<version>Greenwich.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<resources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
</project>

注意 springboot与springcloud版本的一个对照 参考


application.properties配置

spring.application.name=springboot-sample-openfeign-client
server.port=8085

调用服务端的client接口编码

/**
* @Description:[一句话描述该类的功能]
* @Author:gaoxinfu
* @Date:2022/7/19 07:16
* @Version 1.0.0
*/
@FeignClient(name = "demo",url = "http://localhost:8086")
public interface ServerFeignClient {

@GetMapping("/index")
IndexResDto index(@RequestBody IndexReqDto indexReqDto);
}

客户端restful controller编码

/**
* @Description:[一句话描述该类的功能]
* @Author:gaoxinfu
* @Date:2022/7/6 21:47
* @Version 1.0.0
*/
@RestController
@Controller
public class DemoController {

@Resource
private ServerFeignClient serverFeignClient;

@GetMapping("/client/index")
public IndexResDto index(@RequestParam int id) {
return serverFeignClient.index(new IndexReqDto(id));
}
}

客户端调用验证


springboot openfeign服务端与客户端调用演示demo_xml_04

源码

​https://github.com/gaoxinfu/springboot-sample/tree/master/springboot-sample-openfeign​

写在最后


​​​ https://github.com/gaoxinfu/springboot-sample/tree/master/springboot-sample-openfeignd现在​

https://github.com/gaoxinfu/springboot-sample/tree/master/springboot-sample-openfeign


标签:maven,springboot,openfeign,demo,boot,sample,org
From: https://blog.51cto.com/u_11812624/5856098

相关文章