dubbo+spring_mvc配置使用

dubbo分佈式服務配置git

分爲兩個工程provider和consumergithub

輸入圖片說明

pom文件配置(provider與consumer相同)web

<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>
	<artifactId>dubbo_example_provider</artifactId>
	<parent>
		<groupId>com.zhu.example</groupId>
		<artifactId>example</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<dependencies>
		<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>${dubbo.version}</version>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>${zkclient.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>${zookeeper.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-log4j12</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>



		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>

		<!-- spring-test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

除去作單元檢測的依賴包,核心依賴包有spring

<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>${dubbo.version}</version>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>${zkclient.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>${zookeeper.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-log4j12</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>



		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

dubbo-provider 工程apache

接口類DubboxProviderNoSerial,NoSerialEntity 爲個人實驗實體類mvc

package com.zhu.example.dubbox;

import com.zhu.example.entity.NoSerialEntity;

public interface DubboxProviderNoSerial {

	public NoSerialEntity testDubboxNoSerialParam();
}

實現類DubboxProviderNoSerialImplapp

package com.zhu.example.dubbox;

import com.zhu.example.entity.NoSerialEntity;


public class DubboxProviderNoSerialImpl implements DubboxProviderNoSerial {

	/* (non-Javadoc)
	 * @see com.zhu.example.dubbox.DubboxProviderNoSerial#testDubboxNoSerialParam()
	 */
	public NoSerialEntity testDubboxNoSerialParam() {
		NoSerialEntity test=new NoSerialEntity();
		test.setName("test");
		return test;
	}

	
}

dubbo-spring-provider配置負載均衡

<?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">

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
    </bean>

    <!-- dubbo 配置 -->
    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="${dubbo.application.name}"/>
    <!-- dubbo服務註冊地址 -->
    <dubbo:registry address="${dubbo.registry.address}"/>
    <!-- dubbo服務協議名稱及端口 -->
    <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}"/>
	<!-- 經過註冊中心發現監控中心服務 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>

    <!-- 服務接口實現聲明 -->
    <bean id="dubboxProviderNoSerialImpl" class="com.zhu.example.dubbox.DubboxProviderNoSerialImpl"/>
    <!-- 服務接口暴露聲明 -->
    <dubbo:service interface="com.zhu.example.dubbox.DubboxProviderNoSerial" ref="dubboxProviderNoSerialImpl" version="0.1" retries="0" timeout="35000" loadbalance="${dubbo.service.loadbalance}" filter="logcontext"/>
</beans>

provider配置文件application.propertiesmaven

#dubbo
#提供方應用信息,用於計算依賴關係
dubbo.application.name=dubbox_provider
#dubbo服務註冊地址
dubbo.registry.address=zookeeper://localhost:2181
#經過註冊中心發現監控中心服務
dubbo.monitor.protocol=registry
#dubbo服務協議名稱及端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#dubbo服務負載均衡設置
dubbo.service.loadbalance=consistenthash
#日誌文件配置
dubbo.log4j.file=logs/dubbox_provider.log
dubbo.log4j.level=DEBUG

junit Test類分佈式

package com.zhu.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-dubbo-provider.xml")
public class TestDubboxProvider {
	
	@Test
	public void testProvider() throws InterruptedException {
		
		while(true){
			System.out.println("spring container is alived!");
			Thread.sleep(10000L);
		}
	}

}

dubbo——consumer工程

DubboxProviderNoSerial接口與provider相同

application.properties配置文件

#dubbo
dubbo.application.name=dubbox_consumer
dubbo.registry.address=zookeeper://localhost:2181

applicationContext-dubbo-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 配置 -->
    <dubbo:application name="${dubbo.application.name}"/>
    <dubbo:registry address="${dubbo.registry.address}" timeout="15000"/>
    <dubbo:consumer check="false"/>

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
    </bean>

    <!-- 消費者聲明 -->
    <dubbo:reference id="dubboxNoSerial" interface="com.zhu.example.dubbox.DubboxProviderNoSerial" version="0.1" filter="logcontext"/>
</beans>

junit Test類

package com.zhu.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

import com.zhu.example.dubbox.DubboxProviderNoSerial;
import com.zhu.example.entity.NoSerialEntity;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-dubbo-consumer.xml")
public class TestDubboxConsumer {
	@Autowired
	DubboxProviderNoSerial dubboxNoSerial;

	@Test
	public void testConsumer() throws InterruptedException {
		
		NoSerialEntity result = dubboxNoSerial.testDubboxNoSerialParam();
		System.out.println(result.getName());
		Assert.notNull(result);
	}
	
}
相關文章
相關標籤/搜索