dubbo-service公共Service
package com.itman.service; public interface UserService { // 提供服务 使用userId查找用户信息FS public String getUserId(Integer id); }
dubbo-producer生产者
引入依赖
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.6</version> </dependency> <!-- 添加zk客户端依赖 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.itman</groupId> <artifactId>dubbo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
实现Service
package com.itman.service.impl; import com.itman.service.UserService; public class UserServiceImpl implements UserService { public String getUserId(Integer id) { System.out.println("被客户端(消费者)消费....id:" + id); if (id == 1) { return "itman"; } if (id == 2) { return "itman2"; } if (id == 1) { return "itman3"; } return "未找到..."; } }
发布服务
<?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="provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://169.254.197.135:2181" /> <!-- 用dubbo协议在29014端口暴露服务 --> <dubbo:protocol name="dubbo" port="29014" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.itman.service.UserService" ref="userService" /> <!-- 具体的实现bean --> <bean id="userService" class="com.itman.service.impl.UserServiceImpl" /> </beans>
启动代码
package com.itman; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMember { public static void main(String[] args) throws IOException { // 发布服务 ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("provider.xml"); app.start();// 加载 System.out.println("服务发布成功..."); System.in.read(); // 让程序阻塞 } }
dubbo-consumer消费者
消费类
package com.itman; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.itman.service.UserService; public class TestConsumer { public static void main(String[] args) { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("consumer.xml"); UserService userService = (UserService) app.getBean("userService"); String name = userService.getUserId(1); System.out.println("name:" + 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="consumer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://169.254.197.135:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="userService" interface="com.itman.service.UserService" /> </beans>
Dubbo-admin管理平台搭建
1.将dubbo-admin.zip 解压到webapps目录下
2.修改dubbo.properties zk注册中心连接地址连接信息
3.启动tomcat即可
Dubbo集群、负载均衡、容错
修改:provider.xml 端口号 <dubbo:protocol name="dubbo" port="29015" />
启动两个服务,配置相关权重,负载均衡策略