ダボの基本(1):導入と構築

ダボは:

  • 分散型の高性能で透過的なRPCサービスフレームワーク。
  • 自動登録やサービスの自動検出など、効率的なサービスセルフケアソリューションを提供します。
  • 主な機能には、高性能NIO通信とマルチプロトコル統合、サービスの動的アドレス指定とルーティング、ソフトロードバランシングとフォールトトレランス、依存関係の分析と劣化などが含まれます。

ダボの構造と機能:

  1. コンテナーは、プロバイダーの開始、ロード、および実行を担当します
  2. プロバイダーが起動したら、独自のサービスをレジストリに登録します
  3. cousumerが起動したら、独自のサービスをレジストリにサブスクライブします
  4. レジストリは、プロバイダーのリストをコンシューマーに提供し、変更をリアルタイムでプッシュします
  5. コンシューマーは、プロバイダーリストに従って、ロードアルゴリズムに従って呼び出すプロバイダーを選択します。
  6. モニターはrpcの呼び出し頻度をカウントします

プロジェクトに配備されたダボ:

  1. アプリケーションサービスapplication 内には、外部サービス(プロバイダー)と外部サービスへの依存(コンシューマー)の両方があります

  2. プロバイダー涉及:レジストリ/プロトコル/サービス/メソッド/プロバイダー

  3. 消費者涉及:レジストリ/参照/メソッド/消費者

  4. 各サービスインターフェースの情報はモニターに反映されます。応用所属するアプリケーションの名前を識別します。

分散プロジェクトを構築し、dubboを使用してサービスを管理するだけです。

環境の依存関係:zookeeper、jdk1.8、tomcat7、dubbo-admin(コンソール)

dubbo-adminのインストール:

  1.     ダウンロードリンク:https//github.com/apache/incubator-dubbo/archive/dubbo-2.6.0.zip
  2.      解凍してdubbo-adminディレクトリに入り、コマンドウィンドウを開いて、コマンドmvn cleanpackageを入力します。
  3.      warパッケージを解凍し、dubbo構成ファイルdubbo.propertiesを見つけて、zk接続アドレスを変更します
  4.      このフォルダをtomcatのwebappsディレクトリに置き、tomcatを起動します
  5.      localhost:8080 /ファイル名、ユーザー名/パスワードroot / rootを入力します

1つは、単純なmvcプロジェクトstorePortalを作成する

mvcに必要な依存関係を導入することに加えて、pomファイルはdubbo関連の依存関係も導入する必要があります

       <dependency>
        	<groupId>com.101tec</groupId>
        	<artifactId>zkclient</artifactId>
        	<version>0.3</version>
        </dependency>
        
        <dependency>
        	<groupId>org.apache.zookeeper</groupId>
        	<artifactId>zookeeper</artifactId>
        	<version>3.4.5</version>
        </dependency>
		    
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.0</version>
			<scope>compile</scope>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
         </dependency>

このモジュールには、外部からアクセスするか、Tomcatプラグインを追加するか、パッケージ化してTomcatに入れる必要があります。

<build>
	   <plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
   </build>

web.xml構成

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>storePortal</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Spring MVC servlet -->
    <servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
    <welcome-file-list>  
        <welcome-file>/index.jsp</welcome-file>  
    </welcome-file-list>  
</web-app>

ダボ構成メソッドには、XML、peoperties、アノテーション、およびapiメソッドが含まれます。

xmlの方法:

サービスプロバイダー:xml構成は、サービスクラスをSpringのiocコンテナーに配信することであり、dubboはサービスをrpcサービスとして開き、外部にサービスを提供するため、サービスクラスに@Serviceアノテーションを追加する必要はありません。

<?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:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
	 http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <context:component-scan base-package="com.enjoy.dao"/>

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="storeServer"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.244.2:2181"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:protocol name="rmi" port="21880"/>

    <!-- 
               声明需要暴露的服务接口
        dubbo把ioc容器内的service开放成rpc服务 
     -->
    <dubbo:service interface="com.enjoy.service.OrderService" ref="orderService" >
        <!-- <dubbo:method name="getDetail" cache="lru" /> -->
    </dubbo:service>

    <dubbo:service interface="com.enjoy.service.UserService" ref="userService" />
    <dubbo:service interface="com.enjoy.service.VipUserService" ref="vipUserService" />

     <!--
                 和本地bean一样实现服务 
                 把服务交给ioc容器管理
     -->
    <bean id="orderService" class="com.enjoy.service.impl.OrderServiceImpl"/>
    <bean id="userService" class="com.enjoy.service.impl.UserServiceImpl"/>
    <bean id="vipUserService" class="com.enjoy.service.impl.VipUserServiceImpl"/>

サービスコンシューマー:xmlメソッドは、参照されたサービスをSpringのiocコンテナーに提供することです。サービスがコンテナーから取得される場合は、@ Autowiredでアノテーションを付ける必要があります。

