Dubbo框架——消費者服務註冊Zookeeper

使用此項目的前提是有生產者和註冊服務中心:https://blog.csdn.net/weixin_40160361/article/details/104735273

一、新建Maven項目

二、將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>markzp</groupId>
    <artifactId>dubbo-order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
    	<dependency>
	    	<groupId>markzp</groupId>
		    <artifactId>dubbo-memory-api</artifactId>
		    <version>0.0.1-SNAPSHOT</version>
	    </dependency>
	    <dependency>
		    <groupId>org.apache.curator</groupId>
		    <artifactId>curator-framework</artifactId>
		    <version>4.0.1</version>
		</dependency>
	    <dependency>
	    	<groupId>org.apache.zookeeper</groupId>
		    <artifactId>zookeeper</artifactId>
		    <version>3.4.6</version>
		</dependency>
		<dependency>
		    <groupId>io.netty</groupId>
		    <artifactId>netty-all</artifactId>
		    <version>4.1.0.Final</version>
		</dependency>
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>dubbo</artifactId>
		    <version>2.6.1</version>
		    <exclusions>
		    	<exclusion>
			    	<groupId>io.netty</groupId>
			    	<artifactId>netty-all</artifactId>
		    	</exclusion>
		    	<exclusion>
		    		<groupId>org.apache.zookeeper</groupId>
		    		<artifactId>zookeeper</artifactId>
		    	</exclusion>
		    </exclusions>
		</dependency>
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context</artifactId>
		    <version>4.3.9.RELEASE</version>
		</dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

三、添加order-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="order-consumer" />
     
     <dubbo:registry address="zookeeper://localhost:2181" />
     
     <dubbo:reference id="memoryService" interface="markzp.dubbo_memory_api.MemoryInterface" />
</beans>

四、添加啓動類

package markzp.dubbo_order;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import markzp.dubbo_memory_api.MemoryInterface;

public class OrderTest {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("order-provider.xml");
		MemoryInterface bean = app.getBean(MemoryInterface.class);
		System.out.println("消費服務啓動了....");
		String name = bean.name("Order調用Memory接口...");
		System.out.println(name);
	}

}

五、啓動

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章