一、安装配置Zookeeper

[root@instance-38r7isl1] mkdir /usr/local/zookeeper
[root@instance-38r7isl1] cd /usr/local/zookeeper
[root@instance-38r7isl1] wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz
  
[root@instance-38r7isl1] tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz
[root@instance-38r7isl1] cd apache-zookeeper-3.6.1-bin
[root@instance-38r7isl1] cd conf
[root@instance-38r7isl1] cp zoo_sample.cfg zoo.cfg

#- 配置zoo.cfg
[root@instance-38r7isl1] vi zoo.cfg 
#......
## 配置省略...
#......

# 配置环境变量
[root@instance-38r7isl1] vi /etc/profile
[root@instance-38r7isl1] export ZOOKEEPER_INSTALL=/usr/local/apache-zookeeper-3.6.1-bin
[root@instance-38r7isl1] export PATH=$PATH:$ZOOKEEPER_INSTALL/bin

[root@instance-38r7isl1] source /etc/profile

# 启动zookeeper
[root@instance-38r7isl1] ./zkServer.sh start

二、Provider工程

  • 依赖
 <!-- Apache Dubbo  -->
 <dependency>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo-dependencies-bom</artifactId>
     <version>2.7.7</version>
     <type>pom</type>
     <scope>import</scope>
 </dependency>

 <dependency>
     <groupId>org.apache.curator</groupId>
     <artifactId>curator-recipes</artifactId>
     <version>2.13.0</version>
 </dependency>

 <!-- Dubbo Spring Boot Starter -->
 <dependency>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo-spring-boot-starter</artifactId>
     <version>2.7.7</version>
 </dependency>

 <dependency>
     <groupId>org.apache.zookeeper</groupId>
     <artifactId>zookeeper</artifactId>
     <version>3.6.1</version>
 </dependency>
  • application.yml
dubbo:
  protocol:
    name: dubbo
    port: 20880
  registry:
    protocol: zookeeper
    address: zookeeper://ip:2181
  application:
    name: study_dubbo
  consumer:
    timeout: 3000
  scan:
    base-packages: com.example.dubbo.service
  • Service
package com.example.dubbo.service.impl;

import com.example.dubbo.facade.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

/**
 * @author liuteng
 */
@Component
@DubboService(timeout = 500, version = "1.0.0", interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Override
    public String getUserIdByPhone(String phone) {
        return "1000000101001";
    }

}

  • 启动项目
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.io.tmpdir=D:\Users\177510~1\AppData\Local\Temp\
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.compiler=<NA>
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.name=Windows 10
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.arch=amd64
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.version=10.0
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.name=17751033130
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.home=D:\Users\17751033130
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.dir=D:\myProject\state_machine
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.memory.free=128MB
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.memory.max=3602MB
2020-06-12 10:00:12.007  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.memory.total=202MB
2020-06-12 10:00:12.008  INFO 20756 --- [           main] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=x.x.x.x:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@5965844d
2020-06-12 10:00:12.010  INFO 20756 --- [           main] org.apache.zookeeper.common.X509Util     : Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-06-12 10:00:12.018  INFO 20756 --- [           main] org.apache.zookeeper.ClientCnxnSocket    : jute.maxbuffer value is 1048575 Bytes
2020-06-12 10:00:12.021  INFO 20756 --- [           main] org.apache.zookeeper.ClientCnxn          : zookeeper.request.timeout value is 0. feature enabled=false
2020-06-12 10:00:12.193  INFO 20756 --- [.13.181.6:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server x.x.x.x/x.x.x.x:2181.
2020-06-12 10:00:12.194  INFO 20756 --- [.13.181.6:2181)] org.apache.zookeeper.ClientCnxn          : SASL config status: Will not attempt to authenticate using SASL (unknown error)
2020-06-12 10:00:12.258  INFO 20756 --- [.13.181.6:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established, initiating session, client: /10.118.37.75:64301, server: x.x.x.x/x.x.x.x:2181
2020-06-12 10:00:12.326  INFO 20756 --- [.13.181.6:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server x.x.x.x/x.x.x.x:2181, session id = 0x1000f88ecbe000f, negotiated timeout = 40000
2020-06-12 10:00:12.330  INFO 20756 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: CONNECTED
2020-06-12 10:00:14.166  INFO 20756 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-12 10:00:14.360  INFO 20756 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2020-06-12 10:00:14.368  INFO 20756 --- [           main] com.example.demo.DubboClientApplication  : Started DubboClientApplication in 3.946 seconds (JVM running for 4.747)
2020-06-12 10:00:21.061  INFO 20756 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-12 10:00:21.061  INFO 20756 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-06-12 10:00:21.078  INFO 20756 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 17 ms

Process finished with exit code -1

三、Facade工程

该工程主要用于接口定义

  • UserService
package com.example.dubbo.facade.service;

/**
 * @author liuteng
 */
public interface UserService {

    String getUserIdByPhone(String phone);

}

四、Consumer工程

  • 依赖
<!-- Apache Dubbo  -->
 <dependency>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo-dependencies-bom</artifactId>
     <version>2.7.7</version>
     <type>pom</type>
     <scope>import</scope>
 </dependency>

 <dependency>
     <groupId>org.apache.curator</groupId>
     <artifactId>curator-recipes</artifactId>
     <version>2.13.0</version>
 </dependency>

 <!-- Dubbo Spring Boot Starter -->
 <dependency>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo-spring-boot-starter</artifactId>
     <version>2.7.7</version>
 </dependency>

 <dependency>
     <groupId>org.apache.zookeeper</groupId>
     <artifactId>zookeeper</artifactId>
     <version>3.6.1</version>
 </dependency>
  • application.yml
server:
  port: 9090


dubbo:
  application:
    name: dubbo_client
  registry:
    protocol: zookeeper
    address: 106.13.181.6:2181
  scan:
    base-packages: com.example.demo.controller

  • Controller
package com.example.demo.controller;

import com.example.dubbo.facade.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuteng
 */
@RestController
@RequestMapping(value = "user/")
public class UserController {

    @DubboReference(version = "1.0.0")
    private UserService userService;

    @GetMapping(value = "/getUserIdByPhone")
    public String getUserIdByPhone(String phone){
        return userService.getUserIdByPhone(phone);
    }

}

  • 测试结果
2020-06-12 10:19:30.516  INFO 18260 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-12 10:19:30.516  INFO 18260 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-06-12 10:19:30.516 DEBUG 18260 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2020-06-12 10:19:30.532 DEBUG 18260 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2020-06-12 10:19:30.532  INFO 18260 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 16 ms
2020-06-12 10:19:30.539 DEBUG 18260 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/user/getUserIdByPhone?phone=123", parameters={masked}
2020-06-12 10:19:30.541 DEBUG 18260 --- [nio-9090-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.demo.controller.UserController#getUserIdByPhone(String)
2020-06-12 10:19:30.673 DEBUG 18260 --- [nio-9090-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2020-06-12 10:19:30.673 DEBUG 18260 --- [nio-9090-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing ["1000000101001"]
2020-06-12 10:19:30.680 DEBUG 18260 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

参考文档:https://github.com/apache/dubbo-spring-boot-project

Demo地址:https://github.com/LiutTeng/study_dubbo.git

问题一: java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
解决方案:添加依赖:

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.13.0</version>
</dependency>
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