dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。java
结合本公司的开发也是用的dubbo这款优秀的框架,加上 最近工做重心的。因此对于dubbo的学习已是迫在眉睫了。git
在中秋假期,抽空实战了一把基于spring boot +dubbo+zookeeper 。其中也遇到了 不少的坑。github
在这里记录一下。web
咱们看下dubbo的官网。http://dubbo.apache.org/en-us/ 。这里不在作赘述。spring
咱们开始上手去实战一个初步的demo。用实战的角度去学习他。apache
在官网上也给咱们了例子。api
咱们按照官网去实现它 app
首先 咱们去搭建zookeeper。之因此选择zookeeper 我感受仍是资料多吧。框架
咱们去下载zookeeper 而后启动就能够。webapp
我这里用的是服务模式启动的。因此我在项目中用的 是3000的端口
咱们去常见一个通用的api
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
配置文件
spring: application: name: interface
咱们去建立provider
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.studay.leizi</groupId> <artifactId>leiziuser-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>leiziuser-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- Spring Boot dependencies --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!-- Zookeeper dependencies --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.4</version> <scope>compile</scope> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.0.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
实现方法
package com.studay.leizi.leiziuserserver.imp; import com.alibaba.dubbo.config.annotation.Service; import com.example.demo.service.DemoService; import org.springframework.beans.factory.annotation.Value; @Service public class DefaultDemoService implements DemoService { @Value("dubbo-auto-configuration-provider-demo") private String serviceName; public String sayHello(String name) { return String.format("Hello, %s", name); } }
application.yml配置
spring: application: name: dubbo-registry-zookeeper-consumer-sample dubbo: registry: address: zookeeper://127.0.0.1:3000 protocol: zookeeper id: my-registry protocol: name: dubbo port: 12345 id: dubbo status: server application: id: cusss name: cusss scan: basePackages: com.studay.leizi.leiziuserserver server: port: 8081
这个时候咱们去启动。
启动成功,咱们去建立服务消费方
package com.styday.leiziapi.connec; import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.config.annotation.Service; import com.example.demo.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Service @Component public class Demoe implements DemoService { @Reference DemoService demoService; @Override public String sayHello(String name) { return demoService.sayHello(name); } }
为了方便咱们查看调用是否成功,咱们编写controller
package com.styday.leiziapi.connter; import com.styday.leiziapi.connec.Demoe; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @RestController("/home") public class Consned { @Autowired Demoe demoe; @GetMapping("/getPayInfo") public String say(@RequestParam("name") String name) { return demoe.sayHello(name); } }
application.yml配置
spring: application: name: consumer dubbo: registry: address: zookeeper://127.0.0.1:3000 protocol: name: dubbo port: 2081 id: dubbo application: id: cusss name: cusss scan: basePackages: com.styday.leiziapi.connec.Demoe server: port: 9999
启动完毕后, 启动。
、
咱们能够用dubbo管理后台,去看服务的注册。下载地址 https://github.com/apache/dubbo/tree/2.5.x、
mvn clean package -Dmaven.test.skip=true
而后 放在Tomcat启动。
配置下
webapps\dubbo-admin-2.6.0\WEB-INF\dubbo.properties
而后启动后登录就能够
这样就能够在管理后台查看咱们的服务了。