1. 程式人生 > >在同一SpringBoot專案中啟動Dubbo服務端和客戶端

在同一SpringBoot專案中啟動Dubbo服務端和客戶端

Dubbo是微服務框架,服務需要部署到不同JVM虛擬機器,由此代理的問題是需要提供一個統一Jar包(包含Dubbo介面和JVM間傳遞的實體類),Dubbo伺服器端與客戶端需要共享這個Jar包,在專案開發中這是最為通用的方法,
在這裡插入圖片描述
但在Dubbo案例中如此實現,需要定義三個Maven工程,工作量很大,如下方案實現類在一個SpringBoot專案中啟動Dubbo服務端和Dubbo客戶端,避免共享Jar帶來的問題
在這裡插入圖片描述
實現思路如下:
提供兩個SpringBoot啟動類,一個啟動Dubbo服務,另一個啟動Dubbo客戶端,啟動Dubbo服務的類,使用程式碼修改server.port埠,並且自動掃描包路徑中,不掃描Controller所在包;Dubbo客戶端啟動類,使用配置檔案application.properties中定義的埠,並且SpringBoot啟動時排除RabbitMQ自動配置,包掃描路徑只配置Controller所在包。

啟動Dubbo伺服器啟動類

package com.test;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;


@SpringBootApplication
@EnableDubboConfiguration
//只掃描工具類所在包和服務實現類所在包,排除Controller所在包的掃描
@ComponentScan("com.test.util,com.test.service.impl")
public class DubboServer extends SpringBootServletInitializer
{
    public static void main( String[] args )
    {
    	//使用程式碼修改server.port埠
    	System.setProperty("server.port","6070");
        SpringApplication.run(DubboServer.class,args);
    }
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

啟動Dubbo客戶端啟動類

在這裡插入圖片描述

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.test.util.RabbitmqListener;

import org.springframework.context.annotation.FilterType;

//SpringBoot啟動排除RabbitMQ自動配置
@SpringBootApplication(exclude={RabbitAutoConfiguration.class})
@EnableDubboConfiguration
//只掃描Controller所在包
@ComponentScan("com.test.ctrl")
//@ComponentScan(basePackages={"com.test.util"},excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION, value=Component.class)})
public class Starter 
{
    public static void main( String[] args )
    {
        SpringApplication.run(Starter.class,args);
    }
}

Dubbo服務實現類
在這裡插入圖片描述

ServiceImpl.java

package com.test.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.mapper.OrderMapper;
import com.test.model.AddressInfo;
import com.test.model.OrderInfo;
import com.test.model.OrderItemInfo;
import com.test.service.IDaoService;
import com.test.service.IService;
import com.test.util.IMsgProcess;
import com.test.util.SendMsg;

@com.alibaba.dubbo.config.annotation.Service(interfaceClass=IService.class)
@Service
public class ServiceImpl implements IService,IMsgProcess{
	@Autowired
	private IDaoService dao;
	@Autowired
	private SendMsg sendmsg;
	
	public Map getOrder(Integer page,Integer rows)
	{
		PageHelper.startPage(page,rows);
		List<OrderInfo> list = dao.findOrder();
		PageInfo pi = new PageInfo(list);
		Long total = pi.getTotal();
		List<OrderInfo> orders = pi.getList();
		Map m = new HashMap();
		m.put("total", total);
		m.put("orders", orders);
		
		return m;
	}
	
	/**
	 * 訂單Controller遠端呼叫的方法
	 * @param oi 包含訂單主表資訊和明細表資訊
	 * @return
	 */
	@Override
	public boolean saveOrder(OrderInfo oi) {
		System.out.println("ServiceImpl.saveOrder oi="+oi);
		List<OrderItemInfo> items = oi.getItems();
		System.out.println("ServiceImpl.saveOrder items="+items);
		//訂單主表資料
		dao.saveOrder(oi);
		for(OrderItemInfo item:items)
		{
			item.setOrder_id(oi.getOrder_id());
			Integer price = item.getItem_price();
			Integer num = item.getItem_num();
			item.setTotal_fee(price*num);
			//訂單明細表資料
			dao.saveOrderItem(item);
		}
		
		//傳送訊息
		oi.setItems(items);
		sendmsg.sendObject(oi, 1);
		return true;
	}

	@Override
	public boolean updateStore(OrderInfo oi) {
		System.out.println("ServiceImpl.updateStore oi="+oi);
		List<OrderItemInfo> items = oi.getItems();
		for(OrderItemInfo item:items)
		{
			System.out.println("ServiceImpl.updateStore item="+item);
			dao.updateStore(item);
		}
		return false;
	}

	@Override
	public Boolean process(Object obj) {
		if(obj instanceof OrderInfo)
		{
			OrderInfo oi = (OrderInfo)obj;
			updateStore(oi);
		}
		return true;
	}

	@Override
	public List<AddressInfo> findProvince() {
		return dao.findProvince();
	}

	@Override
	public List<AddressInfo> findCityByPrvnc(String code) {
		return dao.findCityByPrvnc(code);
	}

	@Override
	public List<AddressInfo> findCountyByCity(String cityCode) {
		return dao.findCountyByCity(cityCode);
	}

}

Dubbo客戶端Controller
在這裡插入圖片描述

程式碼:https://pan.baidu.com/s/1xkXvePQ5Z_SgFny02vFwDA