国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

搭建 maven+springmvc+dubbo

2019-11-06 09:57:48
字體:
來源:轉載
供稿:網友

新建3個maven項目

1,test-dubbo-PRovider ,java項目,作為提供者,serviceImpl 和dao 層2,test-public-interface,java項目,存放公共的接口,servicei是service的接口(備注:1要依賴2,3也要依賴2)3,test-web-consumer,web項目,存放 Controller 和 頁面 首先我們來開發服務的提供者也就是 test-dubbo-provider ,目錄結構為:

applicationContent-dubbo.xml        dubbo服務的配置文件(名字隨意)     (待會再介紹)ApplicationContent.xml       spring的配置文件這兩個配置文件 必須要放到 MATE-INF/spring/下面 (原因后面再說)UserServiceImpl.java   接口的實現類

package com.cl.user.serviceImpl;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import com.cl.user.servicei.UserService;@Service("userService")@Componentpublic class UserServiceImpl implements UserService{	public String sayHello(String  id) {		System.out.println("hello world----------------------------");		StringBuffer sb=new StringBuffer();;		for (int i = 0; i < 10; i++) {			sb=sb.append("hello world-->"+i+"===="+id+"/n");		}		return sb.toString();	}	public String test(int a,int b) {		return (a+b)+"";	}	public String test2() {		String str="hello dubbo";		return str;	}}有了實現類,還需要接口,也就是項目test-public-interface 

這個項目很簡單 只需要 接口 就行了

package com.cl.user.servicei;public interface UserService {	public String sayHello(String id);	public String test(int a,int b);	public String test2();}

當然 項目 1 要 依賴 項目 2  

所以在1 的 pom.xml 中 有

<dependency>	<groupId>test-web</groupId>	<artifactId>test-pubilc-interface</artifactId>	<version>0.0.1-SNAPSHOT</version>	<scope>test</scope></dependency>這樣 1 中的 UserServiceImpl 就可以實現 UserService接口

當然我們用到 dubbo,就要有dubbo的核心jar包和zookeeper的jar包,所以在1 的 pom.xml 中有

<dependency>	<groupId>com.alibaba</groupId>	<artifactId>dubbo</artifactId>	<version>2.5.3</version>	<exclusions>		<exclusion>			<groupId>org.springframework</groupId>			<artifactId>spring</artifactId>		</exclusion>	</exclusions></dependency><dependency>	<groupId>org.apache.zookeeper</groupId>	<artifactId>zookeeper</artifactId>	<version>3.3.3</version></dependency>下面介紹一下項目1 中 的 ApplicationContent-dubbo.xml   和  ApplicationContent.xmlApplicationContent.xml  主要是spring的配置不多說

<?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:aop="http://www.springframework.org/schema/aop" 	xmlns:context="http://www.springframework.org/schema/context"	xmlns:tx="http://www.springframework.org/schema/tx"	xsi:schemaLocation="http://www.springframework.org/schema/beans 						http://www.springframework.org/schema/beans/spring-beans.xsd						http://www.springframework.org/schema/aop 						http://www.springframework.org/schema/aop/spring-aop.xsd						http://www.springframework.org/schema/context 						http://www.springframework.org/schema/context/spring-context.xsd						http://www.springframework.org/schema/tx 						http://www.springframework.org/schema/tx/spring-tx.xsd">	<!-- 啟用注解 -->	<context:annotation-config/>	<!-- 啟動組件掃描,排除@Controller組件,該組件由SpringMVC配置文件掃描 -->	<context:component-scan base-package="com.cl.user.serviceImpl"/>			<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       	<property name="dataSource" ref="dataSource"></property> 	</bean>		<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  		<property name="locations">  			<list>                   <!-- value>/WEB-INF/classes/dbconfig.properties</value-->                 <value>dbconfig.properties</value>             </list>          </property>  	</bean> 		<!-- 阿里 druid數據庫連接池 -->	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">           <!-- 數據庫基本信息配置 -->         <property name="url" value="${url}" />           <property name="username" value="${username}" />           <property name="passWord" value="${password}" />           <property name="driverClassName" value="${driverClassName}" />                    <!-- 初始化連接大小 -->		<property name="initialSize" value="${jdbc.initialSize}"></property>		<!-- 連接池最大數量 -->		<property name="maxActive" value="${jdbc.maxActive}"></property>		<!-- 連接池最大空閑 -->		<property name="maxIdle" value="${jdbc.maxIdle}"></property>		<!-- 連接池最小空閑 -->		<property name="minIdle" value="${jdbc.minIdle}"></property>		<!-- 獲取連接最大等待時間 -->		<property name="maxWait" value="${jdbc.maxWait}"></property>	</bean>  		<!-- 配置mybatis -->	<bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    	<property name="dataSource" ref="dataSource" />        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>        <!-- mapper掃描 -->        <property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property>    </bean>        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">		<constructor-arg ref="sqlSessionFactory" />	</bean>		<tx:advice id="txAdvice" transaction-manager="transactionManager">		<tx:attributes>			<tx:method name="delete*" propagation="REQUIRED" read-only="false" 			           rollback-for="Exception"/>			<tx:method name="insert*" propagation="REQUIRED" read-only="false" 			           rollback-for="Exception" />			<tx:method name="update*" propagation="REQUIRED" read-only="false" 			           rollback-for="Exception" />			<tx:method name="save*" propagation="REQUIRED" read-only="false" 			           rollback-for="Exception" />			<tx:method name="*" rollback-for="Exception"/> 		</tx:attributes>	</tx:advice>		<aop:aspectj-autoproxy proxy-target-class="true"/>	<!-- 事物處理 -->	<aop:config>		<aop:pointcut id="pc" expression="execution(* com.um.*.services..*(..))" />		<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />	</aop:config></beans>ApplicationContent-dubbo.xml   dubbo服務的配置

