apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.4k stars 26.41k forks source link

Context loss when async invoke #13666

Open bringwu opened 8 months ago

bringwu commented 8 months ago

provider service method:

    public CompletableFuture<Integer> newToken(NewTokenInfo tReq) {
        TokenInfo tokenInfo = new TokenInfo(JsonUtils.toJson(tReq), 60);    
        // 可以设置回应附加信息
//      RpcContext.getServerResponseContext().setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
        // 可以设置回应附加信息
//      RpcContext.getServerContext().setAttachment("testInfo", JsonUtils.toJson(tokenInfo));

        CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
        completableFuture = completableFuture.completeAsync(()->{
            logger.debug("tokenInfo:" + JsonUtils.toJson(tokenInfo));
            return 1;
        }, Executors.newFixedThreadPool(1));

        // 不可以设置回应附加信息
        RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
//      RpcContextAttachment serverResponseContext = RpcContext.getClientAttachment();
//      RpcContextAttachment serverResponseContext = RpcContext.getServerContext();
//      RpcServiceContext serverResponseContext = RpcContext.getServiceContext();
//      RpcServiceContext serverResponseContext = RpcContext.getCurrentServiceContext();
//      RpcContextAttachment serverResponseContext = RpcContext.getClientResponseContext();
//      RpcContextAttachment serverResponseContext = RpcContext.getServerAttachment();
        completableFuture = completableFuture.thenApply(ret->{
            // 不可以设置回应附加信息
//          RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
//          RpcContextAttachment serverResponseContext = RpcContext.getClientAttachment();
//          RpcContextAttachment serverResponseContext = RpcContext.getServerContext();
//          RpcServiceContext serverResponseContext = RpcContext.getServiceContext();
//          RpcServiceContext serverResponseContext = RpcContext.getCurrentServiceContext();
//          RpcContextAttachment serverResponseContext = RpcContext.getClientResponseContext();
//          RpcContextAttachment serverResponseContext = RpcContext.getServerAttachment();

            serverResponseContext.setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
            return ret;
        });
        return completableFuture;
    }

consumer :

        NewTokenInfo tReq = new NewTokenInfo("testType", "testToken");
        CompletableFuture<Integer> future = tokenRpcProxy.newToken(tReq);
        CompletableFuture<Void> completableFuture = future.thenAccept(ret->{
            logger.debug("newToken: " + ret + ", testInfo: " + RpcContext.getClientResponseContext().getAttachment("testInfo"));
            logger.debug("newToken: " + ret + ", testInfo: " + RpcContext.getServerContext().getAttachment("testInfo"));
        });
        try {
            completableFuture.get();
        } catch (Exception e) {
            throw new RuntimeException("未知异常", e);
        }

provider的方法是异步执行,异步执行完后才能往RpcContext设置回应附加信息,这种情况回应附加信息没法传递到consumer这边

AlbumenJ commented 8 months ago

image image image It works on 3.2.10.

bringwu commented 8 months ago

@AlbumenJ my dubbo version is 3.2.10 too. there is my new test code:

api

public interface AsyncContext {

    public CompletableFuture<Integer> newToken(String token);
    CompletableFuture<Integer> sayHi(String name);

}

provider:

 @DubboService
public class AsyncContextImpl implements AsyncContext {
    private static final Logger logger = LoggerFactory.getLogger(AsyncContextImpl.class);

    public CompletableFuture<Integer> newToken(String token) {
        CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
        completableFuture = completableFuture.completeAsync(() -> {
            logger.debug("tokenInfo:" + token);
            return 1;
        }, Executors.newFixedThreadPool(1));

        RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
        completableFuture = completableFuture.thenApply(ret -> {
            serverResponseContext.setAttachment("tokenInfo", token);
            return ret;
        });
        return completableFuture;
    }

    @Override
    public CompletableFuture<Integer> sayHi(String name) {
        String text = "hi, " + name;
        CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
        completableFuture = completableFuture.completeAsync(() -> {
            logger.debug(text);
            return 1;
        }, Executors.newFixedThreadPool(1));

        RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
        completableFuture = completableFuture.thenApply(ret -> {
            serverResponseContext.setAttachment("testInfo", text);
            return ret;
        });
        return completableFuture;
    }

}

consumer:

@Component
public class AsyncContextTest {
    private static final Logger logger = LoggerFactory.getLogger(AsyncContextTest.class);

    @DubboReference
    private AsyncContext asyncContext;

