既然是开发案例,显然不会扯那么多老婆舌,有不清楚这两个东东的请自行百度。
JDK1.7、Maven、Eclipse、SpringBoot1.5.1、Dubbo2.8.4
我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。
pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.itstyle.dubbogroupId>
<artifactId>spring-boot-dubbo-providerartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>springBoot_dubbo_providername>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.7java.version>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.1.RELEASEversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.8.4version>
<exclusions>
<exclusion>
<artifactId>springartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.6version>
dependency>
<dependency>
<groupId>org.javassistgroupId>
<artifactId>javassistartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>springloadedartifactId>
<version>1.4.0.RELEASEversion>
dependency>
dependencies>
plugin>
plugins>
build>
project>
User.java 一定要实现序列化接口
public class User implements Serializable {
private static final long serialVersionUID = -1L;
private String username;
private Integer age;
public User(String username, Integer age) {
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
IUserService.java 接口
public interface IUserService {
void saveUser(User user);
}
UserServiceImpl.java 实现
@Service("userService")
public class UserServiceImpl implements IUserService {
@Override
public void saveUser(User user) {
System.out.println("保存用户:"+user.getUsername());
}
}
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="springBoot_dubbo_provider" />
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" check="false" />
<dubbo:protocol name="dubbo" port="-1" dispather="all" check="false" />
<dubbo:provider timeout="10000" threads="10" threadpool="fixed" loadbalance="roundrobin"/>
<dubbo:service interface="com.itstyle.dubbo.service.IUserService" ref="userService"/>
beans>
application.properties
# 项目contextPath,一般在正式发布版本中,我们不配置
server.context-path=/provider
# 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知
server.error.path=/error
# 服务端口
server.port=8080
# session最大超时时间(分钟),默认为30
server.session-timeout=60
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
# server.address=192.168.16.11
# tomcat最大线程数,默认为200
server.tomcat.max-threads=800
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
#ZooKeeper
dubbo.registry.address=192.168.1.180:2181
logging.level.root=INFO
SpringbootDubboApplication.java 测试类
@SpringBootApplication
@ComponentScan(basePackages={"com.itstyle.dubbo"})
@ImportResource({"classpath:dubbo.xml"})
public class SpringbootDubboApplication implements CommandLineRunner {
@Autowired
private IUserService userService;
public static void main(String[] args) {
SpringApplication.run(SpringbootDubboApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
User user = new User("张三",19);
userService.saveUser(user);
}
}
此时,我们运行SpringbootDubboApplication类,如下结果说明成功:
然后使用ZooInspector查看zookeeper信息,会发现注册了一个提供者:
项目:http://git.oschina.net/52itstyle/springboot_dubbo_provider
作者: 小柒
出处: https://blog.52itstyle.com
本文版权归作者和云栖社区所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 如有问题, 可邮件([email protected])咨询。
阅读原文
http://click.aliyun.com/m/26281/