标签:ext service pid 地址 webapps listener 需要 public hot
1.Apache Dubbo是一款高性能的ava RPC 框架
特性
架构原理
调用关系
1. 服务容器负责启动,加载,运行服务提供者。
2. 服务提供者在启动时,向注册中心注册自己提供的服务。
3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
快速入门
实现功能
存在两个系统,a系统和b系统,a调用b系统的接口获取数据,用于查询用户列表
安装zookeeper centos windows
解压到
https://pan.baidu.com/s/1JYY_Lx5PTnaz9rTr02K4gQ
修改配置文件zoo.cfg 该目录必须存在
启动zookeeper服务
搭建B系统
<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.ykq.dubbo</groupId>
<artifactId>dubbo-b</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<!-- 排除传递spring依赖 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置log4j文件
log4j.rootLogger=DEBUG,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
创建user对象
package com.ykq.dubbo.pojo; // 使用dubbo要求传输的对象必须实现序列化接口 public class User implements java.io.Serializable { private static final long serialVersionUID = -2668999087589887337L; private Long id; private String username; private String password; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
创建UserService(接口)提供查询服务
package com.ykq.dubbo.service; import java.util.List; import com.ykq.dubbo.pojo.User; public interface UserService { /** * 查询所有的用户数据 * * @return */ public List<User> queryAll(); }
创建UserServiceImp实现类
package com.ykq.dubbo.service.impl; import java.util.ArrayList; import java.util.List; import com.ykq.dubbo.pojo.User; import com.ykq.dubbo.service.UserService; public class UserServiceImpl implements UserService { /** * 实现查询,这里做模拟实现,不做具体的数据库查询 */ public List<User> queryAll() { List<User> list = new ArrayList<User>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setAge(10 + i); user.setId(Long.valueOf(i + 1)); user.setPassword("123456"); user.setUsername("username_" + i); list.add(user); } return list; } }
编写dubbo配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-b-server" /> <!-- 这里使用的注册中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 将该接口暴露到dubbo中 --> <dubbo:service interface="com.ykq.dubbo.service.UserService" ref="userServiceImpl" /> <!-- 将具体的实现类加入到Spring容器中 --> <bean id="userServiceImpl" class="com.ykq.dubbo.service.impl.UserServiceImpl" /> </beans>
导入zookeeper依赖
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
编写web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>dubbo-b</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo/dubbo-*.xml</param-value> </context-param> <!--Spring的ApplicationContext 载入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
启动tomcat
查看tomcat日志
2016-06-03 09:38:35,307 [localhost-startStop-1] [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry]-[INFO] [DUBBO] Register: dubbo://192.168.56.1:20880/com.ykq.dubbo.service.UserService?anyhost=true&application=dubbo-b-server&dubbo=2.5.3&interface=com.ykq.dubbo.service.UserService&methods=queryAll&pid=5436&side=provider×tamp=1464917914608, dubbo version: 2.5.3, current host: 127.0.0.1
可以看到,已将userservice服务注册到zookeeper注册中心,协议是dubbo
搭建A系统
创建完成
导入依赖
<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.ykq.dubbo</groupId>
<artifactId>dubbo-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<!-- 排除传递spring依赖 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
配置log4j文件
log4j.rootLogger=DEBUG,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
从B系统中拷贝user对象,userservice接
编写dubbo配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-a-consumer" /> <!-- 这里使用的注册中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/> <!-- 从注册中心中查找服务 --> <dubbo:reference id="userService" interface="com.ykq.dubbo.service.UserService"/> </beans>
编写userservice测试用例
package com.ykq.dubbo.service; import static org.junit.Assert.*; import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ykq.dubbo.pojo.User; public class UserServiceTest { private UserService userService; @Before public void setUp() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:dubbo/*.xml"); this.userService = applicationContext.getBean(UserService.class); } @Test public void testQueryAll() { List<User> users = this.userService.queryAll(); for (User user : users) { System.out.println(user); } } }
解决代码重复的问题
创建dubbo-b-api
将dubbo-b的pojo和service接口移动到dubbo-b-api中
在dubbo-a的pom.xml文件中添加dubbo-b-api的依赖,并且将pojo与service接口删除
监控
原理:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
搭建监控服务
https://pan.baidu.com/s/1ReV7W7bLcJI0cfwxOqu8mA
修改配置文件
修改注册中心的地址
在dubbo-b中配置监控
启动
查看界面
说明:端口在这里指定
功能
查看服务
统计
图表
管理
dubbo提供了一套在线管理整个服务的功能,管理控制台为阿里内部裁剪版本,开源部分主要包含:路由规则,动态配置,服务降级,访问控制,权重调整,负载均衡,等管理功能。
安装
将dubbo-admin-2.5.3.war(ROOT)部署到tomcat的webapps目录下:
https://pan.baidu.com/s/1ldZqr47NWz-QLgSIkpteXA
修改配置文件
启动tomcat
查看管理界面
效果:
标签:ext service pid 地址 webapps listener 需要 public hot
原文地址:https://www.cnblogs.com/sh-0131/p/11824537.html