• 19.在springboot中集成dubbo(zookeeper)


    一、创建一个公共接口项目

    1.选择普通的maven创建项目

    项目结构

    2.pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>com.itgroupId>
    7. <artifactId>022-interface-apiartifactId>
    8. <version>1.0.0version>
    9. project>

    3.Student类(需要实现序列化)

    1. package com.it.entity;
    2. import java.io.Serializable;
    3. public class Student implements Serializable {
    4. private static final long serialVersionUID = -4654501682750659489L;
    5. private Integer id;
    6. private String name;
    7. private Integer age;
    8. @Override
    9. public String toString() {
    10. return "Student{" +
    11. "id=" + id +
    12. ", name='" + name + '\'' +
    13. ", age=" + age +
    14. '}';
    15. }
    16. public Integer getId() {
    17. return id;
    18. }
    19. public void setId(Integer id) {
    20. this.id = id;
    21. }
    22. public String getName() {
    23. return name;
    24. }
    25. public void setName(String name) {
    26. this.name = name;
    27. }
    28. public Integer getAge() {
    29. return age;
    30. }
    31. public void setAge(Integer age) {
    32. this.age = age;
    33. }
    34. }

    4.service包下的StudentService接口

    1. package com.it.service;
    2. import com.it.entity.Student;
    3. public interface StudentService {
    4. //根据id查找学生
    5. Student queryStudentById(Integer id);
    6. }

    二、创建服务提供者

    1.新建springboot项目,不需要添加任何依赖。

    项目结构

     2.手动导入dubbo依赖,导入zookeeper依赖时需要排除导入SLF4j依赖,以避免重复导入SLF4j依赖导致报错。

    pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.3version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.itgroupId>
    12. <artifactId>023-service-providerartifactId>
    13. <version>1.0.0version>
    14. <properties>
    15. <java.version>1.8java.version>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>com.itgroupId>
    20. <artifactId>022-interface-apiartifactId>
    21. <version>1.0.0version>
    22. dependency>
    23. <dependency>
    24. <groupId>org.apache.dubbogroupId>
    25. <artifactId>dubbo-spring-boot-starterartifactId>
    26. <version>2.7.8version>
    27. dependency>
    28. <dependency>
    29. <groupId>org.apache.dubbogroupId>
    30. <artifactId>dubbo-dependencies-zookeeperartifactId>
    31. <version>2.7.8version>
    32. <type>pomtype>
    33. <exclusions>
    34. <exclusion>
    35. <artifactId>slf4j-log4j12artifactId>
    36. <groupId>org.slf4jgroupId>
    37. exclusion>
    38. exclusions>
    39. dependency>
    40. <dependency>
    41. <groupId>org.springframework.bootgroupId>
    42. <artifactId>spring-boot-starterartifactId>
    43. dependency>
    44. <dependency>
    45. <groupId>org.springframework.bootgroupId>
    46. <artifactId>spring-boot-starter-testartifactId>
    47. <scope>testscope>
    48. dependency>
    49. dependencies>
    50. <build>
    51. <plugins>
    52. <plugin>
    53. <groupId>org.springframework.bootgroupId>
    54. <artifactId>spring-boot-maven-pluginartifactId>
    55. plugin>
    56. plugins>
    57. build>
    58. project>

    3.service包下的StudentServiceImpl实现类

    1. package com.it.service.impl;
    2. import com.it.entity.Student;
    3. import com.it.service.StudentService;
    4. import org.apache.dubbo.config.annotation.DubboService;
    5. import org.springframework.stereotype.Component;
    6. /**
    7. * 使用dubbo中的注解暴露服务
    8. */
    9. @Component
    10. @DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
    11. public class StudentServiceImpl implements StudentService {
    12. @Override
    13. public Student queryStudentById(Integer id) {
    14. Student student=new Student();
    15. if (1001==id){
    16. student.setId(1001);
    17. student.setName("小红");
    18. student.setAge(24);
    19. }else if (1002==id){
    20. student.setId(1002);
    21. student.setName("小黄");
    22. student.setAge(24);
    23. }else if (1003==id){
    24. student.setId(1003);
    25. student.setName("小蓝");
    26. student.setAge(21);
    27. }
    28. return student;
    29. }
    30. }

    4.springboot的核心配置文件application.properties

    1. #配置服务名称:等同于dubbo:application name="名称"
    2. spring.application.name=studentservice-provider
    3. #配置扫描的包,扫描的@DubboService
    4. dubbo.scan.base-packages=com.it.service
    5. #配置dubbo协议
    6. #dubbo.protocol.name=name
    7. #dubbo.protocol.port=20881
    8. #注册中心
    9. dubbo.registry.address=zookeeper://localhost:2181

    5.主函数入口类ProviderApplication

    1. package com.it;
    2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. /**
    6. * @EnableDubbo: 启动dubbo
    7. */
    8. @SpringBootApplication
    9. @EnableDubbo
    10. public class ProviderApplication {
    11. public static void main(String[] args) {
    12. SpringApplication.run(ProviderApplication.class, args);
    13. }
    14. }

    三、创建消费者

    1.新建springboot项目,选择添加web起步依赖。

    项目结构

     

    2.手动导入dubbo依赖,导入zookeeper依赖时需要排除导入SLF4j依赖,以避免重复导入SLF4j依赖导致报错。

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.3version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.itgroupId>
    12. <artifactId>024-consumerartifactId>
    13. <version>1.0.0version>
    14. <properties>
    15. <java.version>1.8java.version>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>com.itgroupId>
    20. <artifactId>022-interface-apiartifactId>
    21. <version>1.0.0version>
    22. dependency>
    23. <dependency>
    24. <groupId>org.apache.dubbogroupId>
    25. <artifactId>dubbo-spring-boot-starterartifactId>
    26. <version>2.7.8version>
    27. dependency>
    28. <dependency>
    29. <groupId>org.apache.dubbogroupId>
    30. <artifactId>dubbo-dependencies-zookeeperartifactId>
    31. <version>2.7.8version>
    32. <type>pomtype>
    33. <exclusions>
    34. <exclusion>
    35. <artifactId>slf4j-log4j12artifactId>
    36. <groupId>org.slf4jgroupId>
    37. exclusion>
    38. exclusions>
    39. dependency>
    40. <dependency>
    41. <groupId>org.springframework.bootgroupId>
    42. <artifactId>spring-boot-starterartifactId>
    43. dependency>
    44. <dependency>
    45. <groupId>org.springframework.bootgroupId>
    46. <artifactId>spring-boot-starter-testartifactId>
    47. <scope>testscope>
    48. dependency>
    49. dependencies>
    50. <build>
    51. <plugins>
    52. <plugin>
    53. <groupId>org.springframework.bootgroupId>
    54. <artifactId>spring-boot-maven-pluginartifactId>
    55. plugin>
    56. plugins>
    57. build>
    58. project>

    3.DubboController类

    1. package com.it.controller;
    2. import com.it.entity.Student;
    3. import com.it.service.StudentService;
    4. import org.apache.dubbo.config.annotation.DubboReference;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.annotation.Resource;
    9. @RestController
    10. public class DubboController {
    11. /**
    12. * @DubboReference:引用远程接口,把创建好的代理对象,注入给StudentService
    13. */
    14. @DubboReference(interfaceClass = StudentService.class,version = "1.0")
    15. private StudentService studentService;
    16. @GetMapping("/query")
    17. public String queryStudentById(Integer id){
    18. Student student = studentService.queryStudentById(1002);
    19. return "调用远程接口来获取对象:"+student;
    20. }
    21. }

    4.application.properties文件

    1. #指定服务名称
    2. spring.application.name=consumer-application
    3. #指定注册中心
    4. dubbo.registry.address=zookeeper://localhost:2181

     5.ConsumerApplication类

    1. package com.it;
    2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. @SpringBootApplication
    6. @EnableDubbo
    7. public class ConsumerApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(ConsumerApplication.class, args);
    10. }
    11. }

    四、启动zookeeper

    2.执行程序后,不能关闭程序,可以把该命令窗口最小化,才能保证 zookeeper是启动状态

     3.启动服务提供者

    4.启动消费者

    5.在浏览器地址框中输入url进行访问

     

  • 相关阅读:
    mac electron引入原生SDK
    sqli-labs(Less-4) extractvalue闯关
    MySQL主/从-主/主集群安装部署
    C++信息学奥赛1168:大整数加法
    数据库概念介绍
    基于stm32F103的座面声控台灯
    Java中的GUI编程
    2022-8-20 B树和B+树
    房产中介管理系统,房产中介预约看房系统,看房预约系统毕设作品
    Vue3.0 响应式reactive的原理实现
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/126725073