springboot整合dubbo,zookeeper
一、项目结构
此处创建了一个父子项目,其中 admin是后台,common是公共模块,比如一些工具类、实体类都放在此处,portal是前台,servcie是dubbo服务。大体是这么划分。
此处我们以portal 调用 service (前台消费者,调用dubbo服务)为例。像admin中不在写demo。
二、dubbo服务
结构如下
主要的maven依赖
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency>
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; @SpringBootApplication @EnableDubboConfiguration public class ServerApp { public static void main(String[] args) { SpringApplication.run(ServerApp.class, args); } }
配置文件
spring.dubbo.application.name=dubbo-server spring.dubbo.registry.address=zookeeper://192.168.1.171:2181 #spring.dubbo.registry.address=zookeeper://192.168.1.171:2181?backup=192.168.1.172:2181,192.168.1.173:2181 spring.dubbo.server=true spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.exam.dubbo.service.impl
servcie接口和实现类
import com.exam.common.entity.User; public interface UserDubboService { public boolean saveUser(User user); }
注意实现类注入的servcie 注解,是阿里巴巴包下的注解
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.alibaba.dubbo.config.annotation.Service; import com.exam.common.entity.User; import com.exam.dubbo.dao.UserDao; import com.exam.dubbo.service.UserDubboService; @Service(interfaceClass=UserDubboService.class) @Component public class UserDubboServiceImpl implements UserDubboService { @Autowired private UserDao userDao; @Override @Transactional public boolean saveUser(User user) { return userDao.save(user); } }
三、portal消费者
项目结构
主要maven依赖
<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency>
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; @SpringBootApplication @EnableDubboConfiguration public class PortalApp extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { return application.sources(PortalApp.class); } public static void main(String[] args) { SpringApplication.run(PortalApp.class, args); } }
配置文件
#dubbo consumer spring.dubbo.application.name=exam-portal-consumer spring.dubbo.registry.address=zookeeper://192.168.1.171:2181 spring.dubbo.protocol.port=20800 spring.dubbo.protocol.name=dubbo spring.dubbo.scan=com.exam.dubbo.service.impl
controller
@Autowired private UserService userService; @RequestMapping("saveUser") public String saveUser(){ User user = new User(); user.setName("李四"); user.setPhone("123456"); userService.saveUser(user); return "success"; }
service和servcie的实现
import com.exam.common.entity.User; public interface UserService { public boolean saveUser(User user); }
注意此处调用dubbo服务,需要使用@Reference
import org.springframework.stereotype.Service; import com.alibaba.dubbo.config.annotation.Reference; import com.exam.common.entity.User; import com.exam.dubbo.service.UserDubboService; import com.exam.portal.service.UserService; @Service public class UserServiceImpl implements UserService { @Reference private UserDubboService userDubboService; @Override public boolean saveUser(User user) { return userDubboService.saveUser(user); } }
然后启动dubbo服务,再启动portal消费者服务。可以在dubbo的控制台看到,服务的提供者,和消费者都已经启动。
访问控制器,查看数据库中记录,发现已经执行完毕
最后附上案例代码
相关推荐
kongjunlongaa 2020-06-29
ATenhong 2020-10-15
supperme 2020-09-08
doctorvian 2020-08-02
aNian 2020-08-01
Fightingxr 2020-06-26
whileinsist 2020-06-24
doctorvian 2020-06-16
XuNeely 2020-06-16
wangyangsoftware 2020-06-16
大步流星 2020-06-16
aNian 2020-06-16
gaoyongstone 2020-06-16
MartellJenkins 2020-06-11
范群松 2020-06-11
Fightingxr 2020-06-08
XuNeely 2020-06-07
大步流星 2020-06-05