首先我要灌输两个概念:服务生产者(用来提供接口(以及接口业务的实现))
服务消费者(用来获取到服务生产者产出的接口)
服务中心(服务提供者开启后,将自己的服务都发布在这上面)
1:我们要开启Zookeeper。Zookeeper是可以用来将服务提供者所提供的服务进行发布的程序(可以自己在网络上下载)
2:那么我们现在开始正式搭建服务提供者:
①:服务提供者的框架是SpringBoot工程(内嵌了tomcat),我们也可以自己启动tomcat。
②:在resources目录下创建proverder-context.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="dubbodemo-provider"/> <dubbo:registry id="zookeeper" address="zookeeper://172.16.42.212:2181" /> <dubbo:protocol name="dubbo" port="28080" /> <!--这里的接口路径,是自己新生成的普通工程在打包后下倒入该工程后生成的路径(往下会说,先这么配置)--> <dubbo:service interface="com.MoneyDubbo" ref="MoneyDubbo" /> <bean id="MoneyDubbo" class="com.example.service.ServiceImpl.MoneyDubboImpl" /> </beans>
③:首先声明下我用的是idea编译器并且是基于gradle构建的项目
所以我的build.gradle里面是这样配置的:
buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { maven{ url"http://172.16.60.188:8081/nexus/content/groups/public" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { maven{ url"http://172.16.60.188:8081/nexus/content/groups/public" } } dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') compile('com.alibaba:dubbo:2.5.3'){ exclude group: 'org.springframework' } compile('com.101tec:zkclient:0.4'){ exclude group:'org.slf4j' exclude group :'log4j' } compile('org.apache.zookeeper:zookeeper:3.4.6'){ exclude group:'org.slf4j' exclude group:'log4j' } }
④:我创建的接口名称是MoneyDubbo,并且在服务生产者这里我写好了里面的实现
以上是服务生产者的主要配置流程
3:现在我说下如何打成jar包的:
①:我们都听说过通过jar包向别人提供方法支持。那么jar包的生成是怎么的呢?
②:我通过新创建一个普通java工程(基于gradle的)。然后我在新生成的工程下创建自定义包。
并且在该包下面我创建了与我服务生产者里面的接口名一模一样的类(当然也是接口的)。
/*这样做的目地是:我是要将该生成jar包放回我的服务生产者和服务消费者中。类似于一个中间和两端都要适配的中 间件*/
③:在idea的右边框上找到gradle后弹出自己工程的目录管理结构。如图在Tasks下build下面jar上双击即可得到jar包。
4:现在我们开始搭建服务消费者:
①:服务消费者创建customer.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="demo-consumer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry id="zookeeper" address="zookeeper://172.16.42.212:2181" /> <dubbo:protocol name="dubbo" port="28080"/> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="moneyDubbo" interface="com.MoneyDubbo" /> </beans>
②:build.gradle中写入:
buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { maven{ url"http://172.16.60.188:8081/nexus/content/groups/public" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { maven{ url"http://172.16.60.188:8081/nexus/content/groups/public" } } dependencies { compile ('com.alibaba:dubbo:2.5.3'){ exclude group:'org.springframework' } compile ('com.101tec:zkclient:0.4'){ exclude group: 'org.slf4j' exclude group: 'log4j' } compile ('org.apache.zookeeper:zookeeper:3.4.6'){ exclude group: 'org.slf4j' exclude group: 'log4j' } compile 'com.lemon:sxf-demo-dubbo:1.0-dev' compile('org.springframework.boot:spring-boot-starter-web') compile('org.apache.tomcat.embed:tomcat-embed-jasper') testCompile('org.springframework.boot:spring-boot-starter-test') }
③:在自己生成的主方法中生成该文件项目:
@ImportResource("classpath:dubbo-demo-consumer.xml") @SpringBootApplication public class DemoApplication extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{ public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(DemoApplication.class); springApplication.run(args); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-demo-consumer.xml"); MoneyDubbo dubbo = (MoneyDubbo) context.getBean("moneyDubbo"); System.out.println("测试结果"+dubbo.getMoney()); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8081); } }
****************最后的最后要将我们自己生成的jar包放入到服务生产者和服务消费者上。*******
着步骤的最为关键:因为这个jar包就相当于中间件。我们服务生产者引用实现类时候都要使用该jar包的引用!!!
我们服务销费者的引用也要是调用该jar包!!!
最后我们在将服务生产者先启动后,服务消费者会在启动后会获取到服务生产者所写的实现类!