https://blog.csdn.net/IT_Holmes/article/details/124518330

部署ZooKeeper

详情见ZooKeeper

搭建控制台管理

  • 去Dubbo官方的Github上面下载dubbo-admin项目

git clone https://github.com/apache/dubbo-admin.git

  • 修改application.properties配置

    image.png

  • 按官方的方法来一步步启动

image.png

Production Setup

  1. Clone source code on develop branch git clone https://github.com/apache/dubbo-admin.git
  2. Specify registry address in dubbo-admin-server/src/main/resources/application.properties
  3. Build
    1. mvn clean package -Dmaven.test.skip=true
    2. 打包后可以在每个对应的target目录下,找到打包后的jar包 java -jar 包名 :就可以执行的。
  4. Start dubbo-admin
  • mvn --projects dubbo-admin-server spring-boot:run OR
  • cd dubbo-admin-distribution/target; java -jar dubbo-admin-0.1.jar
  • 注意要启动zookeeper注册中心。
  1. Visit http://localhost:8080
  2. Default username and password is root

dubbo-admin是管理页面应该属于Monitor监视部分,就算没有也不影响整个dubbo的工作:

  • dubbo admin是dubbo的图形页面控制台,具有服务查询、服务治理的功能。

image.png

问题一:8080端口占用
zookeeper 的一些高版本中包含一个AdminServer默认的端口是8080,所以导致8080端口占用,dubbo的jar包没法启动。
解决:修改dubbo-admin-server目录下面的application.properties文件添加server.port=9980,将其端口号改为9980。对应访问页面就成http://localhost:9980/了。

问题二: zookeeper not connected
https://zhuanlan.zhihu.com/p/343204228


进入发现,在CuratorZookeeperClient类中设置了连接的超时时间

  1. int timeout = url.getParameter("timeout",
  2. this.DEFAULT_CONNECTION_TIMEOUT_MS);

正确配置:

  • 修改application.properties文件

image.png

问题三:zookeeper提示包太大
https://www.iteye.com/blog/shift-alt-ctrl-1845568
原因:
客户端发送的包太大,超过jute.maxbuffer的设置,默认大小为1048575。
image.png
解决
修改jute.maxbuffer的配置,设置成50M

  • 修改zookeeper配置文件

image.png


搭建服务提供者、消费者

创建主module

