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

server demo 演示

创建server项目

image

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);
    }
}

验证

image

openfeign client demo

创建client项目

image

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版本的一个对照 参考

https://blog.csdn.net/u014636209/article/details/125863935?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125863935%22%2C%22source%22%3A%22u014636209%22%7D&ctrtid=dXJ80

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));
    }
}

客户端调用验证

image

源码

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
NTRIP(Networked Transport of RTCM via Internet Protocol)是一种用于实时差分GPS数据传输的协议。Spring Boot是一个开源的Java web框架,提供了一种快速构建应用程序的方式。 接下来,我将提供一个简单的示例,演示如何使用Spring Boot构建NTRIP服务器,并编写一个客户端调用该服务器的demo。 1. 首先,我们需要添加Spring Boot和NTRIP依赖项。可以在Maven中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.javagently</groupId> <artifactId>ntrip</artifactId> <version>1.3</version> </dependency> ``` 2. 接下来,我们需要编写一个NTRIP服务器。可以使用Spring Boot的@RestController注释来创建一个RESTful API,并使用NTRIP的NtripServer类来处理NTRIP连接。 ```java @RestController public class NtripServerController { @PostMapping("/ntrip") public void ntrip(HttpServletRequest request, HttpServletResponse response) throws IOException { NtripServer ntripServer = new NtripServer(new NtripServerHandler() { @Override public void handleNtripRequest(NtripRequest request) { // 处理NTRIP请求 } }); ntripServer.start(request.getInputStream(), response.getOutputStream()); } } ``` 3. 最后,我们需要编写一个NTRIP客户端调用服务器。可以使用NTRIP的NtripClient类来处理连接。以下是一个简单的示例: ```java public class NtripClientDemo { public static void main(String[] args) throws IOException { NtripClient ntripClient = new NtripClient("localhost", 8080, "username", "password", "mountpoint"); NtripResponse ntripResponse = ntripClient.sendRequest(); if (ntripResponse.getStatusCode() == 200) { InputStream inputStream = ntripResponse.getInputStream(); // 处理NTRIP数据流 } else { // 处理错误 } } } ``` 这就是一个使用Spring Boot构建NTRIP服务器,并编写一个客户端调用该服务器的简单示例。需要注意的是,在实际应用中,需要根据具体的需求来编写更加完善的服务器和客户端代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山富哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值