栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

dubbo学习

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;


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 {
	
	
	public List initOrder(String userId);

}

UserService.java:
 

package com.atguigu.gmall.service;

import java.util.List;

import com.atguigu.gmall.bean.UserAddress;


public interface UserService {
	
	
	public List 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 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:




	
	
	
	
	
	
	
	
	
	
	
	
		
	 
	
	 

    
	
    
	

 pom.xml:



    4.0.0
    
        1.8
        1.8
    
    com.service.provider
    my-service-provider-dubbo
    1.0-SNAPSHOT
    
        
        
        
            com.alibaba
            dubbo
            2.6.2
        
        
        
            org.apache.curator
            curator-framework
            2.12.0
        
    

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

consumer.xml:



	
		
		
		
	
	
	
	
	
	
		
	
		
	
	
	
    
	
    
	

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:



    4.0.0
    
        1.8
        1.8
    
    com.order.consumer
    order-service-consumer
    1.0-SNAPSHOT
    
        
        
        
            com.alibaba
            dubbo
            2.6.2
        
        
        
            org.apache.curator
            curator-framework
            2.12.0
        
     

 先启动提供者,在启动消费者,在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 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:



	4.0.0
	com.atguigu
	boot-user-service-provider
	0.0.1-SNAPSHOT
	jar
	boot-user-service-provider
	Demo project for Spring Boot
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	
	
		UTF-8
		UTF-8
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter
		
		
		
			com.alibaba.boot
			dubbo-spring-boot-starter
			0.2.0
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.cloud
			
				spring-cloud-starter-netflix-hystrix
			
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.SR1
				pom
				import
			
		
	

 基于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 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;


@Service
public class OrderServiceImpl implements OrderService {
	//@Autowired
	@Reference  //替代之前xml里的 dubbo:reference
	UserService userService;	
	@HystrixCommand(fallbackMethod="hello")
	@Override
	public List initOrder(String userId) {
		// TODO Auto-generated method stub
		System.out.println("用户id:"+userId);
		//1、查询用户的收货地址
		List 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);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号