SpringBoot2整合dubbo使用Zookeeper作为注册中心,使用yml配置
创建新项目
我们使用maven的空项目,不用任何模板直接创建
成都创新互联公司主要从事成都网站设计、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务江宁,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
输入项目信息
idea会自动去掉横线,自己加上
在项目中新建空Maven的module如下:
父项目依赖文件:使用dependencyManagement做依赖管理
4.0.0 io.ilss.dubbo spring-boot-dubbo-ilss 1.0 ilss-provider ilss-service ilss-consumer pom 1.8 2.0.3.RELEASE 0.2.0 2.0.0 1.1.10 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import com.alibaba.boot dubbo-spring-boot-starter ${dubbo-boot.version} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis-boot.version} com.alibaba druid ${druid.version} sonatype-nexus-snapshots https://oss.sonatype.org/content/repositories/snapshots false true
Provider配置
provider的依赖
spring-boot-dubbo-ilss io.ilss.dubbo 1.0 4.0.0 ilss-provider io.ilss.dubbo ilss-service 1.0 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.alibaba.boot dubbo-spring-boot-starter mysql mysql-connector-java runtime org.mybatis.spring.boot mybatis-spring-boot-starter com.alibaba druid org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin 1.8
provider的yml配置文件
spring: application: name: ilss-provider datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/xxx?useSSL=false username: root password: xxxmybatis: mapperLocations: classpath:mapper/*.xmldubbo: registry: protocol: zookeeper address: 127.0.0.1:2181 id: registry-1 protocol: port: 20888 name: dubbo id: dubbo status: server application: name: ilss-provider id: ilss-provider scan: basePackages: io.ilss.dubbo.service.impl provider: version: 1.0
provider的启动类
package io.ilss.dubbo;import org.springframework.boot.WebApplicationType;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;/** * className IlssProviderApplication * description IlssProviderApplication * * @author feng * @version 1.0 * @date 2019-01-30 18:08 */@SpringBootApplicationpublic class IlssProviderApplication { public static void main(String[] args) { //由于Provider不需要web服务,所以我们使用new SpringApplicationBuilder时web指定NONE new SpringApplicationBuilder(IlssProviderApplication.class).web(WebApplicationType.NONE).run(args); } }
Consumer配置
consumer依赖
spring-boot-dubbo-ilss io.ilss.dubbo 1.0 4.0.0 ilss-consumer io.ilss.dubbo ilss-service 1.0 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.alibaba.boot dubbo-spring-boot-starter org.springframework.boot spring-boot-starter-web
consumer的yml配置文件
server: port: 8080dubbo: application: id: ilss-consumer name: ilss-consumer registry: protocol: zookeeper address: 127.0.0.1:2181 id: registry-1 protocol: name: dubbo port: 20889 consumer: version: 1.0spring: application: name: ilss-consumer
consumer的启动类
package io.ilss.dubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * className IlssConsumerApplication * description IlssConsumerApplication * * @author feng * @version 1.0 * @date 2019-01-30 18:18 */@SpringBootApplicationpublic class IlssConsumerApplication { public static void main(String[] args) { SpringApplication.run(IlssConsumerApplication.class); } }
服务实现
service接口:在service中新建interface TestService
代码如下:
package io.ilss.dubbo.service;/** * className TestService * description TestService * * @author feng * @version 1.0 * @date 2019-01-30 18:07 */public interface TestService { String test(String text); }
在provider中新建ServiceImpl
代码如下:
package io.ilss.dubbo.service.impl;import com.alibaba.dubbo.config.annotation.Service;import io.ilss.dubbo.service.TestService;/** * className TestServiceImpl * description TestServiceImpl * * @author feng * @version 1.0 * @date 2019-01-30 18:08 */@Service( version = "${dubbo.provider.version}", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")public class TestServiceImpl implements TestService { @Override public String test(String text) { return "Success! Your String is " + text; } }
Consumer中添加测试的TestController
package io.ilss.dubbo.web;import com.alibaba.dubbo.config.annotation.Reference;import io.ilss.dubbo.service.TestService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * className TestController * description TestController * * @author feng * @version 1.0 * @date 2019-01-30 18:19 */@RestControllerpublic class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Reference private TestService testService; @GetMapping("/test") public String test(@RequestParam String text) { logger.info("text value : {}",text); return testService.test(text); } }
完整结构
运行
启动Zookeeper端口为2181,怎么安装启动自行Google
启动IlssProviderApplication
启动IlssConsumerApplication
游览器访问http://localhost:8080/test?text=abc 看到如下页面和log即完成
标题名称:SpringBoot2整合dubbo使用Zookeeper作为注册中心,使用yml配置
文章URL:http://cdiso.cn/article/giopip.html