    public void sayHi() {
        CompletableFuture<Integer> future = asyncContext.sayHi("dubbo");
        CompletableFuture<Void> completableFuture = future.thenAccept(ret->{
            logger.debug("ret: " + ret + ", testInfo: " + RpcContext.getClientResponseContext().getAttachment("testInfo"));
        });
        try {
            completableFuture.get();
        } catch (Exception e) {
            throw new RuntimeException("error", e);
        }
    }

    public void newToken() {
        String token = "dubboToken";
        CompletableFuture<Integer> future = asyncContext.newToken(token);
        CompletableFuture<Void> completableFuture = future.thenAccept(ret->{
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getClientResponseContext().getAttachment("tokenInfo"));
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getServerAttachment().getAttachment("tokenInfo"));
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getServiceContext().getAttachment("tokenInfo"));
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getServerContext().getAttachment("tokenInfo"));
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getCurrentServiceContext().getAttachment("tokenInfo"));
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getServerResponseContext().getAttachment("tokenInfo"));
            logger.debug("newToken: " + ret + ", rsp: " + RpcContext.getClientAttachment().getAttachment("tokenInfo"));
        });
        try {
            completableFuture.get();
        } catch (Exception e) {
            throw new RuntimeException("error", e);
        }
    }
}

when Bootstrap class like that, it prints:

@SpringBootApplication
@EnableDubbo
public class Bootstrap {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Bootstrap.class, args);

        applicationContext.getBean(AsyncContextTest.class).newToken();
        applicationContext.getBean(AsyncContextTest.class).sayHi();
        applicationContext.getBean(AsyncContextTest.class).sayHi();

        applicationContext.stop();
    }
}

2024-01-17 17:39:25.121 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: dubboToken
2024-01-17 17:39:25.121 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: dubboToken
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.122 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.123 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:39:25.133 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:39:25.146 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo

when Bootstrap class like that, it prints error:

@SpringBootApplication
@EnableDubbo
public class Bootstrap {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Bootstrap.class, args);

        applicationContext.getBean(AsyncContextTest.class).sayHi();
        applicationContext.getBean(AsyncContextTest.class).sayHi();
        applicationContext.getBean(AsyncContextTest.class).newToken(); // see this

        applicationContext.stop();
    }

}

2024-01-17 17:45:11.242 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:45:11.249 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:45:11.258 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.258 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.259 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:45:11.260 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null

it there any error in my code?

bringwu commented 8 months ago

when I test multi times,it also prints

2024-01-17 17:57:05.029 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:57:05.038 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: null
2024-01-17 17:57:05.050 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.050 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.050 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null

and

2024-01-17 17:58:42.136 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: null
2024-01-17 17:58:42.144 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: null
2024-01-17 17:58:42.156 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.156 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null

it seens that there is some bugs in RpcContext

bobbyz007 commented 8 months ago

3.3.0 beta-1 版本也不行 server:

