Zookeeper注册中心的搭建
- 下载zookeeper压缩包并解压,官网地址:http://www.apache.org/dyn/closer.cgi/zookeeper/
- 进入conf目录将 zoo_sample.cfg 改名为 zoo.cfg。
- 进入bin目录双击zkServer.cmd,若启动成功,则windows单机版zookeeper搭建成功!
开始搭建
1. 服务提供方和消费方都需要的包
pom.xml中配置共同需要使用的jar包。
2. 在demo-api中定义服务接口(注意服务提供方和消费方都需要依赖这个项目)
3. 服务提供方实现
其中数据库的配置
- 实现接口
package com.test;
import org.springframework.stereotype.Service;
import com.test.DemoService;
@Service("demoService")
public class DemoServiceImpl implements DemoService{
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
return name;
}
}
- 声明暴露服务:
<?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="dubbo_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.test.DemoService" ref="demoService" />
</beans>
- 在springmvc.xml中扫描service注解并将dubbo-provider.xml中的相关的dubbo配置引入进来
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"
default-autowire="byName">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.test" />
<import resource="classpath:dubbo-provider.xml" /></beans>
- 加载Spring配置,启动服务:
package com.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
context.start();
System.out.println("Dubbo provider start...");
try {
System.in.read(); // 按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 服务消费者实现
- 项目结构
- 在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:application name="dubbo_consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.test.DemoService" />
</beans>
- 在springmvc.xml中扫描service注解并将dubbo-consumer.xml中的相关的dubbo配置引入进来
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"
default-autowire="byName">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.test" />
<import resource="classpath:/dubbo-consumer.xml" /></beans>
- 加载Spring配置,调用服务:
package com.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println(demoService.sayHello("哈哈哈"));
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本篇本章最初借鉴https://blog.csdn.net/jingyangV587/article/details/78901937