Dubbo and Spring Boot Integrated simplified version (XML and annotation)

Source: Internet
Author: User
Spring Boot and Dubbo consolidated XML + annotation version

(It feels like the integration between Spring and Dubbo)
Provider key points:
1. Dubbo.properties configuration Dubbo Various parameters application, Registry.addres
Dubbo.provider.xml here is the use of the <dubbo: .../> tag to import the parameters in the Dubbo.properties and output the provider instance

<dubbo:application name= "${dubbo.application.name}"/>
    <dubbo:registry protocol= "${ Dubbo.registry.protocol} "address=" ${dubbo.registry.address} "/>
    <dubbo:protocol name=" ${ Dubbo.protocol.name} "port=" ${dubbo.protocol.port} "/>
    <dubbo:service interface=" Com.zhb.app.dubbo.Hello "ref=" Helloimpl "/>

Where Helloimpl is in the code, the instance defined by the annotation is used.
2. Use the following annotation to introduce the configuration file in spring boot

@Configuration
@PropertySource ("classpath:dubbo/dubbo.properties")  
@ImportResource ({"classpath:dubbo/* . xml "}) public 
class Dubboconfig {

}

Next
3. The third-party jar defined by Build.gradle, in addition to the spring boot-related
Also: Dubbo and zkclient,zkclient if not, the project start will be an error. I don't know why I need it for the moment. There are API jars common to both consumer and provider.

Consumer Essentials
1. Dubbo.properties reduced exposure to service mode configuration
Dubbo-consumer.xml

<!--provider app information for compute dependencies--  
    <dubbo:application name= "${dubbo.application.name}"/>  
    <!-- Registry Exposure Service Address--  
   <dubbo:registry protocol= "${dubbo.registry.protocol}" address= "${dubbo.registry.address} "/>    
    <dubbo:reference id=" Helloimpl "interface=" Com.zhb.app.dubbo.Hello "/>  

And then the code inside @autowire
2. Annotation the introduction of the configuration file ibid.
3. Build.gradle with spring boot and Dubbo to integrate pure annotation version

Provider key points:
1. Using annotation to introduce parameters,
Dubbo.annotation.package This parameter is more important, set up to use annotation scan
Use beans such as annotationbean,applicationconfig to inject Dubbo parameters.

@Configuration
@ConditionalOnClass (exporter.class)
@PropertySource (value = "classpath:dubbo/ Dubbo.properties ") Public
class Dubboconfiguration {

    @Value (" ${dubbo.application.name} ")
    private String ApplicationName;

   @Bean public
    static Annotationbean Annotationbean (@Value ("${dubbo.annotation.package}") String PackageName) {
        Annotationbean Annotationbean = new Annotationbean ();
        Annotationbean.setpackage (PackageName);
        return annotationbean;
    }

The following code omits .....
2. Use spring's @component and Dubbo's @service to improve an example

@Component
@Service (version= "1.0.0") Public
class Helloimpl implements hello{
    private static final Logger Logger = Loggerfactory.getlogger (helloimpl.class);
    @Override public
    string SayHello (string name) {
        Logger.info ("Recevie invoke," + name);
        Return "Hello World," + name;
    }

}

Next
3. Previous provider.xml and related classes can be used, other configurations will not change

Consumer Key points:
1. Using annotation to introduce parameters, similar to the above, but to remove the protocolconfig configuration, and change the Providerconfig to Consumerconfig, others remain unchanged
2. Introducing an example using Dubbo's reference annotations

@Reference (Version = "1.0.0")
    private Hello hello;

You can use it directly
3. Previous consumer.xml and related classes can be used, other configurations will not change

Finally add the Build.gradle configuration file

Import java.lang.invoke.LambdaForm.Compiled;
    buildscript{ext{springbootversion = ' 1.3.5.RELEASE '} repositories {mavencentral ()} dependencies {classpath ("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}")}} APPL Y plugin: ' java ' Apply plugin: ' Eclipse ' Apply plugin: ' Spring-boot ' springboot {Backupsource = False mainClass = ' com.zhb.app.PortalApplication '} jar {baseName = ' springboottest ' version = ' 0.0.1-snapshot '}//apply Plug In: ' Application '//applicationdefaultjvmargs = ['-javaagent:e:\\xgsdk\\commonlib\\springloaded-1.2.5.release.jar- Noverify '] repositories {mavencentral ()} dependencies {compile Filetree (dir: "Libs", include: "*.jar") CO
            Mpile "Org.springframework.boot:spring-boot-starter-web" Compile ("com.alibaba:fastjson:1.2.4", "commons-codec:commons-codec:1.5", "org.apache.commons:commons-lang3:3.3.2", "Com.alibaba:dubbo:2.5.6 ") Compile (" com.github.sgroschupf:zkclient:0.1 ") {Exclude group: ' ORG.SLF4J ' } testcompile ("Org.springframework.boot:spring-boot-starter-test")}

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.