zoukankan      html  css  js  c++  java
  • dubbo入门(1)

    dubbo是什么:
    1. 负载均衡是对外提供一个公共地址,请求过来时通过轮询、随机等,路由到不同server。目的分摊压力。
    失效备援是发现一台server挂了,就让另外一台去服务了。跟餐馆换个服务员继续招待你一样。

    1. Java下的一套RPC框架(soa思想),作用就是统一管理配置,各个系统服务间的调用。dubbo在淘宝也是解决他们实际问题的,不一定适合其他。 另外各家公司也都有大同小异的实现,所以没多少人用、也就没多少介绍。
      原理就是: A系统调用B系统接口服务, 后面就是怎么把这个流程,动态化(zookeeper通知)、权限化、配置化、低耦合化、自动化。
    package com.dubbo.test;
    
    public interface BankService {
        public String getBinkinfo();
    }
    package com.dubbo.test;
    
    import com.alibaba.dubbo.config.annotation.Service;
    
    @Service(protocol = {"dubbo"}, timeout = 3000)
    public class BankServiceImpl implements BankService{
    
        @Override
        public String getBinkinfo() {
    
            return "icbc";
        }
    
    }

    provider.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://code.alibabatech.com/schema/dubbo
                            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <dubbo:application name="bankServiceProvider"/>
    
        <dubbo:registry address="zookeeper://192.168.1.50:2182" register="true"/>
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.dubbo.test.BankService" ref="bankService" />
    
        <bean id="bankService" class="com.dubbo.test.BankServiceImpl" />
    </beans>

    consumer.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://code.alibabatech.com/schema/dubbo
                            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <dubbo:application name="bankServiceConsumer"/>
    
        <dubbo:registry address="zookeeper://192.168.1.50:2182" register="true"/>
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <dubbo:reference id="bankService" interface="com.dubbo.test.BankService" />
    </beans>
    package dubbotest.d1;
    
    import java.io.IOException;
    
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * dubbo提供者
     * title: ProviderTest.java
     * description: 
     * @author lulei
     * @created 2016年12月29日 上午10:20:24
     */
    public class ProviderTest {
    
        @Test
        public void test1() throws IOException {
             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
             context.start();
             System.in.read(); // 按任意键退出
    
        }
    
    
    }
    
    package dubbotest.d1;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.dubbo.test.BankService;
    
    public class ConsumerTest {
        public static void main(String[] args) {
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
                context.start();
    
                BankService bankService = (BankService)context.getBean("bankService"); // 获取远程服务代理
                String hello = bankService.getBinkinfo(); // 执行远程方法
    
                System.out.println( hello ); // 显示调用结果
        }
    }
    

    pom.xml:

    <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>dubbotest</groupId>
        <artifactId>d1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!-- <packaging>jar</packaging> -->
    
        <name>d1</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <!-- dubbo -->
            <dependency>
                <groupId>dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.8.4</version>
            </dependency>
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.18.1-GA</version>
            </dependency>
            <!-- <dependency>
                 <groupId>io.netty</groupId>
                 <artifactId>netty</artifactId>
                 <version>3.7.1.Final</version>
            </dependency> -->
            <!--spring相关 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.0.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.0.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.0.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.6</version>
            </dependency>
    
          <dependency>
             <groupId>com.github.sgroschupf</groupId>
             <artifactId>zkclient</artifactId>
             <version>0.1</version>
          </dependency>
            <!-- <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> 
                </dependency> -->
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </project>
    
  • 相关阅读:
    echarts3.0 实例容器不实时更新页面的问题
    Mac下搭建atx2环境
    MAC 下SFT环境搭建及使用
    【转发】基本adbui命令使用 可做图像识别
    UIAutomator2的API文档(三)
    UIAutomator2的API文档(二)
    UIAutomator2的API文档(一)
    UIAutomator2安装及连接
    uiautomator2通过wifi操作手机
    ATX-UI自动化环境搭建
  • 原文地址:https://www.cnblogs.com/luleiitlife/p/8545040.html
Copyright © 2011-2022 走看看