dubbo学习

1.zookeeper下载安装
到官网下载,我下的3.4.11版本
下载地址:链接:https://pan.baidu.com/s/1zJEi_qJJMD86Bs-vAqtIFw  提取码:aj1n
1.1 如下图


1.2修改配置文件

1.3在zookeeper的bin目录下启动cmd执行命令zkServer.cmd启动zookeeper.
报错JAVA_HOME is not set 和 JAVA_HOME is incorrectly set.
直接把jdk写死如下:

2.到dubbo官网下载dubbo-admin控制台。用maven方式运行
下载地址:链接:https://pan.baidu.com/s/17yy_gE_qfD5ew4JUDlu7zw  提取码:gzk8

然后 在dubbo-admin目录下启动cmd, 执行命令mvn clean package打包,打好包后会在target目录下,在当前目录(也可以把包复制到别的目录)执行java -jar dubbo-admin-0.0.1-SNAPSHOT.jar


端口号为7001, 在浏览器上打输入 http://localhost:7001/ 帐号和密码 都是root
以下是代码:
公共接口在一个项目中单独的:

UserAddress.java类:

package com.atguigu.gmall.bean;

import java.io.Serializable;

/**
 * 用户地址
 * @author lfy
 *
 */
public class UserAddress implements Serializable {
	
	private Integer id;
    private String userAddress; //用户地址
    private String userId; //用户id
    private String consignee; //收货人
    private String phoneNum; //电话号码
    private String isDefault; //是否为默认地址    Y-是     N-否
    
    public UserAddress() {
		super();
		// TODO Auto-generated constructor stub
	}
    
	public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
			String isDefault) {
		super();
		this.id = id;
		this.userAddress = userAddress;
		this.userId = userId;
		this.consignee = consignee;
		this.phoneNum = phoneNum;
		this.isDefault = isDefault;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserAddress() {
		return userAddress;
	}
	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getConsignee() {
		return consignee;
	}
	public void setConsignee(String consignee) {
		this.consignee = consignee;
	}
	public String getPhoneNum() {
		return phoneNum;
	}
	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}
	public String getIsDefault() {
		return isDefault;
	}
	public void setIsDefault(String isDefault) {
		this.isDefault = isDefault;
	}
}

OrderService.java:

package com.atguigu.gmall.service;

import java.util.List;

import com.atguigu.gmall.bean.UserAddress;

public interface OrderService {
	
	/**
	 * 初始化订单
	 * @param userId
	 */
	public List<UserAddress> initOrder(String userId);

}

UserService.java:

package com.atguigu.gmall.service;

import java.util.List;

import com.atguigu.gmall.bean.UserAddress;

/**
 * 用户服务
 * @author lfy
 *
 */
public interface UserService {
	
	/**
	 * 按照用户id返回所有的收货地址
	 * @param userId
	 * @return
	 */
	public List<UserAddress> getUserAddressList(String userId);

}

提供者代码示例(就是提供服务的生产者):

UserServiceImpl.java:

package com.my.service.impl;

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;

import java.util.Arrays;
import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        System.out.println("UserServiceImpl.....old...");
        UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");

        return Arrays.asList(address1,address2);
    }

}

provider.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://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">

	<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
	<dubbo:application name="user-service-provider"></dubbo:application>
	
	<!-- 2、指定注册中心的位置 -->
	<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
	
	<!-- 3、指定通信规则(通信协议?通信端口) -->
	<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
	
	<!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
	<dubbo:service interface="com.atguigu.gmall.service.UserService" 
		ref="UserServiceImpl" timeout="1000" version="1.0.0">
		<!--<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>-->
	</dubbo:service> 
	<!-- 服务的实现 -->
	<bean id="UserServiceImpl" class="com.my.service.impl.UserServiceImpl"></bean> 

    <!-- 从注册中心发现监控中心地址,否则直边监控中心 -->
	<dubbo:monitor protocol="registry"></dubbo:monitor>
    <!--直接监控中心服务器地址-->
	<!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
</beans>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <groupId>com.service.provider</groupId>
    <artifactId>my-service-provider-dubbo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- 引入dubbo -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

MainApplication .java:

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class MainApplication {	
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ioc = new 
    ClassPathXmlApplicationContext("provider.xml");
		 ioc.start();
		System.in.read();
	}
}

注意:先启动服务者,把服务者注册到注册中心,才启动消费者
消费者示例代码:

OrderServiceImpl.java:

