目录
1、搭建环境
1.1 ZooKeeper
1.2 dubbo-admin
2、搭建项目
2.1 父项目 dubbo-demo-parent
2.2 服务接口 demo-service
2.3 服务提供者demo-provider
2.4 服务消费者demo-consumer
3、测试
案例代码,tomcat,zookeeper,dubbo-admin包已上传,下载地址:https://download.csdn.net/download/qq_43280198/13524427,下文如有错误欢迎指出。
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=自己的地址/tmp
dataDirLog=自己的地址/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
建议自己拉取项目编译打包,在网上搜的war大多都启动不了...
启动的话有两种方式(建议使用第一种。第二种需要维持一个IDEA工作空间,有点占用内存...)
修改tomcat端口↓
修改项目访问路径↓,点击apply
启动成功会自动打开浏览器访问,输入用户名密码即可
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.5.RELEASE
com.example
dubbo-demo-parent
0.0.1-SNAPSHOT
dubbo-demo-parent
pom
Demo project for Spring Boot
demo-service
demo-provider
demo-consumer
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.1.6
org.projectlombok
lombok
1.18.16
com.baomidou
mybatis-plus-boot-starter
3.2.0
com.github.pagehelper
pagehelper-spring-boot-starter
RELEASE
src/main/resources
src/main/java
**/*.properties
**/*.xml
**/*.yml
false
org.springframework.boot
spring-boot-maven-plugin
true
central
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
springsource-repos
SpringSource Repository
https://repo.spring.io/release/
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
central-repos
Central Repository
https://repo.maven.apache.org/maven2/
central-repos2
Central Repository 2
https://repo1.maven.org/maven2/
aliyun-plugin
https://maven.aliyun.com/repository/public
true
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
4.0.0
com.example
dubbo-demo-parent
0.0.1-SNAPSHOT
demo-service
0.0.1-SNAPSHOT
demo-service
Demo project for Spring Boot
package com.example.demoservice.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
@Data
@TableName(value = "user")
public class User implements Serializable {
private static final long serialVersionUID = -7291508790203233996L;
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
}
package com.example.demoservice.service;
import com.example.demoservice.model.User;
public interface UserService {
String save(User user);
String sayHello(String word);
}
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&failOverReadOnly=false&useSSL=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
4.0.0
com.example
dubbo-demo-parent
0.0.1-SNAPSHOT
demo-provider
0.0.1-SNAPSHOT
demo-provider
Demo project for Spring Boot
com.example
demo-service
0.0.1-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
2.7.3
org.slf4j
slf4j-log4j12
org.apache.curator
curator-framework
4.0.1
org.apache.curator
curator-recipes
2.8.0
org.apache.zookeeper
zookeeper
3.4.13
pom
org.slf4j
slf4j-log4j12
com.101tec
zkclient
0.10
package com.example.demoprovider.service.impl;
import com.example.demoprovider.mapper.UserMapper;
import com.example.demoservice.model.User;
import com.example.demoservice.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Service
@Component
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public String save(User user) {
userMapper.insert(user);
return "success";
}
@Override
public String sayHello(String word) {
return "hello "+word;
}
}
package com.example.demoprovider.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demoservice.model.User;
public interface UserMapper extends BaseMapper {
}
server:
port: 8081 #端口
servlet:
context-path: /demo-provider #访问项目名称
tomcat:
uri-encoding: UTF-8
max-connections: 10000 # tomcat最大连接数
threads:
max: 200 #tomcat最大线程数
dubbo:
application:
name: demo-provider
registry:
address: zookeeper://localhost:2181
scan:
base-packages: com.example.demoprovider.service
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&failOverReadOnly=false&useSSL=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
hikari:
maximum-pool-size: 50
minimum-idle: 10
idle-timeout: 60000
connection-timeout: 60000
validation-timeout: 3000
login-timeout: 5
max-lifetime: 60000
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
mapper-locations: classpath*:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.example.demoprovider.model
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#mp2.3+ 全局表前缀 mp_
#table-prefix: mp_
#刷新mapper 调试神器
refresh-mapper: false
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置(下面3个配置)
logic-delete-value: 4
logic-not-delete-value: 0
configuration:
#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
map-underscore-to-camel-case: true
cache-enabled: false
#配置JdbcTypeForNull, oracle数据库必须配置
jdbc-type-for-null: 'null'
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pagehelper:
helperDialect: mysql
reasonable: false #设置为true当页码大于最大页码时会合理化为当前最大页码
supportMethodsArguments: true
params: count=countSql
package com.example.demoprovider;
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;
@SpringBootApplication
@EnableDubbo
@MapperScan("com.example.demoprovider.mapper")
public class DemoProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DemoProviderApplication.class, args);
}
}
4.0.0
com.example
dubbo-demo-parent
0.0.1-SNAPSHOT
demo-consumer
0.0.1-SNAPSHOT
demo-consumer
Demo project for Spring Boot
com.example
demo-service
0.0.1-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
2.7.3
org.slf4j
slf4j-log4j12
org.apache.curator
curator-framework
4.0.1
org.apache.curator
curator-recipes
2.8.0
org.apache.zookeeper
zookeeper
3.4.13
pom
org.slf4j
slf4j-log4j12
com.101tec
zkclient
0.10
server:
port: 8082 #端口
servlet:
context-path: /demo-consumer #访问项目名称
tomcat:
uri-encoding: UTF-8
max-connections: 10000 # tomcat最大连接数
threads:
max: 200 #tomcat最大线程数
dubbo:
application:
name: demo-consumer
registry:
address: zookeeper://localhost:2181
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&failOverReadOnly=false&useSSL=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
package com.example.democonsumer.controller;
import com.example.demoservice.model.User;
import com.example.demoservice.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference //该注解是dubbo提供的
private UserService userService;
@GetMapping("/say/{word}")
public String sayWord(@PathVariable String word) {
return userService.sayHello(word);
}
@GetMapping("/saveUser")
public String save() {
User user = new User();
user.setName("admin");
user.setAge(22);
return userService.save(user);
}
}