<?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="enjoyStore"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.244.2:2181"/>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="orderService" interface="com.enjoy.service.OrderService" >
    </dubbo:reference>

    <dubbo:reference id="userService" interface="com.enjoy.service.UserService"  />
    <dubbo:reference id="vipUserService" interface="com.enjoy.service.VipUserService"  />


</beans>
@Controller
public class IndexController implements ApplicationContextAware{
	 private ApplicationContext context;

	   
	    @Autowired
	    private UserService userService;

	   
	    @Autowired
	    private OrderService orderService;
}

そのため、このサービスをご利用いただけます。

プロパティメソッド:xmlメソッドの補足です。xml構成で構成情報が定義されていない場合、dubboはdubbo.propertiesファイルからそれを読み取ります。関連情報がxmlで定義されている場合、関連するpeopertiesの構成は有効になりません、プロパティファイルのレベルは最低です。通常、接続情報はプロパティファイルに設定され、このファイルはdubbo.propertiesという名前のResourcesフォルダーに配置され、dubboはこのファイルを自動的にロードします。

# 应用名
dubbo.application.name=enjoyStore_properties
# 注册中心地址
dubbo.registry.address=zookeeper://192.168.244.2:2181
# 调用协议地址
dubbo.protocol.name=dubbo
dubbo.protocol.port=28080

注釈モード構成ダボ

サービスプロバイダー:アノテーションメソッドは、スキャンパッケージをxmlに追加し、指定されたパッケージの下のクラスをスキャンすることです。@ Service(このアノテーションはダボアノテーションです)アノテーションがクラスで見つかった場合、クラスは引き渡されます。 iocコンテナ管理を強化し、このクラスをrpcサービスとして開いて外部サービスを提供します

<?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:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
	 http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <context:component-scan base-package="com.enjoy.dao"/>

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="storeServer_annotation"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.244.2:2181"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="rmi" port="20880"/>

    <dubbo:annotation package="com.enjoy.service" />

</beans>

サービスコンシューマー:アノテーションメソッドは、スキャンパッケージを指定し、指定されたパッケージの下のクラスをスキャンし、@ Referenceアノテーション(dubboアノテーション)を持つクラスメンバー変数を見つけることです。その後、プロキシサービスオブジェクトがこのクラスに挿入されて完了します。呼び出し。ここではSpringアノテーションを使用できません。参照を完了するには、dubboアノテーションを使用する必要があります。

@Controller
public class IndexController implements ApplicationContextAware{
	 private ApplicationContext context;

	    @Reference
	    private UserService userService;

	    @Reference
	    private OrderService orderService;
}

APIメソッド:このメソッドは開発中には使用されませんが、このメソッドの構造はより明確で、習得と使用が容易になります。

サービスプロバイダー:

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.enjoy.service.VipUserService;
import com.enjoy.service.impl.VipUserServiceImpl;

import java.io.IOException;

public class StoreProvider {
    public static void main(String[] args) throws IOException {
        initDubbo();
    }

    public static void initDubbo() throws IOException {
            // 当前应用配置
            ApplicationConfig application = new ApplicationConfig();
            application.setName("StoreServerApi");

            // 连接注册中心配置
            RegistryConfig registry = new RegistryConfig();
            registry.setProtocol("zookeeper");
            registry.setAddress("192.168.244.2:2181");

            // 服务提供者协议配置
            ProtocolConfig protocol = new ProtocolConfig();
            protocol.setName("rmi");
            protocol.setPort(21880);
            protocol.setThreads(100);

            // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
            // 服务提供者暴露服务配置
            // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
            ServiceConfig<VipUserService> service = new ServiceConfig<>();

            service.setApplication(application);
            service.setRegistry(registry); // 多个注册中心可以用setRegistries()
            service.setProtocol(protocol); // 多个协议可以用setProtocols()
            service.setInterface(VipUserService.class);
            service.setRef(new VipUserServiceImpl());

            // 暴露及注册服务
            service.export();
            System.out.println("dubbo服务开启。。。。。。。。");
            System.in.read();

    }
}

サービス消費者:

import com.alibaba.dubbo.config.*;
import com.enjoy.service.VipUserService;
import java.io.IOException;

public class StoreConsumer {
    public static void main(String[] args) throws IOException {
        // 当前应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("StoreServerClientApi");

        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setProtocol("zookeeper");
        registry.setAddress("192.168.244.2:2181");

        // 服务提供者协议配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(20882);
        protocol.setThreads(100);

        // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
        // 引用远程服务
        ReferenceConfig<VipUserService> reference = new ReferenceConfig<>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
        reference.setInterface(VipUserService.class);

        // 和本地bean一样使用xxxService
        VipUserService vipUserService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
        String ret = vipUserService.getVipDetail("123");
        reference.destroy();
        System.out.println(ret);
        System.in.read();
    }
}

 

おすすめ

転載: blog.csdn.net/csdnbeyoung/article/details/91830735