5分钟实现SpringBoot整合Dubbo构建分布式服务
阅读本文大概需要 7 分钟。
概述:
-
Registry:服务注册,我们一般会采取Zookeeper 作为我们的注册中心
-
Provider:服务提供者(生产者),提供具体的服务实现
-
Consumer:消费者,从注册中心中订阅服务
-
Monitor:监控中心,RPC调用次数和调用时间监控
-
生产者发布服务到服务注册中心中
-
消费者在服务注册中心中订阅服务
-
消费者调用已经注册的服务
一、项目构建
-
Spring-boot
-
JDK 8
-
Dubbo
-
Zookeeper
http://www.cnblogs.com/jaycekon/p/7562688.html
http://www.cnblogs.com/jaycekon/p/7553909.html
二、Productor
2.1、Pom.xml
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
2.2、配置文件
spring.dubbo.application.name=provider –服务名称
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 — 注册中心地址
spring.dubbo.protocol.name=dubbo — dubbo 协议
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.jaycekon.dubbo.service –声明需要暴露的服务接口
<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=“provider” />
<!– 注册中心服务地址 –>
<dubbo:registry protocol=“zookeeper” address=“127.0.0.1” check=“false” />
<!– 用dubbo协议–>
<dubbo:protocol name=“dubbo” port=“-1” dispather=“all” check=“false” />
<dubbo:provider timeout=“10000” threads=“10” threadpool=“fixed” loadbalance=“roundrobin”/>
<!– 声明需要暴露的服务接口 –>
<dubbo:service interface=“com.jaycekon.dubbo.service” ref=“userService”/> </beans>
2.3、服务提供
/**
* Created by Jaycekon on 2017/9/19.
*/
public interface UserService {
User saveUser(User user);
}
import com.jaycekon.dubbo.domain.User;
import com.jaycekon.dubbo.service.UserService;
/**
* Created by Jaycekon on 2017/9/19.
*/
@Service
public class UserServiceImpl implements UserService {
@Override
public User saveUser(User user) {
user.setId(1);
System.out.println(user.toString());
return user;
}
}
2.4、总体结构
三、Consumer
3.1、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/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jaycekon</groupId>
<artifactId>spring-boot-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!– Spring Boot Dubbo 依赖 –>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!– mvn spring-boot:run 热部署启动 –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2、配置文件
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.jaycekon.dubbo.service
<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=“provider” />
<!– 注册中心服务地址 –>
<dubbo:registry protocol=“zookeeper” address=“${dubbo.registry.address}” check=“false” />
<!– 用dubbo协议–>
<dubbo:protocol name=“dubbo” port=“-1” dispather=“all” check=“false” />
<dubbo:provider timeout=“10000” threads=“10” threadpool=“fixed” loadbalance=“roundrobin”/>
<!– 声明需要暴露的服务接口 –>
<dubbo:service interface=“com.jaycekon.dubbo.service” ref=“userService”/>
</beans>
3.3、服务实现
/**
* Created by Jaycekon on 2017/9/19.
*/
public interface UserService {
User saveUser(User user);
}
import com.jaycekon.dubbo.domain.City;
import com.jaycekon.dubbo.domain.User;
import org.springframework.stereotype.Component;
/**
* 城市 Dubbo 服务消费者
* <p>
* Created by Jaycekon on 20/09/2017.
*/
@Component
public class CityDubboConsumerService {
@Reference
CityDubboService cityDubboService;
@Reference
UserService userService;
public void printCity() {
String cityName = “广州”;
City city = cityDubboService.findCityByName(cityName);
System.out.println(city.toString());
}
public User saveUser() {
User user = new User();
user.setUsername(“jaycekon”)
.setPassword(“jaycekong824”);
return userService.saveUser(user);
}
}
3.4、服务调用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Jaycekon on 2017/9/19.
*/
@RestController
public class UserController {
@Autowired
private CityDubboConsumerService service;
@RequestMapping(“/save“)
public Object saveUser() {
return service.saveUser();
}
}
3.5、目录结构
四、总结
https://github.com/jaycekon/SpringBoot
推荐阅读:
微信扫描二维码,关注我的公众号
朕已阅
文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/1208.html