spring boot +dubbo+zookeeper
dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
结合本公司的开发也是用的dubbo这款优秀的框架,加上 最近工作重心的。所以对于dubbo的学习已经是迫在眉睫了。
在中秋假期,抽空实战了一把基于spring boot +dubbo+zookeeper 。其中也遇到了 很多的坑。
在这里记录一下。
我们看下dubbo的官网。http://dubbo.apache.org/en-us/ 。这里不在做赘述。
我们开始上手去实战一个初步的demo。用实战的角度去学习他。
在官网上也给我们了例子。
我们按照官网去实现它
首先 我们去搭建zookeeper。之所以选择zookeeper 我感觉还是资料多吧。
我们去下载zookeeper 然后启动就可以。
我这里用的是服务模式启动的。所以我在项目中用的 是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
然后启动后登陆就可以
这样就可以在管理后台查看我们的服务了。