apache / shenyu

Apache ShenYu is a Java native API Gateway for service proxy, protocol conversion and API governance.
https://shenyu.apache.org/
Apache License 2.0
8.42k stars 2.92k forks source link

AlibabaDubboServiceBeanListener #1439

Closed JemyXiao closed 3 years ago

JemyXiao commented 3 years ago

hi, branch: master class: AlibabaDubboServiceBeanListener method: onApplicationEvent env: springboot detail: init AlibabaDubboServiceBeanListener invoke onApplicationEvent Map<String, ServiceBean> serviceBean = contextRefreshedEvent.getApplicationContext().getBeansOfType(ServiceBean.class); serviceBean.size() = 0

yu199195 commented 3 years ago

whats the dubbo version? you use XML config or spring boot starter?

JemyXiao commented 3 years ago

whats the dubbo version? you use XML config or spring boot starter?

dubbo version is 2.6.0 i use springboot starter ver=2.0.0

KevinClair commented 3 years ago

Please show us your dubbo service interface

JemyXiao commented 3 years ago

Please show us your dubbo service interface image

JemyXiao commented 3 years ago

i debug application start process, i find registerServiceBean not in,i am not sure because of that image

KevinClair commented 3 years ago

May be you have to make sure that your dubbo service has been registed successfully.

yu199195 commented 3 years ago

@KevinClair i think it the dubbo-spring-boot-starter , not register spring bean, can you fix it?

JemyXiao commented 3 years ago

May be you have to make sure that your dubbo service has been registed successfully.

i am sure dubbo service registed successful already.

KevinClair commented 3 years ago

@KevinClair i think it the dubbo-spring-boot-starter , not register spring bean, can you fix it?

ok. i will fix it.

JemyXiao commented 3 years ago

@KevinClair i find another problem, i try registry ServiceBean successful,but when invoke AlibabaDubboServiceBeanListener handler method,line 101 method.getAnnotation(ShenyuDubboCilent.Class) is null. please look it together,thanks. image

KevinClair commented 3 years ago

@KevinClair i find another problem, i try registry ServiceBean successful,but when invoke AlibabaDubboServiceBeanListener handler method,line 101 method.getAnnotation(ShenyuDubboCilent.Class) is null. please look it together,thanks. image

Got it.

yu199195 commented 3 years ago

I think dubbo proxy bean maybe miss the annotation, if use dubbo annotation type..

JemyXiao commented 3 years ago

I think dubbo proxy bean maybe miss the annotation, if use dubbo annotation type..

i agree with you, i hope it can be fixed together?

KevinClair commented 3 years ago

Hello @JemyXiao ,I've tried many times and it's all successful.Here are some of my configuration.

