手把手教你使用Springboot整合dubbo,搭建一个微服务
由于dubbo的注册中心用的是zookeeper,所以首先需要。
·
文章目录
开发前提
由于dubbo的注册中心用的是zookeeper,所以首先需要安装zookeeper。
构建Springboot项目
第一步:选择新建project或者module,在界面中选择maven点击next:
第二步:填上项目的基本信息点击Finish:
第三步:右击项目new -> Module:
第四步:在界面中选择maven点击next:
第五步:填上项目的基本信息点击Finish:
第六步:重复第三,四,五步,分别创建项目需要的dubbo-api,dubbo-provider,dubbo-customer几个模块,如下:
第七步:导入父工程依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-provider</module>
<module>dubbo-customer</module>
<module>dubbo-api</module>
</modules>
<properties>
<java.version>1.8</java.version>
<source.level>1.8</source.level>
<target.level>1.8</target.level>
<lombok.version>1.18.16</lombok.version>
<skip_maven_deploy>true</skip_maven_deploy>
<spring-boot-dependencies.version>2.4.1</spring-boot-dependencies.version>
<spring-cloud-dependencies.version>Dalston.SR4</spring-cloud-dependencies.version>
<junit.version>4.12</junit.version>
<dubbo.version>3.0.2.1</dubbo.version>
<spring-dubbo.version>2.0.0</spring-dubbo.version>
<lombok.version>1.18.16</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 统一jar版本管理,避免使用 spring-boot-parent -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--dubbo 和 springboot 整合的包-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
开发api模块
user实体类:
package com.demo.api.entity;
import lombok.Data;
import java.io.Serializable;
/**
* @Author: laz
* @CreateTime: 2022-10-26 10:56
* @Version: 1.0
*/
@Data
public class User implements Serializable {
private Long id;
private String username;
private String password;
}
创建本次测试的接口:
package com.demo.api.service;
import com.demo.api.entity.User;
public interface IUserService {
User selectUserById(Long id);
}
开发生产者模块
第一步:导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo 与 spring-boot 整合包-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--springboot 启动核心包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql 的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
第二步:添加配置
server:
port: 8081
spring:
application:
name: dubbo-samples-privider-springCloud
#配置数据源信息
datasource:
#配置连接数据库的各个信息
driver-class-name: com.mysql.cj.jdbc.Driver
#设置字符集
url: jdbc:mysql://8.142.127.37:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: 123456
mybatis-plus:
#配置类型别名所对应的包
type-aliases-package: com.demo.provider.entity
#配置SQL输出语句com.winsun.dataclean.mapper
mapper-locations: com/demo/provider/mapper/*.xml
dubbo:
application:
name: ${spring.application.name}
registry:
address: zookeeper://43.139.86.193:2181
timeout: 2000
protocol:
name: dubbo
port: 20890
# 扫描 @DubboService 注解
scan:
base-packages: com.demo.provider.service.impl
第三步:编写启动类
package com.demo.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author: laz
* @CreateTime: 2022-10-26 11:05
* @Version: 1.0
*/
@EnableDubbo
@SpringBootApplication
@MapperScan("com.demo.provider.mapper")
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
System.out.println("生产者启动完毕");
}
}
第四步:添加mapper接口
package com.demo.provider.mapper;
import com.demo.api.entity.User;
/**
* @Author: laz
* @CreateTime: 2022-10-26 11:01
* @Version: 1.0
*/
public interface UserMapper {
User selectUserById(Long id);
}
xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.demo.provider.mapper.UserMapper">
<select id="selectUserById" resultType="com.demo.api.entity.User">
select * from user where id = #{id}
</select>
</mapper>
第五步:实现接口:
package com.demo.provider.service.impl;
import com.demo.api.entity.User;
import com.demo.api.service.IUserService;
import com.demo.provider.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @Author: laz
* @CreateTime: 2022-10-26 11:00
* @Version: 1.0
*/
@DubboService
@AllArgsConstructor
public class UserServiceImpl implements IUserService {
private final UserMapper userMapper;
public User selectUserById(Long id) {
User user = userMapper.selectUserById(id);
return user;
}
}
第六步:编写controller层接口
package com.demo.provider.controller;
import com.demo.api.entity.User;
import com.demo.api.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: laz
* @CreateTime: 2022-10-26 11:53
* @Version: 1.0
*/
@RestController
@RequestMapping("/provider")
@AllArgsConstructor
public class UserController {
private final IUserService userService;
@RequestMapping("/selectUserById/{id}")
public User selectUserById(@PathVariable("id")Long id){
return userService.selectUserById(id);
}
}
开发消费者模块
第一步:导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-customer</artifactId>
<dependencies>
<!--dubbo-samples-springcloud-api 项目 依赖-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
</dependencies>
</project>
第二步:添加配置
server:
port: 8082
spring:
application:
name: dubbo-samples-consumer-springCloud
dubbo:
registry:
address: zookeeper://43.139.86.193:2181
timeout: 2000
protocol:
name: dubbo
第三步:编写启动类:
package com.demo.customer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
System.out.println("消费者启动完毕!");
}
}
第四步:编写调用生产者接口
package com.demo.customer.controller;
import com.demo.api.entity.User;
import com.demo.api.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerUserController {
@DubboReference( protocol = "dubbo", loadbalance = "random")
private IUserService userService;
@RequestMapping("/selectUserById/{id}")
public User getUser(@PathVariable("id") Long id) {
User user = userService.selectUserById(id);
log.info("response from provider: {}", user);
return user;
}
}
整个项目结构如下:
测试
分别启动生产者和消费者,在浏览器分别调用以下接口:
http://localhost:8081/provider/selectUserById/1
http://localhost:8082/consumer/selectUserById/1
结果:
更多推荐
已为社区贡献5条内容
所有评论(0)