SpringBoot dubbo 的簡單使用註解版本

輕雲K發表於2020-10-17

SpringBoot dubbo 的簡單使用註解版本

關於工程的關係配置 可以看上一篇

關於dubbo 的引用是

 implementation  group: 'org.apache.dubbo', name: 'dubbo-spring-boot-starter', version: '2.7.6'
  1. alibb-dubbo-mineserviceapi 模組 ---- 介面模組
public interface AnnotationDemoService {
   String sayHello();
}

  1. alibb-sample-dubbo-provider ---- 服務提供者
import com.study.alibbdubbomineserviceapi.demo.AnnotationDemoService;
import org.apache.dubbo.config.annotation.Service;

@Service//服務實現 注意這個註解是Dubbo的
public class AnnotationDemoProvider implements AnnotationDemoService {
    @Override
    public String sayHello() {
        return "AnnotationDemo Hello";
    }
}

//啟動
@SpringBootApplication
@EnableDubbo//啟動dubbo服務
public class AlibbSampleDubboProviderApplication {

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

}

配置
application.properties

dubbo.application.name=sample-provider
# 應用所屬著
dubbo.application.owner=jmx
#應用所屬組織
dubbo.application.organization=study
#沒有註冊中心
dubbo.registry.address=N/A
dubbo.protocol.name=dubbo
dubbo.protocol.port=28810
dubbo.protocol.host=127.0.0.1

  1. alibb-sample-dubbo-consumer 服務消費者
@SpringBootApplication
@EnableDubbo
public class AlibbSampleDubboConsumerApplication {

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

}

import com.study.alibbdubbomineserviceapi.demo.AnnotationDemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/annntation")
public class AnnotationDemoApi {

    @Autowired(required = false)
    @Reference(url = "dubbo://127.0.0.1:28810")//注意這個註解的是dubbo的
    private AnnotationDemoService annotationDemoService;


    @RequestMapping("hello")
    public String sayHello(){
        return annotationDemoService.sayHello();
    }


}

配置
application.properties

server.port=8082
dubbo.application.name=sample-consumer
dubbo.registry.address=N/A


相關文章