spring boot  整合 dubbo的簡單介紹

參考官方文檔

http://dubbo.apache.org/zh-cn/blog/download.html

https://github.com/apache/dubbo-admin

 

接口實現類 @Service  用apach 的 不能用spring的

 

@reference  需要指定版本號

 

需要指定版本號

You can introduce the latest dubbo-spring-boot-starter to your project by adding the following dependency to your pom.xml

<properties>
    <spring-boot.version>2.2.6.RELEASE</spring-boot.version>
    <dubbo.version>2.7.6</dubbo.version>
</properties>
    
<dependencyManagement>
    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

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

<dependencies>
    <!-- Dubbo Spring Boot Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.6</version>
    </dependency>    
</dependencies>

If your project failed to resolve the dependency, try to add the following repository:

<repositories>
    <repository>
        <id>apache.snapshots.https</id>
        <name>Apache Development Snapshot Repository</name>
        <url>https://repository.apache.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

Legacy Versions

If you still use the legacy Dubbo whose version is less than 2.7.0, please use the following Spring Boot starters:

Dubbo Spring Boot Dubbo Spring Boot
0.2.1.RELEASE 2.6.5+ 2.x
0.1.2.RELEASE 2.6.5+ 1.x

Build from Source

If you'd like to attempt to experience latest features, you also can build from source as follow:

  1. Maven install current project in your local repository.

Maven install = mvn install

Getting Started

If you don't know about Dubbo, please take a few minutes to learn http://dubbo.apache.org/. After that you could dive deep into dubbo user guide.

Usually, There are two usage scenarios for Dubbo applications, one is Dubbo service(s) provider, another is Dubbo service(s) consumer, thus let's get a quick start on them.

First of all, we suppose an interface as Dubbo RPC API that a service provider exports and a service client consumes:

public interface DemoService {

    String sayHello(String name);

}

Dubbo service(s) provider

  1. Service Provider implements DemoService

    @Service(version = "1.0.0")
    public class DefaultDemoService implements DemoService {
    
        /**
         * The default value of ${dubbo.application.name} is ${spring.application.name}
         */
        @Value("${dubbo.application.name}")
        private String serviceName;
    
        public String sayHello(String name) {
            return String.format("[%s] : Hello, %s", serviceName, name);
        }
    }
  2. Provides a bootstrap class

    @EnableAutoConfiguration
    public class DubboProviderDemo {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboProviderDemo.class,args);
        }
    }
  3. Configures the application.properties:

    # Spring boot application
    spring.application.name=dubbo-auto-configuration-provider-demo
    # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
    dubbo.scan.base-packages=org.apache.dubbo.spring.boot.demo.provider.service
    
    # Dubbo Application
    ## The default value of dubbo.application.name is ${spring.application.name}
    ## dubbo.application.name=${spring.application.name}
    
    # Dubbo Protocol
    dubbo.protocol.name=dubbo
    dubbo.protocol.port=12345
    
    ## Dubbo Registry
    dubbo.registry.address=N/A

Dubbo service(s) consumer

  1. Service consumer also provides a bootstrap class to reference DemoService

    @EnableAutoConfiguration
    public class DubboAutoConfigurationConsumerBootstrap {
    
        private final Logger logger = LoggerFactory.getLogger(getClass());
    
        @Reference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
        private DemoService demoService;
    
        public static void main(String[] args) {
            SpringApplication.run(DubboAutoConfigurationConsumerBootstrap.class).close();
        }
    
        @Bean
        public ApplicationRunner runner() {
            return args -> {
                logger.info(demoService.sayHello("mercyblitz"));
            };
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章