com.101tec zkclient 0.10 org.apache.shenyu shenyu-spring-boot-starter-client-alibaba-dubbo 2.3.1-SNAPSHOT
* spring-dubbo.xml file
```java
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-service"/>
    <!-- 注册中心的ip地址 -->
    <dubbo:registry protocol="zookeeper" address="localhost:2181"/>
    <!-- 协议/端口-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 暴露的接口 -->
    <dubbo:service ref="dubboService" interface="com.example.demo.dubbo.TestService"
                   timeout="3000" />
</beans>

And when I run my application. 微信截图_20210512172943 微信截图_20210512173016

yu199195 commented 3 years ago

@KevinClair He not use XML, used Annotation.....

KevinClair commented 3 years ago

@KevinClair He not use XML, used Annotation.....

Sorry, I run application with Annotation, It works as well.

JemyXiao commented 3 years ago

@KevinClair He not use XML, used Annotation.....

Sorry, I run application with Annotation, It works as well.

please show detail your dubbo service impl

JemyXiao commented 3 years ago

@KevinClair please remove dubbo config xml,retry add @Service dubbo Annotation

JemyXiao commented 3 years ago

imageimage

KevinClair commented 3 years ago

@KevinClair please remove dubbo config xml,retry add @service dubbo Annotation

I try it and works as well.

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.dubbo.TestService;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.stereotype.Component;

/**
 * Des
 *
 * @author kevin
 * @date 2021/5/12 16:57
 **/
@Component
@Service
public class TestServiceImpl implements TestService {

    @Override
    @ShenyuDubboClient(path = "/test")
    public String test() {
        return "haha";
    }
}
yu199195 commented 3 years ago

@KevinClair Hi, @Services is Dubbo Annotaion, not spring

JemyXiao commented 3 years ago

@KevinClair please remove dubbo config xml,retry add @service dubbo Annotation

I try it and works as well.

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.dubbo.TestService;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.stereotype.Component;

/**
 * Des
 *
 * @author kevin
 * @date 2021/5/12 16:57
 **/
@Component
@Service
public class TestServiceImpl implements TestService {

    @Override
    @ShenyuDubboClient(path = "/test")
    public String test() {
        return "haha";
    }
}

in think your xml not remove

KevinClair commented 3 years ago

in think your xml not remove

I delete xml file.

KevinClair commented 3 years ago

Hello @JemyXiao , if you can, please send your simple example to my emil kevinclair@apache.org so that we can check the problem.

JemyXiao commented 3 years ago

Hello @JemyXiao , if you can, please send your simple example to my emil kevinclair@apache.org so that we can check the problem.

i use shenyu-examples-alibaba-dubbo-service module, i edit DubboTestServiceImpl use dubbo @Service Annotation, u can try it

JemyXiao commented 3 years ago

Hello @JemyXiao , if you can, please send your simple example to my emil kevinclair@apache.org so that we can check the problem.

i use shenyu-examples-alibaba-dubbo-service module, i edit DubboTestServiceImpl use dubbo @service Annotation, u can try it

image

KevinClair commented 3 years ago

Got it.

KevinClair commented 3 years ago

Hello @JemyXiao , I have found problem, you have to add @DubboComponentScan({"org.apache.shenyu.examples.alibaba.dubbo.service.impl"}) in the org.apache.shenyu.examples.alibaba.dubbo.service.TestAlibabaDubboApplication to make sure Dubbo scan your interface with @Service, just like this:

}

* DubboTestServiceImpl 
```java
package org.apache.shenyu.examples.alibaba.dubbo.service.impl;

import java.util.Arrays;
import java.util.Random;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
import org.apache.shenyu.examples.dubbo.api.service.DubboTestService;
import org.springframework.stereotype.Component;

/**
 * DubboTestServiceImpl.
 */
@Component("dubboTestService")
@Service
public class DubboTestServiceImpl implements DubboTestService {

@Override
    @ShenyuDubboClient(path = "/findById", desc = "Query by Id")
    public DubboTest findById(final String id) {
        DubboTest dubboTest = new DubboTest();
        dubboTest.setId(id);
        dubboTest.setName("hello world shenyu Alibaba Dubbo, findById");
        return dubboTest;
    }
}

// you can also use this.


* if you delete spring-dubbo.xml, you have to add configuration in the application.yml and add `@EnableDubbo` in `TestAlibabaDubboApplication`, for example:
```java
@SpringBootApplication
@EnableDubbo
public class TestAlibabaDubboApplication {

    /**
     * Main Entrance.
     *
     * @param args startup arguments
     */
    public static void main(final String[] args) {
        SpringApplication.run(TestAlibabaDubboApplication.class, args);
    }

}
dubbo:
  application:
    name: demo-service
  registry:
    protocol: zookeeper
    address: localhost:2181
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: org.apache.shenyu.examples.alibaba.dubbo.service.impl
JemyXiao commented 3 years ago

@KevinClair thanks for patiently answer, i try it successfull follow your way. about another problem not found @ShenyuXXXClient Annotation from impl class, i fix it and pull request also.

KevinClair commented 3 years ago

@KevinClair thanks for patiently answer, i try it successfull follow your way. about another problem not found @ShenyuXXXClient Annotation from impl class, i fix it and pull request also.

But if you follow my way, @ShenyuXXXClient would be found.

JemyXiao commented 3 years ago

@KevinClair thanks for patiently answer, i try it successfull follow your way. about another problem not found @ShenyuXXXClient Annotation from impl class, i fix it and pull request also.

But if you follow my way, @ShenyuXXXClient would be found.

The scene is like this,I run my project not shenyu-example,my impl class generate spring proxy, so when handler method AopUtils.isCglibProxy() param type has a problem, i had pr. please code review my pr code