原创

Spring Boot (十四)集成Dubbo


项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot (十四)集成Dubbo,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.1.1


Spring Boot Dubbo 客户端测试 项目地址:

https://github.com/FrameReserve/TrainingSpringBootDubboConsumer


pom.xml

<!-- Dubbo -->
        <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>



修改:Spring Boot 配置文件,增加zookeeper地址

/src/main/resources/application.yml

#Dubbo
#ZooKeeper
dubbo:
    registry:
        address: 192.168.2.72:2181
            


增加:Dubbo配置文件,src/main/resources/dubbo.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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="compute-service"  />

    <!-- 注册中心服务地址 -->
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

    <!-- 用dubbo协议在30001 -->
    <dubbo:protocol name="dubbo" port="30001" dispather="all" threadpool="cached" threads="5000"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.training.dubbo.service.DubboDemoService" ref="dubboDemoService"
                   version="1.0" registry="zookeeper" owner="shp"/>

    <!-- 具体服务接口的实现 -->
    <bean id="dubboDemoService" class="com.training.dubbo.service.impl.DubboDemoServiceImpl" />

</beans>


Spring Boot启动类,引入dubbo.xml

package com.training;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
@ImportResource({"classpath:dubbo.xml"})
public class SpringBootServlet extends SpringBootServletInitializer {

	// jar启动
	public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(SpringBootServlet.class, args);
    }

	// tomcat war启动
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootServlet.class);
    }

}



增加Dubbo,生产者接口案例。


接口:/src/main/java/com/training/dubbo/service/DubboDemoService.java

package com.training.dubbo.service;

import java.util.List;

import com.training.dubbo.dto.DubboDemoDto;

public interface DubboDemoService {

	public DubboDemoDto getDubboDemoDto();
	
	public List<DubboDemoDto> findAllDubboDemoDto();
	
}


实现:/src/main/java/com/training/dubbo/service/impl/DubboDemoServiceImpl.java

package com.training.dubbo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.training.dubbo.dto.DubboDemoDto;
import com.training.dubbo.service.DubboDemoService;

public class DubboDemoServiceImpl implements DubboDemoService {

	@Override
	public DubboDemoDto getDubboDemoDto() {
		return new DubboDemoDto(1, "张三");
	}

	@Override
	public List<DubboDemoDto> findAllDubboDemoDto() {
		List<DubboDemoDto> list = new ArrayList<DubboDemoDto>();
		list.add(new DubboDemoDto(1, "张三"));
		list.add(new DubboDemoDto(2, "李四"));
		return list;
	}

}


业务传输对象:/src/main/java/com/training/dubbo/dto/DubboDemoDto.java

注意:必须实现序列化 implements Serializable,否则传输过程中,序列化错误。

package com.training.dubbo.dto;

import java.io.Serializable;

public class DubboDemoDto implements Serializable {

	public DubboDemoDto() {
		super();
	}

	public DubboDemoDto(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	private Integer id;
	
	private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}















正文到此结束
Loading...