<?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-2.5.xsd 	http://code.alibabatech.com/schema/dubbo	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">	<!-- 提供方應用信息,用于計算依賴關系 -->	<dubbo:application name="hehe_provider" />	<!-- 使用zookeeper注冊中心暴露服務地址 -->	<dubbo:registry address="zookeeper://172.30.9.173:2181"/>	<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->	<!-- 用dubbo協議在20880端口暴露服務 -->	<dubbo:protocol name="dubbo" port="20880" />	<!-- 具體的實現bean -->	<bean id="userService" class="com.cl.user.serviceImpl.UserServiceImpl" />	<!-- 聲明需要暴露的服務接口 -->	<dubbo:service interface="com.cl.user.servicei.UserService" ref="userService" /></beans>注意:有可能你的配置文件中不識別 <dubbo:>  解決辦法(缺少dubbo.xsd,具體怎么導入到編譯器,上網百度,這里不多說了)到這里我們的  “提供者” 即 服務已經開發完了,那么如何啟動服務呢?  在項目1中有個start.java 
package com.test;import org.apache.log4j.PropertyConfigurator;public class start {	static{              PropertyConfigurator.configure("src/main/resources/log4j.properties");          }  	public static void main(String[] args) {               com.alibaba.dubbo.container.Main.main(args);    }}

注意里面有main方法,可以直接運行啟動。別急! 之前還有個問題就是為什么ApplicationContent-dubbo.xml 和  ApplicationContent.xml 這兩個配置文件 必須要放到/MATE-INF/spring/下面

因為  com.alibaba.dubbo.container.Main.main(args)  默認就會去加載 /MATE-INF/spring/ 下的配置文件

我們來看一下源碼

看完源碼 這下明白為什么了吧!!!!!!!!!!!!!!!!!

啟動服務后我們在 dubbo-admin中就能看到我們剛才開發的的  “提供者”

192.168.139.127 是本地局域網地址

這是 “提供者”  此時還沒有“消費者”   接下來我們就要開發 “消費者”

UserController.java

package com.cl.user.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.cl.user.servicei.UserService;@Controllerpublic class UserController {	@Resource(name="userService")	private UserService userService;		@RequestMapping("/hello/test/world")	public void sayHello(){		System.out.println(userService.sayHello("hello")+"**************************");	}}ApplicationContext-mvc.xml 都是springmvc的配置 如下,具體不解釋了
<?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:mvc="http://www.springframework.org/schema/mvc"	xmlns:context="http://www.springframework.org/schema/context"	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">		<mvc:annotation-driven/>	<mvc:default-servlet-handler/>	<context:component-scan base-package="com" />		<!-- 對靜態資源文件的訪問  restful-->     	<mvc:resources mapping="/js/**" location="/js/" />	<mvc:resources mapping="/lib/**" location="/lib/" />	<mvc:resources mapping="/plugins/**" location="/plugins/" />	<mvc:resources mapping="/uploadFiles/**" location="/uploadFiles/" /> 	<mvc:resources mapping="/WEB-INF/html/**" location="/WEB-INF/html/" /> 	 	<!-- 配置SpringMVC的視圖解析器 -->	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">		<property name="prefix" value="/WEB-INF/html"/>		<property name="suffix" value=".jsp"/>	</bean>		<!-- 上傳攔截,如最大上傳值及最小上傳值 -->	  <bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >   		  <property name="maxUploadSize">    	          <value>104857600</value>    	       </property>   	        <property name="maxInMemorySize">    	            <value>4096</value>    	        </property>   	         <property name="defaultEncoding">    	            <value>utf-8</value>    	        </property>     </bean>  </beans>ApplicationContext-dubbo.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-2.5.xsd	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">	<!-- 消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣 -->      <dubbo:application name="hehe_consumer" />        <!-- 使用zookeeper注冊中心暴露服務地址 -->       <dubbo:registry address="zookeeper://172.30.9.173:2181"/>         <!-- 組播注冊 -->   <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->            <!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService -->      <dubbo:reference id="userService"    interface="com.cl.user.servicei.UserService" />	</beans>開發完消費者后,啟動消費者(啟動MyTest類),可以在管控臺看到

本次教程教導這里!!Dubbo更多詳細的東西還去官網的用戶手冊尋找吧!!  http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/    用戶手冊說的挺詳細的!這里僅供參考!

源碼:http://pan.baidu.com/s/1pLg4ivx  密碼:a05p 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 栾城县| 兴城市| 南投市| 瑞昌市| 香格里拉县| 台东市| 花垣县| 奇台县| 鸡西市| 怀宁县| 德令哈市| 枞阳县| 商城县| 广河县| 广河县| 汕头市| 陈巴尔虎旗| 崇义县| 罗源县| 嫩江县| 都江堰市| 云霄县| 吉林市| 色达县| 阳谷县| 库伦旗| 东源县| 静宁县| 北票市| 武夷山市| 彭州市| 阿图什市| 康保县| 浦城县| 濮阳县| 芒康县| 巴东县| 新干县| 陆良县| 宁陵县| 吴堡县|