Enterprise applications, how the services of three (dubbo entry-case) enterprise applications, how the services of two (dubbo architecture)

  Today is Children's Day, thousands of miles away from Guangzhou, back home in Guizhou, nice! Haoshanhaoshui good mood, well then write something. This enterprise-class applications, how the services of the third in the series. In the last one: enterprise applications, how the services of two (dubbo architecture) , the understanding of the overall architecture dubbo, and dubbo four characteristics: connectivity, robustness, scalability, upgradeability. The following first to implement an entry-level demo, intuitive feel.

1. Remarks

By a simple case, the presentation entry dubbo use. Only cases service providers, service consumers.

2. Case realization

  2.1. Create a project

 

 

  2.2 Configuration dependencies are imported pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.anan</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <!--spring 版本-->
        <spring.version>5.0.2.RELEASE</spring.version>
        <!--dubbo版本-->
        <dubbo.version>2.7.0</dubbo.version>
    </properties>

    <dependencies>
        <!--spring依赖包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

    </dependencies>
</project>

 

  2.3 server-side development

    2.3.1.service Interface

Package com.anan.dubbo.service; 

/ ** 
 * service interface 
 * / 
public  interface the HelloService { 

    / ** 
     * hello 
     * / 
    String the sayHello (String name); 
}

 

    2.3.2.service interface

package com.anan.dubbo.service.impl;

import com.anan.dubbo.service.HelloService;

/**
 * 服务接口实现
 */
public class HelloServiceImpl implements HelloService{

    /**
     * 问好
     *
     * @param name
     */
    public String sayHello(String name) {
        return "hello,"+name+"!";
    }
}

 

 

  2.4. Configuration Service Provider

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xmlns: Dubbo =" http://dubbo.apache.org/schema/dubbo " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
       HTTP: // the WWW. springframework.org/schema/beans/spring-beans.xsd 
       http://dubbo.apache.org/schema/dubbo 
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd " > 

    <-! offers party application information, for calculating the dependencies -> 
    < Dubbo:application name="provider-hello-world-app"  />

    <-! Using multicast broadcast exposure registry service address -> 
    < dubbo: Registry address = "multicast: //224.1.1.1: 6666" /> 

    <! - with dubbo exposed service agreement in 20880 port -> 
    < Dubbo: Protocol name = "Dubbo" Port = "20880" /> 

    <-! statement needs to be exposed service interface -> 
    < Dubbo: service interface = "com.anan.dubbo.service.HelloService" ref = "helloService" /> 

    <! - and implement the service as local bean -> 
    < bean the above mentioned id = "helloService" class = "com.anan.dubbo.service.impl.HelloServiceImpl"/>

</beans>

 

  2.5. Configuration service consumer

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xmlns: Dubbo =" http://dubbo.apache.org/schema/dubbo " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
       HTTP: // the WWW. springframework.org/schema/beans/spring-beans.xsd 
       http://dubbo.apache.org/schema/dubbo 
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd " > 

    <-! consumption party application name, used to calculate dependencies, not matching conditions, do not like the provider -> 
    < Dubbo:application name="consumer-hello-world-app"  /> 

    <! - use multicast broadcast registry exposure to discovery service address -> 
    < Dubbo: Registry address = "multicast: //224.1.1.1: 6666" /> 

    <! - generated the Remote Service Agent, and local bean can Like using the helloService -> 
    < Dubbo: Reference ID = "the helloService" interface = "com.anan.dubbo.service.HelloService"  /> 

</ Beans >

 

  2.6. Test

    2.6.1. Start the service provider Provider

Package com.anan.dubbo.provider; 

Import org.springframework.context.ApplicationContext;
 Import org.springframework.context.support.ClassPathXmlApplicationContext; 

/ ** 
 * provider 
 * / 
public  class Provider { 

    public  static  void main (String [] args) throws Exception {
         // load spring configuration file, creating spring containers 
        the ApplicationContext context = new new the ClassPathXmlApplicationContext ( "CLASSPATH: provider.xml" ); // blocking: waiting for input, press any key to exit         System.in.read (); 
    } 
}
                

        

 

    2.6.2. Start Consumers Consumer Services

Package Penalty for com.anan.dubbo.consumer; 

Import com.anan.dubbo.service.HelloService;
 Import org.springframework.context.ApplicationContext;
 Import org.springframework.context.support.ClassPathXmlApplicationContext; 

/ ** 
 * Consumer Services 
 * / 
public  class Consumer { 

    public  static  void main (String [] args) {
         // load the spring configuration file, create a spring container 
        ApplicationContext context = new new ClassPathXmlApplicationContext ( "the CLASSPATH: consumer.xml" ); // get remote service agent 
        helloService helloService = (helloService ) context.getBean ( "helloService"
                

        );
         // System.out.println (helloService.getClass ()); 

        // perform remote method 
        String helloService.sayHello Result = ( "Anan" ); 
        System.out.println (Result); 


    } 
}

 

Guess you like

Origin www.cnblogs.com/itall/p/10958365.html