Spring Boot Integrated Dubbo

Source: Internet
Author: User
Tags zookeeper

Spring-boot-start-dubbo

Spring-boot-start-dubbo allows you to develop Dubbo programs in a Spring-boot way. Makes Dubbo development so simple.

How to use 1. Clone code (optional, has been published to the central warehouse, can directly rely on a stable version of the Central warehouse)
git clone [email protected]:teaey/spring-boot-starter-dubbo.git
2. Compile and install (optional)
cd spring-boot-starter-dubbomvn clean install
3. Modify MAVEN configuration file (refer to sample Spring-boot-starter-dubbo-sample)
    • The pom.xml of the Spring boot project adds the parent:
<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.6.RELEASE</version></parent>
    • Add the following dependencies in the pom.xml of the Spring Boot project:

Depending on the actual situation depends on the latest version

 <dependency>     <groupId>io.dubbo.springboot</groupId>     <artifactId>spring-boot-starter-dubbo</artifactId>     <version>1.0.0</version> </dependency>
    • The Maven plugin is used to package the executable Uber-jar file and add the following plug-in (this must be loaded in the POM that needs to be packaged as a jar Mudule)
<plugin>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>    <version>1.3.6.RELEASE</version></plugin>
4. Publishing services

Service Interface:

package cn.teaey.sprintboot.test;public interface EchoService {    String echo(String str);}

In Application.properties Add the Dubbo version information and the client timeout information, as follows:

spring.dubbo.application.name=providerspring.dubbo.registry.address=zookeeper://192.168.99.100:32770spring.dubbo.protocol.name=dubbospring.dubbo.protocol.port=20880spring.dubbo.scan=cn.teaey.sprintboot.test

Add Spring.dubbo.scan to Application.properties in spring application to support Dubbo Service Publishing, where scan represents the package directory to be scanned

    • Spring Boot Boot
package cn.teaey.sprintboot.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Server {    public static void main(String[] args) {        SpringApplication.run(Server.class, args);    }}
    • To write your Dubbo service, simply add @Service to the service implementation you want to publish, as follows
package cn.teaey.sprintboot.test;import com.alibaba.dubbo.config.annotation.Service;@Service(version = "1.0.0")public class EchoServerImpl implements EchoService {    public String echo(String str) {        System.out.println(str);        return str;    }}
5. Consumer Dubbo Services
    • In Application.properties Add the Dubbo version information and the client timeout information, as follows:
spring.dubbo.application.name=consumerspring.dubbo.registry.address=zookeeper://192.168.99.100:32770spring.dubbo.scan=cn.teaey.sprintboot.test

Add Spring.dubbo.scan to Application.properties in spring application to support Dubbo Service Publishing, where scan represents the package directory to be scanned

    • Spring Boot Boot
package cn.teaey.sprintboot.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class Client {    public static void main(String[] args) {        ConfigurableApplicationContext run = SpringApplication.run(Client.class, args);        AbcService bean = run.getBean(AbcService.class);        System.out.println(bean.echoService.echo("abccc"));    }}
    • To reference the Dubbo service, simply add @Reference to the service implementation you want to publish, as follows:
package cn.teaey.sprintboot.test;import com.alibaba.dubbo.config.annotation.Reference;import org.springframework.stereotype.Component;@Componentpublic class AbcService {    @Reference(version = "1.0.0")    public EchoService echoService;}
6. Packaging

Server or client startup can be performed directly

Can be packaged into an executable Uber-jar file via the MVN clean package

Spring Boot Integrated Dubbo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.