首先我们先用myeclipse创建一个maven项目(可直接去dubbo官网生成http://start.dubbo.io/)。
码云demo地址:https://gitee.com/itCjb/Springboot-dubbo-mybatisplus
目录结构:
接下来复制一下pom配置(根据自己所需)
4.0.0
testClient
testClient
0.0.1-SNAPSHOT
jar
testClient
3.1
UTF-8
UTF-8
1.8
1.8
3.0-RC1
3.2.0
org.springframework.boot
spring-boot-starter-parent
1.5.15.RELEASE
com.fasterxml.jackson.dataformat
jackson-dataformat-yaml
2.9.3
mysql
mysql-connector-java
5.1.6
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jetty
com.h2database
h2
com.zaxxer
HikariCP
${HikariCP.version}
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus-boot-starter.version}
com.alibaba.boot
dubbo-spring-boot-starter
0.1.0
io.netty
netty-all
4.1.24.Final
org.apache.commons
commons-lang3
3.7
org.springframework.boot
spring-boot-starter-test
test
Client
maven-compiler-plugin
2.3.2
1.8
1.8
maven-war-plugin
2.6
${basedir}/WebRoot
false
org.springframework.boot
spring-boot-maven-plugin
然后building一下,下载需要的jar包。
完成之后,在com.example.demo包下创建第一个DemoApplication:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.IAffairsService;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@SpringBootApplication
//因为springboot会自动扫描一下mian所在的包,我的service放在上级目录所有不加入扫描。
@ComponentScan(basePackages = {
"com.baomidou.springboot.controller"})
public class DemoApplication {
@Reference(version = "1.0.0")
private IAffairsService affairsService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
/*
* 测试代码
* @PostConstruct
public void init() {
Affairs sayHello = affairsService.getById(8);
System.err.println(sayHello.getTitle());
}*/
}
接下来在com.example包下创建service(可利用mybatisplus的一键生成工具直接生成):
package com.example;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.springboot.entity.Affairs;
/**
*
* 服务类
*
*
* @author FUNKYE
* @since 2018-09-03
*/
public interface IAffairsService extends IService {
//因为使用了mybatisplus,目前没写专用的mapper,如果需要可以自己在下面写入
}
之后在创建所需要的实体类:
package com.baomidou.springboot.entity;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
/**
*
*
*
*
* @author FUNKYE
* @since 2018-09-03
*/
public class Affairs implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String title;
private Date date;
private String content;
@TableField("useradminId")
private Integer useradminId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getUseradminId() {
return useradminId;
}
public void setUseradminId(Integer useradminId) {
this.useradminId = useradminId;
}
@Override
public String toString() {
return "Affairs{" +
", id=" + id +
", title=" + title +
", date=" + date +
", content=" + content +
", useradminId=" + useradminId +
"}";
}
}
之后再创建Controller:
package com.baomidou.springboot.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.springboot.entity.Affairs;
import com.example.IAffairsService;
/**
*
* 前端控制器
*
*
* @author FUNKYE
* @since 2018-09-03
*/
@Controller
public class AffairsController extends BaseController {
@Reference(version = "1.0.0")
private IAffairsService as;
@ResponseBody
@RequestMapping("{token}/test")
public Object test(@PathVariable String token,HttpSession session) throws Exception {
MySessionContext myc= MySessionContext.getInstance();
session = myc.getSession(token);
return renderSuccess(session.getAttribute("111"));
}
@ResponseBody
@RequestMapping("test1")
public Object test1(HttpSession session) throws Exception {
session.setAttribute("111", "111");
return renderSuccess(session.getId());
}
@ResponseBody
@RequestMapping("findId")
public Object findId(String id){
Affairs a=as.getById(id);
return a;
}
}
以及所需要的继承的BaseController:
package com.baomidou.springboot.controller;
import com.baomidou.springboot.result.JsonResult;
/**
* Author: D.Yang
* Email: [email protected]
* Date: 16/10/9
* Time: 下午1:37
* Describe: 基础控制器
*/
public class BaseController {
/**
* 渲染失败数据
*
* @return result
*/
protected JsonResult renderError() {
JsonResult result = new JsonResult();
result.setSuccess(false);
result.setStatus("500");
return result;
}
/**
* 渲染失败数据(带消息)
*
* @param msg 需要返回的消息
* @return result
*/
protected JsonResult renderError(String msg) {
JsonResult result = renderError();
result.setMsg(msg);
return result;
}
/**
* 渲染成功数据
*
* @return result
*/
protected JsonResult renderSuccess() {
JsonResult result = new JsonResult();
result.setSuccess(true);
result.setStatus("200");
return result;
}
/**
* 渲染成功数据(带信息)
*
* @param msg 需要返回的信息
* @return result
*/
protected JsonResult renderSuccess(String msg) {
JsonResult result = renderSuccess();
result.setMsg(msg);
return result;
}
/**
* 渲染成功数据(带数据)
*
* @param obj 需要返回的对象
* @return result
*/
protected JsonResult renderSuccess(Object obj) {
JsonResult result = renderSuccess();
result.setObj(obj);
return result;
}
}
接下配置application.properties配置注册中心:
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.name= dubbo-demo-client
## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = zookeeper://localhost:2181?client=curator
dubbo.application.qosEnable=false
好了,可以去DemoApplication里启动属于你的springboot dubbo项目了!