创建 dubbo-demo 模块,在pom.xml引入需要的依赖

  1. <properties>
  2. <dubbo.version>3.0.8</dubbo.version>
  3. </properties>
  4. <dependencies>
  5. <!-- JAX-RS API -->
  6. <dependency>
  7. <groupId>javax.ws.rs</groupId>
  8. <artifactId>javax.ws.rs-api</artifactId>
  9. <version>2.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.slf4j</groupId>
  13. <artifactId>slf4j-api</artifactId>
  14. <version>1.7.36</version>
  15. </dependency>
  16. <!-- 引入Dubbo3 -->
  17. <dependency>
  18. <groupId>org.apache.dubbo</groupId>
  19. <artifactId>dubbo</artifactId>
  20. <version>${dubbo.version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.dubbo</groupId>
  24. <artifactId>dubbo-dependencies-zookeeper</artifactId>
  25. <version>${dubbo.version}</version>
  26. <type>pom</type>
  27. </dependency>
  28. </dependencies>

搭建公共项目

image.png
其他服务添加相应的依赖

  1. <dependency>
  2. <groupId>com.huang</groupId>
  3. <artifactId>dubbo-demo-interface</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>

XML方式

  • 创建dubbo-demo-xml模块,引入上面的公共项目

image.png

  • 服务提供者配置

image.png

  1. 在服务提供者项目上面创建配置文件,暴露服务 ```xml <?xml version=”1.0” encoding=”UTF-8”?>

  1. 2. 只需要导入xml文件,在spring中执行就可以了
  2. ```java
  3. package com.huang.dubbo.demo.provider;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import java.io.IOException;
  6. public class XmlProviderApplication {
  7. public static void main(String[] args) throws IOException {
  8. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
  9. context.start();
  10. System.in.read();
  11. }
  12. }

image.png

  • 服务消费者配置

image.png

  1. 创建consumer.xml文件,配置dubbo对应文件。 ```xml <?xml version=”1.0” encoding=”UTF-8”?>

  1. - 创建一个main方法来测试,是否能调用成功
  2. ```java
  3. package com.huang.dubbo.demo.consumer;
  4. import com.huang.dubbo.demo.DemoService;
  5. import com.huang.dubbo.demo.GreetingService;
  6. import com.huang.dubbo.demo.RestDemoService;
  7. import com.huang.dubbo.demo.TripleService;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import java.util.concurrent.*;
  10. public class XmlConsumerApplication {
  11. private static final int CORE_POOL_SIZE = 10;
  12. private static final int MAX_POOL_SIZE = 100;
  13. private static final int KEEP_ALIVE_TIME = 10;
  14. private static final int QUEUE_CAPACITY = 200;
  15. public static void main(String[] args) throws InterruptedException {
  16. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
  17. context.start();
  18. DemoService demoService = context.getBean("demoService", DemoService.class);
  19. GreetingService greetingService = context.getBean("greetingService", GreetingService.class);
  20. RestDemoService restDemoService = context.getBean("restDemoService", RestDemoService.class);
  21. TripleService tripleService = context.getBean("tripleService", TripleService.class);
  22. ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE_TIME
  23. ,TimeUnit.SECONDS,new ArrayBlockingQueue<>(QUEUE_CAPACITY), Executors.defaultThreadFactory());
  24. poolExecutor.execute(() -> {
  25. while (true) {
  26. try {
  27. String greetings = greetingService.hello();
  28. System.out.println(greetings + " from separated thread.");
  29. } catch (Exception e) {
  30. // e.printStackTrace();
  31. }
  32. try {
  33. Thread.sleep(1000);
  34. } catch (InterruptedException e) {
  35. }
  36. }
  37. });
  38. poolExecutor.execute(()->{
  39. while (true) {
  40. try {
  41. String restResult = restDemoService.sayHello("rest");
  42. System.out.println(restResult + " from separated thread.");
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. try {
  47. Thread.sleep(1000);
  48. } catch (InterruptedException e) {
  49. }
  50. }
  51. });
  52. poolExecutor.execute(()->{
  53. while (true) {
  54. try {
  55. String restResult = tripleService.hello();
  56. System.out.println(restResult + " from separated thread.");
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. try {
  61. Thread.sleep(1000);
  62. } catch (InterruptedException e) {
  63. }
  64. }
  65. });
  66. while (true) {
  67. try {
  68. CompletableFuture<String> hello = demoService.sayHelloAsync("world");
  69. System.out.println("result: " + hello.get());
  70. String greetings = greetingService.hello();
  71. System.out.println("result: " + greetings);
  72. } catch (Exception e) {
  73. // e.printStackTrace();
  74. }
  75. Thread.sleep(5000);
  76. }
  77. }
  78. }

image.png

  • 控制台管理调用

image.png

注解方式

  • 创建dubbo-demo-annotation模块,引入上面的公共项目
  • 服务提供者配置

image.png

  1. 新增配置文件dubbo-provider.properties

    1. dubbo.application.name=dubbo-demo-annotation-provider
    2. dubbo.protocol.name=dubbo
    3. dubbo.protocol.port=-1
  2. 创建一个main方法来测试,是否能调用成功 ```java package com.huang.dubbo.demo.provider;

import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;

public class AnnotationProviderApplication {

  1. public static void main(String[] args) throws Exception {
  2. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
  3. context.start();
  4. System.in.read();
  5. }
  6. @Configuration
  7. @EnableDubbo(scanBasePackages = "com.huang.dubbo.demo.provider")
  8. @PropertySource("classpath:/spring/dubbo-provider.properties")
  9. static class ProviderConfiguration {
  10. @Bean
  11. public RegistryConfig registryConfig() {
  12. RegistryConfig registryConfig = new RegistryConfig();
  13. registryConfig.setAddress("zookeeper://127.0.0.1:2181");
  14. return registryConfig;
  15. }
  16. }

}

  1. - **服务消费者配置**
  2. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/1416299/1654076914448-3720a922-0a70-45a2-aaa3-2cd61afc97ee.png#clientId=u9d41dfc5-c512-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=281&id=u3beaa577&margin=%5Bobject%20Object%5D&name=image.png&originHeight=281&originWidth=344&originalType=binary&ratio=1&rotation=0&showTitle=false&size=65165&status=done&style=none&taskId=u2c23e693-22d5-4d61-ae1f-79ebdf2ff4f&title=&width=344)
  3. 1. 新增配置文件dubbo-consumer.properties
  4. ```java
  5. dubbo.application.name=dubbo-demo-annotation-consumer
  6. dubbo.registry.address=zookeeper://127.0.0.1:2181
  7. dubbo.protocol.port=-1
  1. 添加配置文件DemoServiceComponent ```java package com.huang.dubbo.demo.consumer.comp;

import org.apache.dubbo.config.annotation.DubboReference; import com.huang.dubbo.demo.DemoService; import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component(“demoServiceComponent”) public class DemoServiceComponent implements DemoService { @DubboReference private DemoService demoService;

  1. @Override
  2. public String sayHello(String name) {
  3. return demoService.sayHello(name);
  4. }
  5. @Override
  6. public CompletableFuture<String> sayHelloAsync(String name) {
  7. return null;
  8. }

}

  1. 3. 创建一个main方法来测试,是否能调用成功
  2. ```java
  3. package com.huang.dubbo.demo.consumer;
  4. import com.huang.dubbo.demo.DemoService;
  5. import com.huang.dubbo.demo.consumer.comp.DemoServiceComponent;
  6. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  7. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.PropertySource;
  11. public class AnnotationConsumerApplication {
  12. public static void main(String[] args) {
  13. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
  14. context.start();
  15. DemoService service = context.getBean("demoServiceComponent", DemoServiceComponent.class);
  16. String hello = service.sayHello("world");
  17. System.out.println("result :" + hello);
  18. }
  19. @Configuration
  20. @EnableDubbo(scanBasePackages = "com.huang.dubbo.demo.consumer.comp")
  21. @PropertySource("classpath:/spring/dubbo-consumer.properties")
  22. @ComponentScan(value = {"com.huang.dubbo.demo.consumer.comp"})
  23. static class ConsumerConfiguration {
  24. }
  25. }

image.png

  • 调用

image.png

SpringBoot方式

  • 创建dubbo-demo-spring-boot模块,引入上面的公共项目,并引入springboot依赖

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter</artifactId>
    4. <version>${spring-boot.version}</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework.boot</groupId>
    8. <artifactId>spring-boot-autoconfigure</artifactId>
    9. <version>${spring-boot.version}</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-starter-logging</artifactId>
    14. <version>${spring-boot.version}</version>
    15. <exclusions>
    16. <!-- Fix the bug of log4j refer:https://github.com/apache/logging-log4j2/pull/608 -->
    17. <exclusion>
    18. <groupId>org.apache.logging.log4j</groupId>
    19. <artifactId>log4j-api</artifactId>
    20. </exclusion>
    21. </exclusions>
    22. </dependency>
  • 服务提供者配置

image.png

  1. dubbo:
  2. application:
  3. name: dubbo-demo-springboot-provider
  4. protocol:
  5. name: dubbo
  6. port: -1
  7. registry:
  8. id: zk-registry
  9. address: zookeeper://127.0.0.1:2181
  10. config-center:
  11. address: zookeeper://127.0.0.1:2181
  12. metadata-report:
  13. address: zookeeper://127.0.0.1:2181
  14. server:
  15. port: 6060
  1. package com.huang.dubbo.demo.provider;
  2. import com.huang.dubbo.demo.DemoService;
  3. import org.apache.dubbo.config.annotation.DubboService;
  4. import org.apache.dubbo.rpc.RpcContext;
  5. @DubboService
  6. public class DemoServiceImpl implements DemoService {
  7. @Override
  8. public String sayHello(String name) {
  9. System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
  10. return "Hello " + name;
  11. }
  12. }
  1. package com.huang.dubbo.demo.provider;
  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import java.util.concurrent.CountDownLatch;
  6. @SpringBootApplication
  7. @EnableDubbo(scanBasePackages = {"com.huang.dubbo.demo.provider"})
  8. public class SpringBootProviderApplication {
  9. public static void main(String[] args) throws InterruptedException {
  10. SpringApplication.run(SpringBootProviderApplication.class,args);
  11. System.out.println("dubbo service started");
  12. new CountDownLatch(1).await();
  13. }
  14. }
  • 服务消费者配置

image.png

  1. dubbo:
  2. application:
  3. name: dubbo-demo-springboot-consumer
  4. protocol:
  5. name: dubbo
  6. port: -1
  7. registry:
  8. id: zk-registry
  9. address: zookeeper://127.0.0.1:2181
  10. config-center:
  11. address: zookeeper://127.0.0.1:2181
  12. metadata-report:
  13. address: zookeeper://127.0.0.1:2181
  14. server:
  15. port: 6061
  1. package com.huang.dubbo.demo.consumer;
  2. import com.huang.dubbo.demo.DemoService;
  3. import org.apache.dubbo.config.annotation.DubboReference;
  4. import org.springframework.stereotype.Service;
  5. @Service
  6. public class SpringService {
  7. @DubboReference
  8. private DemoService demoService;
  9. public String doSayHello(String name) {
  10. return demoService.sayHello(name);
  11. }
  12. }
  1. package com.huang.dubbo.demo.consumer;
  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.ConfigurableApplicationContext;
  6. @SpringBootApplication
  7. @EnableDubbo
  8. public class SpringBootConsumerApplication {
  9. public static void main(String[] args) {
  10. ConfigurableApplicationContext context = SpringApplication.run(SpringBootConsumerApplication.class, args);
  11. SpringService application = context.getBean(SpringService.class);
  12. String result = application.doSayHello("world");
  13. System.out.println("result: " + result);
  14. }
  15. }

image.png

  • 调用

image.png

监控中心(Monitor)