spring-boot1.5.6整合dubbo-spring-boot-start2.0.0
Posted 512178509
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot1.5.6整合dubbo-spring-boot-start2.0.0相关的知识,希望对你有一定的参考价值。
项目是在做技术选型时做的Demo,后续没有用到
整个项目的环境搭建
JDK:1.8
maven:3.0.3
工具是使用的IDEA 2018年的
spring-boot:1.5.6RELEASE
dubbo-spring-boot-start:2.0.0
1、首先创建一个maven项目,做API接口 dubbo-api
package com.example.service; /** * @Author 冯浩 * @Description * @Date: create in 16:29 2018/5/23 * @Modified by */ public interface UserService { public String sayHello(String message); }
2、创建一个spring-boot项目,版本选择1.5.6RELEASE 项目名称dubbo-producer
pom文件如下
<?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.example</groupId> <artifactId>dubbo-producer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-producer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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> <dubbo.version>2.0.0</dubbo.version> <zk-client.version>0.10</zk-client.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-actuator</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zk-client.version}</version> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接口实现类为
package com.example.dubboproducer.service; import com.alibaba.dubbo.config.annotation.Service; import com.example.service.UserService; import org.springframework.stereotype.Component; /** * @Author 冯浩 * @Description * @Date: create in 16:38 2018/5/23 * @Modified by */ @Service(interfaceClass = UserService.class,version = "1.0.0") @Component public class UserServiceImpl implements UserService { @Override public String sayHello(String message) { return "hahha"+message; } }
启动类添加dubbo自动配置
package com.example.dubboproducer; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboProducerApplication { public static void main(String[] args) { SpringApplication.run(DubboProducerApplication.class, args); } }
配置文件为
spring.application.name=dubbo-spring-boot-starter spring.dubbo.server=true spring.dubbo.registry.address=zookeeper://localhost:2181 spring.dubbo.registry.check=false spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.example.dubboproducer.service
3、创建一个spring-boot项目,pom文件同上,项目名称dubbo-consumer
调用接口使用类
package com.example.dubboconsumer.service; import com.alibaba.dubbo.config.annotation.Reference; import com.example.service.UserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author 冯浩 * @Description * @Date: create in 17:08 2018/5/23 * @Modified by */ @RestController public class UserController { @Reference(version = "1.0.0") private UserService userService; @GetMapping("index") public String userTest(String mess){ return userService.sayHello(mess); } }
启动类添加dubbo自动配置类
package com.example.dubboconsumer; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); } }
配置文件
spring.application.name=dubbo-spring-boot-starter server.port=8081 spring.dubbo.registry.address=zookeeper://localhost:2181 spring.dubbo.scan=com.example.dubboconsumer.service
4、下载zookeeper的window的包,在本地启动
启动dubbo-producer,查询是否注册到注册地址
启动dubbo-consumer,之后在浏览器上http://localhost:8081/index?mess=1进行代码测试
返回信息及调用成功
以上是关于spring-boot1.5.6整合dubbo-spring-boot-start2.0.0的主要内容,如果未能解决你的问题,请参考以下文章
spring-boot 快速构建热部署自动配置原理配置文件整合SpringMVCredisjunitmybatis打包部署