生活随笔
收集整理的這篇文章主要介紹了
Spring基础环境搭建
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建一個父工程springdubbo,3個子工程分別為服務(wù)端provider,客戶端consumer
,接口api
選擇都選擇maven quickstart即可
在main目錄下面創(chuàng)建resources文件夾,并且讓idea識別,選擇中右擊:
給父工程添加依賴,這樣provider和consumer最為子項目都可以依賴,由于pom文件中的依賴子項目都需要,因此,吧依賴都添加到父工程的pom文件中。
<dependencies><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.9</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.9</version><type>pom</type></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version></dependency><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.6.Final</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.21.0-GA</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- spring相關(guān)jar --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>
服務(wù)端provider配置如下:
創(chuàng)建配置文件
applicationContext-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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 提供方應(yīng)用信息,用于計算依賴關(guān)系 --><dubbo:application name="provider"/><!--使用zookeeper進(jìn)行注冊中心化--><dubbo:registry address="zookeeper://localhost:2181"/><!-- 用dubbo協(xié)議在20880端口暴露服務(wù) --><dubbo:protocol name="dubbo" port="20880"/><!-- 和本地bean一樣實現(xiàn)服務(wù) --><bean id="serviceAPI" class="com.gblfy.dubbo.api.impl.ServiceAPIImpl"/><!-- 聲明需要暴露的服務(wù)接口 --><dubbo:serviceinterface="com.gblfy.dubbo.api.ServiceAPI"ref="serviceAPI"/>
</beans>
創(chuàng)建一個接口實現(xiàn)類
public class ServiceAPIImpl implements ServiceAPI {@Overridepublic String sendMessage(String message) {return "quickstart-provider-message="+message;}
}
創(chuàng)建一個客戶端
public class ProvierClient {/*** 1.讀取配置文件* 2.創(chuàng)建ioc容器* 3.堵塞(狀態(tài)停留,輸入停止,不輸入就狀態(tài)一直保留)** @param args*/public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-provider.xml");context.start();try {System.in.read(); // 按任意鍵退出} catch (IOException e) {e.printStackTrace();}}
}
依賴
<dependency><groupId>com.gblfy.dubbo</groupId><artifactId>api</artifactId><version>1.0-SNAPSHOT</version></dependency>
客戶端配置:
applicationContext-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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 消費方應(yīng)用名,用于計算依賴關(guān)系,不是匹配條件,不要與提供方一樣 --><dubbo:application name="consumer-of-helloworld-app" /><!-- 用zookeeper進(jìn)行注冊中心化 --><dubbo:registry address="zookeeper://localhost:2181"/><!-- 生成遠(yuǎn)程服務(wù)代理,可以和本地bean一樣使用demoService --><dubbo:referenceid="consumerService"interface="com.gblfy.dubbo.api.ServiceAPI"/></beans>
客戶端
public class ConsumerClient {public static void main(String[] args) {ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext-consumer.xml");context.start();while (true){Scanner scanner = new Scanner(System.in);String message = scanner.next();//獲取接口//從spring容器中,把實現(xiàn)類讀出來 bean idServiceAPI serviceAPI = (ServiceAPI) context.getBean("consumerService");//message 是上面的//客戶端發(fā)送消息--->>>服務(wù)端接收消息--->>>處理后,將結(jié)果返回--->>>客戶端System.out.println(serviceAPI.sendMessage(message));}}
}
依賴
<dependency><groupId>com.gblfy.dubbo</groupId><artifactId>api</artifactId><version>1.0-SNAPSHOT</version></dependency>
啟動zookeeper、服務(wù)端,客戶端即可測試
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的Spring基础环境搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。