package com.ou.service.impl;

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import com.atguigu.gmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) {
        System.out.println("用户id:"+userId);
        //1、查询用户的收货地址
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }
        return addressList;
    }
}

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://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.ou.service.impl"></context:component-scan>
	<dubbo:application name="order-service-consumer"></dubbo:application>	
	<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>	
	<!--  配置本地存根-->	
	<!--声明需要调用的远程服务的接口;生成远程服务代理  -->
	<!-- 
		1)、精确优先 (方法级优先,接口级次之,全局配置再次之)
		2)、消费者设置优先(如果级别一样,则消费方优先,提供方次之)
	-->
	<!-- timeout="0" 默认是1000ms-->
	<!-- retries="":重试次数,不包含第一次调用,0代表不重试-->
	<!-- 幂等(设置重试次数)【查询、删除、修改】、非幂等(不能设置重试次数)【新增】 -->
	<dubbo:reference interface="com.atguigu.gmall.service.UserService"
		id="userService" timeout="5000" retries="3" version="*">
		<!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> -->
	</dubbo:reference>
		
	<!-- 配置当前消费者的统一规则:所有的服务都不检查 -->
	<dubbo:consumer check="false" timeout="5000"></dubbo:consumer>
	
    <!-- 从注册中心发现监控中心地址,否则直边监控中心 -->
	<dubbo:monitor protocol="registry"></dubbo:monitor>
    <!--直接监控中心服务器地址-->
	<!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
</beans>
MainApplication.java:
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.atguigu.gmall.service.OrderService;
public class MainApplication {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");		
		OrderService orderService = applicationContext.getBean(OrderService.class);		
		orderService.initOrder("1");
		System.out.println("调用完成....");
		System.in.read();
	}
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <groupId>com.order.consumer</groupId>
    <artifactId>order-service-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- 引入dubbo -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies> 
</project>

先启动提供者,在启动消费者,在dubbo的控制台可以看到:

接下来启动监控中心:
之前在官网下载的dubbo-admin里有一个dubbo-monitor-simple文件夹,在这个目录里执行命令mvn clean package打包命令,然后将打好的jar包变成压缩包,在把压缩包解压,把里面的conf/dubbo.properties里的dubbo.registry.address和dubbo.protocol.port都改成自己的,然后 执行assembly.bin文件夹里的start启动

基于Spring boot的服务提供者代码:
application.prperties:

dubbo.application.name=user-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#指定通信规则(通信协议,通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

dubbo.monitor.protocol=registry
UserServiceImpl.java:
package com.atguigu.gmall.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;

@Service//暴露服务 是dubbo的api,由原来在xml里写的dubbo:service现在只要一个注解
@Component
public class UserServiceImpl implements UserService {

	//@HystrixCommand
	@Override
	public List<UserAddress> getUserAddressList(String userId) {
		// TODO Auto-generated method stub
		System.out.println("UserServiceImpl..3.....");
		UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
		UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
		if(Math.random()>0.5) {
			throw new RuntimeException();
		}
		return Arrays.asList(address1,address2);
	}

}

application.java运行类:

@EnableDubbo(scanBasePackages="com.atguigu.gmall")
@SpringBootApplication
public class BootUserServiceProviderApplication {
	public static void main(String[] args) {
		SpringApplication.run(BootUserServiceProviderApplication.class, args);
	}
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.atguigu</groupId>
	<artifactId>boot-user-service-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>boot-user-service-provider</name>
	<description>Demo project for Spring Boot</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- dubbo和springboot整合包 -->
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>
				spring-cloud-starter-netflix-hystrix
			</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

基于Springboot的消费者:
application.properties:

server.port=8081

dubbo.application.name=boot-order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
OrderController.java类:
package com.atguigu.gmall.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;

@Controller
public class OrderController {	
	@Autowired
	OrderService orderService;
	
	@ResponseBody
	@RequestMapping("/initOrder")
	public List<UserAddress> initOrder(@RequestParam("uid")String userId) {
		return orderService.initOrder(userId);
	}
}
OrderServiceImpl.java:
package com.atguigu.gmall.service.impl;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import com.atguigu.gmall.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * 1、将服务提供者注册到注册中心(暴露服务)
 * 		1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator)
 * 		2)、配置服务提供者 * 
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址  *
 */
@Service
public class OrderServiceImpl implements OrderService {
	//@Autowired
	@Reference  //替代之前xml里的 dubbo:reference
	UserService userService;	
	@HystrixCommand(fallbackMethod="hello")
	@Override
	public List<UserAddress> initOrder(String userId) {
		// TODO Auto-generated method stub
		System.out.println("用户id:"+userId);
		//1、查询用户的收货地址
		List<UserAddress> addressList = userService.getUserAddressList(userId);
		return addressList;
	}
}

pom.xml和提供者是一样的。
appcation.java类:

package com.atguigu.gmall;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@EnableDubbo
//@EnableHystrix
@SpringBootApplication
public class BootOrderServiceConsumerApplication {
	public static void main(String[] args) {
		SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
	}
}


版权声明:本文为weixin_43075758原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。