Spring Boot TCP 客户端 [英] Spring Boot TCP Client

查看:305
本文介绍了Spring Boot TCP 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个通过 sing boot 连接 TCP 的示例,无需 xml(spring-integration).

I'm looking for an example to connect TCP through sping boot without xml(spring-integration).

我从 如何在spring boot中创建一个Tcp Connection来接受连接? URL.

在这个例子中,只需要 main 方法就足以连接 tcp.为什么这里声明了其他 bean 和转换器?

in this example, just main method alone enough to connect tcp. why other beans and the transformer are declared here?

有错吗?我想与 Spring 集成,而不是使用简单的 Java 套接字客户端来接受响应.但是没有使用 Java DSL 的合适示例.

Is it wrong? Instead of using simple Java socket client to accept the response, I would like to integrate with Spring. But no suitable examples available using Java DSL.

你能帮忙吗?

package com.example;

import java.net.Socket;

import javax.net.SocketFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.transformer.ObjectToStringTransformer;
import org.springframework.messaging.MessageChannel;

@SpringBootApplication
public class So39290834Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So39290834Application.class, args);
        Socket socket = SocketFactory.getDefault().createSocket("localhost", 9999);
        socket.getOutputStream().write("foo
".getBytes());
        socket.close();
        Thread.sleep(1000);
        context.close();
    }

    @Bean
    public TcpNetServerConnectionFactory cf() {
        return new TcpNetServerConnectionFactory(9999);
    }

    @Bean
    public TcpReceivingChannelAdapter inbound(AbstractServerConnectionFactory cf) {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(cf);
        adapter.setOutputChannel(tcpIn());
        return adapter;
    }

    @Bean
    public MessageChannel tcpIn() {
        return new DirectChannel();
    }

    @Transformer(inputChannel = "tcpIn", outputChannel = "serviceChannel")
    @Bean
    public ObjectToStringTransformer transformer() {
        return new ObjectToStringTransformer();
    }

    @ServiceActivator(inputChannel = "serviceChannel")
    public void service(String in) {
        System.out.println(in);
    }

}

推荐答案

这个应用程序既是客户端又是服务器.

This application is both the client and the server.

那个问题具体是关于如何使用 Spring Integration 编写服务器端(接受连接).

That question was specifically about how to write the server side (accept the connection), using Spring Integration.

main() 方法只是一个连接到服务器端的测试.它使用标准的 Java 套接字 API;它也可以编写为在客户端使用 Spring Integration 组件.

The main() method is simply a test that connects to the server side. It uses standard Java sockets APIs; it could also have been written to use Spring Integration components on the client side.

顺便说一句,您不必使用 XML 来编写 Spring Integration 应用程序,您可以使用注解对其进行配置,或者使用 Java DSL.阅读文档.

BTW, you don't have to use XML to write a Spring Integration application, you can configure it with annotations, or use the Java DSL. Read the documentation.

编辑

使用 Java DSL 的客户端/服务器示例

@SpringBootApplication
public class So54057281Application {

    public static void main(String[] args) {
        SpringApplication.run(So54057281Application.class, args);
    }

    @Bean
    public IntegrationFlow server() {
        return IntegrationFlows.from(Tcp.inboundGateway(
                    Tcp.netServer(1234)
                        .serializer(codec()) // default is CRLF
                        .deserializer(codec()))) // default is CRLF
                .transform(Transformers.objectToString()) // byte[] -> String
                .<String, String>transform(p -> p.toUpperCase())
                .get();
    }

    @Bean
    public IntegrationFlow client() {
        return IntegrationFlows.from(MyGateway.class)
                .handle(Tcp.outboundGateway(
                    Tcp.netClient("localhost", 1234)
                        .serializer(codec()) // default is CRLF
                        .deserializer(codec()))) // default is CRLF
                .transform(Transformers.objectToString()) // byte[] -> String
                .get();
    }

    @Bean
    public AbstractByteArraySerializer codec() {
        return TcpCodecs.lf();
    }

    @Bean
    @DependsOn("client")
    ApplicationRunner runner(MyGateway gateway) {
        return args -> {
            System.out.println(gateway.exchange("foo"));
            System.out.println(gateway.exchange("bar"));
        };
    }

    public interface MyGateway {

        String exchange(String out);

    }

}

结果

FOO
BAR

这篇关于Spring Boot TCP 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