1.先啟動我上篇dubbo的提供者
pom.xml ,引入我啟動的提供者的包
<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>com.guilf</groupId> <artifactId>com-guilf-comsumers-service</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.9</log4j.version> </properties> <dependencies> <dependency> <groupId>com.guilf</groupId> <artifactId>com.guilf1.api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.guilf</groupId> <artifactId>com.guilf.api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.guilf1.service.impl.TestMain</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
dubbo-provide.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_provider1"/> <!-- 下面的host主要是放到虛擬機中的話,會因為ip不同報錯 --> <!-- <dubbo:protocol name="dubbo" port="20881" host="192.168.88.134"/> --> <dubbo:protocol name="dubbo" port="20881"/> <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://192.168.88.134:2181?backup=192.168.88.134:2182,192.168.88.134:2183" check="false"/> <!-- 要暴露的服務接口 --> <dubbo:service interface="com.guilf1.service.api.GuilfConsumerServiceApi" ref="guilfConsumerServiceApi"/> <!--具體實現該接口的 bean--> <bean id="guilfConsumerServiceApi" class="com.guilf1.service.impl.GuilfConsumerServiceImpl"/> </beans>
dubbo-consumer.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: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_consumer"/>--> <!-- registry address, used for consumer to discover services <dubbo:registry address="zookeeper://192.168.88.130:2181?backup=192.168.88.130:2182,192.168.88.130:2183" check="false"/> --> <!-- 用dubbo協議在20881端口服務 <dubbo:protocol name="dubbo" port="20880" />--> <!-- which service to consume? dubbo上面的提供者--> <dubbo:reference id="guilfDubboServiceApi" interface="com.guilf.service.api.GuilfDubboServiceApi" check="false"/> </beans>
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd"> <!-- 引入服務提供者配置文件 --> <import resource="dubbo-provider.xml" /> <!-- 掃描文件(自動將servicec層注入) --> <context:component-scan base-package="com.guilf.service.*,com.guilf1.service.*"/> <!-- 引入服務提供者配置文件 --> <import resource="dubbo-consumer.xml" /> </beans>
GuilfConsumerServiceImpl.java 這里有調用dubbo上面的提供者
package com.guilf1.service.impl; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.dubbo.config.annotation.Service; import com.guilf1.service.api.GuilfConsumerServiceApi; import com.guilf.service.api.GuilfDubboServiceApi; public class GuilfConsumerServiceImpl implements GuilfConsumerServiceApi {
// 調用dubbo上面的提供者 @Autowired private GuilfDubboServiceApi guilfDubboServiceApi; public String findConsumerUser(String name, String password) { return "======nameConsumer:"+name+"=====password:"+password; } public String findProviderUser() { String str = guilfDubboServiceApi.findUser("guilf", "root"); return "======nameConsumer:=====password:"+str; } }
GuilfConsumerServiceApi.java
package com.guilf1.service.api; public interface GuilfConsumerServiceApi { public String findConsumerUser(String name, String password); public String findProviderUser(); }
TestMain.java
package com.guilf1.service.impl; import java.io.IOException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.guilf1.service.api.GuilfConsumerServiceApi; import com.guilf.service.api.GuilfDubboServiceApi; public class TestMain { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"classpath:conf/spring.xml"}); GuilfDubboServiceApi guilfDubboServiceApi =(GuilfDubboServiceApi)context.getBean("guilfDubboServiceApi"); String str = guilfDubboServiceApi.findUser("wo", "ini"); GuilfConsumerServiceApi guilfConsumerServiceApi = (GuilfConsumerServiceApi)context.getBean("guilfConsumerServiceApi"); String str1 = guilfConsumerServiceApi.findProviderUser(); System.out.println(str); System.out.println(str1); context.start(); // ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext( // new String[]{"classpath:conf/dubbo-provider.xml"}); // context1.start(); System.out.println("Press any key to exit111."); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
啟動。里面及提供了自己的接口,也消費了上篇的提供者