zoukankan      html  css  js  c++  java
  • Dubbo

    1. dubbo-api

    service:

    package com.jcx.dubbo.demo.service;
    
    public interface IDemoService {
    
        String sayHello(String userName);
    
    }

    2. dubbo-provider:

    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.jcx.dubbo.demo</groupId>
        <artifactId>dubbo-provider</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.12.0</version>
            </dependency>
        </dependencies>
    
    </project>

    service.impl

    package com.jcx.dubbo.demo.service.impl;
    
    import com.jcx.dubbo.demo.service.IDemoService;
    
    public class DemoServiceImpl implements IDemoService {
    
        public String sayHello(String userName) {
            return "Hello," + userName;
        }
        
    }

    ProviderApplication:

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    public class ProviderApplication {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
            ioc.start();
            System.out.println("provider start...");
            System.in.read();
        }
    }

    provider.xml:

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://dubbo.apache.org/schema/dubbo
           http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!--指定当前服务名-->
        <dubbo:application name="dubbo-demo-provider"/>
    
        <!--指定注册中心-->
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    
        <!--指定通信规则(通信协议,通信端口)-->
        <dubbo:protocol name="dubbo" port="20880"/>
    
        <!--暴露服务 ref指向真正实现-->
        <dubbo:service interface="com.jcx.dubbo.demo.service.IDemoService" ref="demoServiceImpl"/>
    
        <!--服务实现-->
        <bean id="demoServiceImpl" class="com.jcx.dubbo.demo.service.impl.DemoServiceImpl"/>
    
    </beans>

    3. dubbo-consumer:

    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.jcx.dubbo.demo</groupId>
        <artifactId>dubbo-consumer</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.12.0</version>
            </dependency>
        </dependencies>
    
    </project>

    ConsumerApplication:

    import com.jcx.dubbo.demo.service.IDemoService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    public class ConsumerApplication {
    
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
            applicationContext.start();
            IDemoService demoService = (IDemoService) applicationContext.getBean("demoService");
            String hello = demoService.sayHello("jcx");
            System.out.println(hello);
        }
    }

    consumer.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://dubbo.apache.org/schema/dubbo
           http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!--指定当前服务名-->
        <dubbo:application name="dubbo-demo-consumer"></dubbo:application>
    
        <!--指定注册中心-->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    
        <!--生成远程服务代理接口  check:启动时是否检查-->
        <dubbo:reference id="demoService" interface="com.jcx.dubbo.demo.service.IDemoService" check="false"/>
    </beans>

    注:先启动提供者,后启动消费者

  • 相关阅读:
    【郑轻邀请赛 G】密室逃脱
    【郑轻邀请赛 C】DOBRI
    【郑轻邀请赛 F】 Tmk吃汤饭
    【郑轻邀请赛 I】这里是天堂!
    【郑轻邀请赛 B】base64解密
    【郑轻邀请赛 A】tmk射气球
    【郑轻邀请赛 H】 维克兹的进制转换
    解决adb command not found以及sdk环境配置
    adb shell 命令详解,android, adb logcat
    Unexpected exception 'Cannot run program ... error=2, No such file or directory' ... adb'
  • 原文地址:https://www.cnblogs.com/s-star/p/12464753.html
Copyright © 2011-2022 走看看