Dubbo-03 20190316

版权声明:本文为博主原创文章,未经博主允许不得转载,如需转载请在明显处标明出处! https://blog.csdn.net/qq_36291682/article/details/88591076

接着昨天的来 实体类和service都放到了公共包里边 在模块中引入
dubbo 和 zookeeper的jar包也引入了
service和实现类也写好了
剩下的就是跟dubbo结合了 也就是写配置文件或加上相关的注解

1. 将服务提供者注册到注册中心

1)
创建provider.xml 可以参考官方文档
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系  相同服务相同名字 不与其他服务重名-->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用zookeeper注册中心  这也是dubbo官方推荐的  2中配置都可以-->
	<!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
 
    <!-- 用dubbo协议在20880端口暴露服务 非常重要!! 消费者调用的时候就是从这个端口进行通信-->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口  暴露的服务在消费端就能使用   ref指明接口真正的实现!  -->
    <dubbo:service interface="com.test.mall.service.UserService" ref="userServiceImpl" />
 
    <!-- 服务的实现      和本地bean一样实现服务 -->
    <bean id="userServiceImpl" class="com.test.mall.service.impl.UserServiceImpl" />
</beans>

2)测试 记得呀 zookeeper一定不能停止 我今天忘记启动了 所以会报错

package com.test.mall;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "provider.xml" });
		context.start();
		System.in.read(); // 按任意键退出
	}
}

3 )去管理页面查看 已经有注册的服务了 这个管理页面就是昨天安装的dubbo-admin
在这里插入图片描述
在这里插入图片描述
192.168.56.1 就是本机地址
2. 服务消费者
1 ) 引入jar包

<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.test.mall</groupId>
  <artifactId>user-service-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  	      <!--common的pom.xml 开头就是这个配置 直接复制过来groupId   artifactId  version -->
  		  <groupId>com.test.mall</groupId>
		  <artifactId>mall-common</artifactId>
		  <version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.6.2</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
	<dependency>
	    <groupId>org.apache.curator</groupId>
	    <artifactId>curator-framework</artifactId>
	    <version>2.12.0</version>
	</dependency>
  </dependencies>
</project>

2 )配置文件 consumer.xml 这个文件名随意~~
官方配置demo: http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

<?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://dubbo.apache.org/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	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-4.3.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
    
    <!-- 为了让注解生效  需要配置扫描包 -->
    <context:component-scan base-package="com.test.mall.service.impl"></context:component-scan>
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样   不同消费者名称不能重复-->
    <dubbo:application name="user-service-consumer"  />
 
    <!-- 使用zookeeper注册中心  也是2中配置方式 这里就只写一种了 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用userService  provider暴露了userService 所以这里就可以引用userService  -->
    <dubbo:reference id="userService" interface="com.test.mall.service.UserService" />
</beans>

3 ) userService 经过上边的配置 它已经在容器中了 所以我们可以在代码中使用了

package com.test.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.mall.bean.UserAddress;
import com.test.mall.service.OrderService;
import com.test.mall.service.UserService;
/**
 *
 */
@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	UserService userService;
	 
	public void  initOrder(String userId) {
		 //查询用户收货地址
		List<UserAddress> addList = userService.getUserAddressList(userId);
		System.out.println(addList);
	}
	
	

}

4 )测试文件

package com.test.mall;

import java.io.IOException;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.mall.bean.UserAddress;
import com.test.mall.service.UserService;

public class MainApplication {
	public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();
        UserService demoService = context.getBean(UserService.class); // 获取远程服务代理
        List<UserAddress> lidt= demoService.getUserAddressList("1"); //执行远程方法
        // 显示调用结果
        for (UserAddress ua :lidt) {
			System.out.println(ua.getUserAddress());
		}
        
        //让他停止一下 可以在控制台查看一下消费者的服务
        System.in.read();
 
	}
}

5 ) 有输出结果 说明调用成功啦
在这里插入图片描述
6 ) 去dubbo-admin 点击服务者的详情按钮 暂时没有方法信息 2.7版本之后会添加方法信息
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

哎呦 监控中心还没有个搞好呢 睡了个觉 一天又快过去了
Dubbo 源码:https://github.com/apache/incubator-dubbo/

Dubbo 版本历史:https://github.com/apache/incubator-dubbo/releases

我下载的是incubator-dubbo-dubbo-2.6.0.zip (https://codeload.github.com/apache/incubator-dubbo/zip/dubbo-2.6.0 )不知道为什么高于这个版本的好像有变动 有一些问题 不是代码问题 是我搞不懂 所以用了这个旧版本
解压 maven 打包 配置 启动
解压:
在这里插入图片描述
配置文件:
D:\soft\dubbo\incubator-dubbo-dubbo-2.6.0\dubbo-simple\dubbo-monitor-simple\src\main\assembly\conf\dubbo.properties
在这里插入图片描述
maven打包:
在这里插入图片描述
在这里插入图片描述

启动:
D:\soft\dubbo\incubator-dubbo-dubbo-2.6.0\dubbo-simple\dubbo-monitor-simple\target\dubbo-monitor-simple-2.6.0-assembly.tar.gz

解压这个文件
D:\soft\dubbo\incubator-dubbo-dubbo-2.6.0\dubbo-simple\dubbo-monitor-simple\target\dubbo-monitor-simple-2.6.0-assembly\dubbo-monitor-simple-2.6.0\bin\start.bat
访问:127.0.0.1::8888 在这里插入图片描述

7 ) 要监控中心发现 还需要做一些配置
consumer项目中:
在这里插入图片描述

 <!-- 注册中心去找监控中心的地址  -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
    <!-- 直接找监控中心  -->
    <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->

在这里插入图片描述
provider项目:
在这里插入图片描述
8 ) 启动服务端 消费端 看监控中心是否有
在这里插入图片描述
上边那个是test 当时打包的时候没有过滤掉test 失误~
应该是 mvn install -Dmaven.test.skip=true

猜你喜欢

转载自blog.csdn.net/qq_36291682/article/details/88591076
03
今日推荐