[置顶] spring boot dubbo 简单例子

时间:2022-03-30 19:41:45

spring boot dubbo demo

首先spring boot简化了spring的配置,方便使用,写一下spring boot dubbo 的整合的文章。

先下载zookeeper,然后解压就行。在这个目录,双击zkServer.cmd就行。linux的用命令行./zkServer.sh start
[置顶]        spring boot dubbo 简单例子
上代码前,介绍一下dubbo
DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案。可以实现系统之间的调用,可以进行
下面开始上代码,项目结构如图:
[置顶]        spring boot dubbo 简单例子
city.java 实体

package org.spring.springboot.domain;

import java.io.Serializable;

/**
* 城市实体类
*
* Created by bysocket on 07/02/2017.
*/

public class City implements Serializable {

private static final long serialVersionUID = -1L;

/**
* 城市编号
*/

private Long id;

/**
* 省份编号
*/

private Long provinceId;

/**
* 城市名称
*/

private String cityName;

/**
* 描述
*/

private String description;

public Long getId() {
return id;
}

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

public Long getProvinceId() {
return provinceId;
}

public void setProvinceId(Long provinceId) {
this.provinceId = provinceId;
}

public String getCityName() {
return cityName;
}

public void setCityName(String cityName) {
this.cityName = cityName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "City{" +
"id=" + id +
", provinceId=" + provinceId +
", cityName='" + cityName + '\'' +
", description='" + description + '\'' +
'}';
}
}

dubbo 消费者

package org.spring.springboot.dubbo;

import com.alibaba.dubbo.config.annotation.Reference;
import org.spring.springboot.domain.City;
import org.springframework.stereotype.Component;

/**
* 城市 Dubbo 服务消费者
*
* Created by bysocket on 28/02/2017.
*/

@Component
public class CityDubboConsumerService {

@Reference(version = "1.0.0")
private CityDubboService cityDubboService;

public void printCity() {
String cityName="温岭";
City city = cityDubboService.findCityByName(cityName);
System.out.println(city.toString());
}
}

dubbo server 接口

package org.spring.springboot.dubbo;

import org.spring.springboot.domain.City;

/**
* 城市业务 Dubbo 服务层
*
* Created by bysocket on 28/02/2017.
*/

public interface CityDubboService {

/**
* 根据城市名称,查询城市信息
* @param cityName
*/

City findCityByName(String cityName);
}

spring boot 启动

package org.spring.springboot;

import org.spring.springboot.dubbo.CityDubboConsumerService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
* Spring Boot 应用启动类
*
* Created by bysocket on 16/4/26.
*/

// Spring Boot 应用的标识
@SpringBootApplication
public class ClientApplication {

public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class);
cityService.printCity();
}
}

application.properties

## 避免和 server 工程端口冲突
server.port=8081

## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=org.spring.springboot.dubbo

dubbo server :

package org.spring.springboot.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.spring.springboot.domain.City;
import org.spring.springboot.dubbo.CityDubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
* 城市业务 Dubbo 服务层实现层
*
* Created by bysocket on 28/02/2017.
*/

// 注册为 Dubbo 服务
@Service(version = "1.0.0")
public class CityDubboServiceImpl implements CityDubboService {

public City findCityByName(String cityName) {
return new City(1L,2L,"温岭","是我的故乡");
}
}

server配置

## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=org.spring.springboot.dubbo

先启动server,然后启动client端,可以看到以下信息。
[置顶]        spring boot dubbo 简单例子这是github的链接