Play Spring Boot Integration Dubbo

Source: Internet
Author: User
Tags zookeeper

Play Spring boot integration Dubbo using spring boot and Dubbo integration, here I have tried to use annotations, the simple use of annotations to register the service is actually no problem, but when you involve the use of annotations in the service to refer to the transaction, injection of other objects, There will be some problems. So I decided to give up the annotations, using XML, which may be described here is Dubbo, but if the use of Dubbox, it is basically compatible. Next, we'll talk about using XML as a way to develop with spring boot. 1. Create a project to add dependencies in Pom.xml the project name is: (1) springboot-dubbo-provide (2) Springboot-dubbo-api (3) Springboot-dubbo-consume Springboot-dubbo-api project is mainly to put some service interface, used to provide consumers with. Springboot-dubbo-provide Engineering is used to provide services. Springboot-dubbo-consume works for consumers. Open Pom.xml in the Springboot-dubbo-provide project to add the following dependencies, complete the code as follows: [HTML] View plain copy 4.0.0 Com.chengli Springboot-dubbo-provide 0.0.1-snapshot Jar Springboot-dubbo-provide http://maven.apache.org org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 1.8 2.5.3 3.4.6 0.1 com.chengli springboot-dubbo-api 0.0.1-snapshot org.springframework.boot spring-boot-starter /dependency>!--Dubbo--> Com.alibaba Dubbo org.springframework spring ${com.al Ibaba.dubbo.version} org.apache.zookeeper zookeep Er ${org.apache.zookeeper.version} com.github. SGROSCHUPF zkclient ${com.github.sgroschupf.zkclient.version} /dependencies> org.springframework.boot spring-boot-maven-plugin Open the Springboot-dubbo-consume project and add the following dependencies to the Pom.xml with the complete code as follows: [HTML] View plain copy 4.0.0 Com.chengli Springboot-dubbo-consume 0.0.1-snapshot Jar Springboot-dubbo-consume http://maven.apache.org org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 1.8 2.5.3 3.4.6 0.1 com.chengli springboot-dubbo-api 0.0.1-snapshot org.springframework.boot spring-boot-starter-web Com.alibaba Dubbo org.springframework spring ${com.alibab A.dubbo.version} org.apache.zookeeper zookeeper ${org.apache.zookeeper.version} com.github.sgroschupf zkclient ${com.github.sgroschupf.zkclient.version} org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-maven-plugin 2.DUBBO Configuration 2.1springboot-dubbo-provide service Provider (1) Create a Portal startup class Mainconfig in the Springboot-dubbo-provide project, complete with the following code: [Java] View Plain Copypackage Com.chengli.springboot; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Mainconfig {public static void main (string[] args) {Springapplication.run (mainconfi G.class, args); try {System.in.read ();} catch (Exception e) {e.printstacktrace ();}} } (2) Create Dubbo configuration class [Java] View plain copypackage Com.chengli.springboot.dubbo; Import org.springframework.context.annotation.Configuration; Import Org.springframework.context.annotation.ImportResource; Import Org.springframework.context.annotation.PropertySource; @Configuration @PropertySource ("Classpath:dubbo/dubbo.properties") @ImportResource ({"Classpath:dubbo/*.xml"}) public class Dubboconfig {} (3) Creates a Dubbo configuration file under Src/main/resources to create a new folder Dubbo, and adds the following configuration: Dubbo-provider.xml content is as follows: [HTML] View Plain Copy Note: The example service I published here is an example of the actual modification (4) to create the dubbo.properties[html] View plain copy# app name Dubbo.application.name= Example-provider #注册中心类型 dubbo.registry.protocol=zookeeper #注册中心地址 dubbo.registry.address=127.0.0.1:2181 #暴露服务方式 Dubbo.protocol.name=dubbo #暴露服务端口 dubbo.protocol.port=20880 2.2springboot-dubbo-consume service Consumer (1) Create a Portal startup class mainconfig[ Java] View plain copypackage com.chengli.springboot; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Mainconfig {public static void main (string[] args) {Springapplication.run (mainconfi G.class, args); }} (2) Create Dubbo configuration class [Java] View plain copypackage Com.chengli.springboot.dubbo; Import org.springframework.context.annotation.Configuration; Import Org.springframework.context.annotation.ImportResource; Import Org.springframework.context.annotation.PropertySource; @Configuration @PropertySource ("Classpath:dubbo/dubbo.properties") @ImportResource ({"Classpath:Dubbo/*.xml "}) public class Dubboconfig {} (3) Create a Dubbo configuration file under src/main/resources New Folder Dubbo and add the following configuration: Dubbo-consume.xml content is as follows: [HTML] View plain copy (4) Create dubbo.properties[html] View plain copy# app name Dubbo.application.name=example-consume #注册中心类型 Dubbo.registry.protocol=zookeeper #注册中心地址 dubbo.registry.address=127.0.0.1:2181 Here is basically ready, but the code for the test class is not posted. Just define the interface implementation in the API. When using spring boot to integrate with Dubbo, it is important to note that you do not use the Devtools hot start provided by spring Boot, because Devtools provides two classloader, and the load policy issue causes an error and cannot be started. If hot loading is required in development, use the springloaded provided by spring. Interested friends can add groups to explore mutual learning:

Play Spring Boot Integration 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.