AsyncContext asyncContext = RpcContext.startAsync();
return CompletableFuture.supplyAsync(() -> {
            asyncContext.signalContextSwitch();

            RpcContextAttachment serverContext = RpcContext.getServerContext();
            // 往调用端传递参数
            serverContext.setAttachment("server-key1", "server-");

client:

CompletableFuture<String> f = greetingService.helloFuture();
f.whenComplete((v, t) -> {
            logger.info(RpcContext.getServerContext().getAttachment("server-key1"));
AlbumenJ commented 8 months ago

when I test multi times,it also prints

2024-01-17 17:57:05.029 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: hi, dubbo
2024-01-17 17:57:05.038 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: null
2024-01-17 17:57:05.050 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.050 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.050 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:57:05.051 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null

and

2024-01-17 17:58:42.136 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: null
2024-01-17 17:58:42.144 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - ret: 1, testInfo: null
2024-01-17 17:58:42.156 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.156 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null
2024-01-17 17:58:42.157 [DubboClientHandler-thread-1] DEBUG c.b.d.s.c.service.AsyncContextTest - newToken: 1, rsp: null

it seens that there is some bugs in RpcContext

Can you please upgrade the demo to Github as a repo?

bringwu commented 8 months ago

@AlbumenJ sorry, I have trouble pushing my repository to GitHub. i had tried many times, but still fail. you can create a maven project to test it.

there is the project maven config :

<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>3.1.5</version>
        <relativePath />
    </parent>

    <groupId>com.bringwu.dubbo</groupId>
    <artifactId>DubboTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>ProviderTest</module>
        <module>ConsumerTest</module>
        <module>ServiceApi</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>17</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

        <dubbo.version>3.2.10</dubbo.version>
    </properties>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

image

AlbumenJ commented 7 months ago

@AlbumenJ sorry, I have trouble pushing my repository to GitHub. i had tried many times, but still fail. you can create a maven project to test it.

there is the project maven config :

<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>3.1.5</version>
      <relativePath />
  </parent>

  <groupId>com.bringwu.dubbo</groupId>
  <artifactId>DubboTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
      <module>ProviderTest</module>
      <module>ConsumerTest</module>
      <module>ServiceApi</module>
  </modules>
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>17</java.version>
      <maven.compiler.source>${java.version}</maven.compiler.source>
      <maven.compiler.target>${java.version}</maven.compiler.target>

      <dubbo.version>3.2.10</dubbo.version>
  </properties>
  <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                  <source>${java.version}</source>
                  <target>${java.version}</target>
              </configuration>
          </plugin>
      </plugins>
  </build>

  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>*</groupId>
                  <artifactId>*</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <scope>provided</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
      </dependency>
      <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
      <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
      </dependency>

      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <scope>test</scope>
      </dependency>

      <dependency>
          <groupId>org.apache.dubbo</groupId>
          <artifactId>dubbo-spring-boot-starter</artifactId>
      </dependency>
  </dependencies>

  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo-bom</artifactId>
              <version>${dubbo.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
</project>

image

You can send the demo to my mail. (albumenj@apache.org)

bringwu commented 7 months ago

@AlbumenJ sorry, I have trouble pushing my repository to GitHub. i had tried many times, but still fail. you can create a maven project to test it. there is the project maven config :

<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>3.1.5</version>
        <relativePath />
    </parent>

    <groupId>com.bringwu.dubbo</groupId>
    <artifactId>DubboTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>ProviderTest</module>
        <module>ConsumerTest</module>
        <module>ServiceApi</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>17</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

        <dubbo.version>3.2.10</dubbo.version>
    </properties>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

image

You can send the demo to my mail. (albumenj@apache.org)

the email has been sent to your email address

AlbumenJ commented 7 months ago

@AlbumenJ sorry, I have trouble pushing my repository to GitHub. i had tried many times, but still fail. you can create a maven project to test it. there is the project maven config :

<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>3.1.5</version>
      <relativePath />
  </parent>

  <groupId>com.bringwu.dubbo</groupId>
  <artifactId>DubboTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
      <module>ProviderTest</module>
      <module>ConsumerTest</module>
      <module>ServiceApi</module>
  </modules>
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>17</java.version>
      <maven.compiler.source>${java.version}</maven.compiler.source>
      <maven.compiler.target>${java.version}</maven.compiler.target>

      <dubbo.version>3.2.10</dubbo.version>
  </properties>
  <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                  <source>${java.version}</source>
                  <target>${java.version}</target>
              </configuration>
          </plugin>
      </plugins>
  </build>

  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>*</groupId>
                  <artifactId>*</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <scope>provided</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
      </dependency>
      <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
      <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
      </dependency>

      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <scope>test</scope>
      </dependency>

      <dependency>
          <groupId>org.apache.dubbo</groupId>
          <artifactId>dubbo-spring-boot-starter</artifactId>
      </dependency>
  </dependencies>

  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo-bom</artifactId>
              <version>${dubbo.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
</project>

image

You can send the demo to my mail. (albumenj@apache.org)

the email has been sent to your email address

Get it. I will test it later.

AlbumenJ commented 7 months ago

@bringwu In your demo, Dubbo process the result before set attachment.

image Add sleep before return to make sure that serverResponseContext.setAttachment("tokenInfo", token) has been processed.

image Then it work.

bringwu commented 7 months ago

@bringwu In your demo, Dubbo process the result before set attachment.

image Add sleep before return to make sure that serverResponseContext.setAttachment("tokenInfo", token) has been processed.

image Then it work.

Adding sleep can make an asynchronous program behave synchronously, but it can lead to performance issues. I think Dubbo should optimize this problem to allow programs to execute purely asynchronously.

AlbumenJ commented 7 months ago

@bringwu In your demo, Dubbo process the result before set attachment. image Add sleep before return to make sure that serverResponseContext.setAttachment("tokenInfo", token) has been processed. image Then it work.

Adding sleep can make an asynchronous program behave synchronously, but it can lead to performance issues. I think Dubbo should optimize this problem to allow programs to execute purely asynchronously.

If you want to use local invoke by default, it would be better to use spring bean directly instead of using Dubbo bean. That performance would be better.

bringwu commented 7 months ago

This is a terrible solution. I can't believe it was proposed by official personnel. I'll figure out a solution on my own.

ling0900 commented 4 months ago

to mark for me