【SpringBoot+dubbo+zk】实现服务之间rpc通信

0)前置准备,我们使用zk作为注册中心,先启动zk,也就是2181端口。

1)父工程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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.imooc</groupId>
  <artifactId>dubbo-demo</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <modules>
    <module>api</module>
    <module>consumer</module>
    <module>provider</module>
  </modules>


</project>

2)api

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.imooc</groupId>
  <artifactId>dubbo-demo-api</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

</project>

2个接口

package com.imooc.springboot.dubbo.demo;

public interface CityService {

    String city(Integer cityId);
}

package com.imooc.springboot.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}

3)服务1

application.properties

server.port=8082
#####dubbo服务消费者#####
#应用名字
spring.dubbo.application.name=demo-consumer
#扫描的包
spring.dubbo.scan=com.imooc.springboot.dubbo.demo.consumer
# 从哪个注册中心取服务
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
# 使用的协议名字(可缺,估计默认的就是这个)
spring.dubbo.protocol.name=dubbo
# 使用的协议端口(不可缺,别的端口如:20881等也是可以,这个应该是唯一的,provider用过了,consumer就不能用了)
spring.dubbo.protocol.port=20881

Main.java

package com.imooc.springboot.dubbo.demo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

DemoController.java

package com.imooc.springboot.dubbo.demo.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.imooc.springboot.dubbo.demo.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Reference
    private DemoService demoService;

    /**
     * 浏览器中访问 http://localhost:8082/sayHello?name="22"
     *
     * @param name
     * @return
     */
    @RequestMapping("/sayHello")
    public String sayHello(@RequestParam String name) {
        return demoService.sayHello(name);
    }
}

CityServiceImpl.java // 这里我们让消费者也是生产者,我们也提供一些实现

package com.imooc.springboot.dubbo.demo.consumer.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.imooc.springboot.dubbo.demo.CityService;

@Service
public class CityServiceImpl implements CityService {

    @Override
    public String city(Integer cityId) {
        return "你的cityId=" + cityId;
    }
}

4)服务2

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.imooc</groupId>
  <artifactId>dubbo-demo-provider</artifactId>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.dubbo.springboot</groupId>
      <artifactId>spring-boot-starter-dubbo</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.imooc</groupId>
      <artifactId>dubbo-demo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>


  </dependencies>

</project>

ProviderApplication.java

package com.imooc.springboot.dubbo.demo.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(ProviderApplication.class,args);

    }

}

CityController.java

package com.imooc.springboot.dubbo.demo.provider.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.imooc.springboot.dubbo.demo.CityService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CityController {

    @Reference
    private CityService cityService;

    /**
     * http://localhost:8081/cityId?cityId=1
     *
     * @param cityId
     * @return
     */
    @RequestMapping("/cityId")
    public String cityId(@RequestParam Integer cityId) {
        return cityService.city(cityId);
    }
}

DemoServiceImpl.java

package com.imooc.springboot.dubbo.demo.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.imooc.springboot.dubbo.demo.DemoService;

@Service
public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Hello, consumer" + name + " (i am provider)";
    }

}

运行:

打开zk,观察下注册中心中的数据:

所以,zk中记录的是:每一个接口的生产者和消费者是谁。这样子有人消费接口时,就可以查找一个就行。

总结dubbo的使用:

1.api中写接口

2.@Service注解使用dubbo的,不是Spring的那个

3.Controller中使用某个Service时使用@Reference而不是@Autowired

就这么简单,每个服务既是生产者又是消费者!所以我们可以轻松的实现集群,GateWay去引用各个服务的实现即可,实现所谓的微服务。

相关推荐
刘Java18 小时前
Dubbo 3.x源码(28)—Dubbo服务发布导出源码(7)应用级服务接口元数据发布
java·dubbo·dubbo源码
刘Java4 天前
Dubbo 3.x源码(26)—Dubbo服务引用源码(9)应用级服务发现订阅refreshServiceDiscoveryInvoker
java·dubbo·dubbo源码
码农老起13 天前
从RocketMQ到Dubbo:自研中间件技术的崛起
中间件·dubbo·rocketmq
huahailing102414 天前
apache-dubbo
apache·dubbo
博洋科技14 天前
关于网站的权重和百度蜘蛛爬虫的关系
小程序·dubbo·网站建设·1024程序员节·保定h5网站建设·保定网站建设
B1nna14 天前
外卖开发(七)——校验收货地址是否超出配送范围
开发语言·dubbo·lua
唐梓航-求职中16 天前
rpc-dubbo-多版本
网络协议·rpc·dubbo
西岭千秋雪_19 天前
Dubbo应用篇
java·微服务·dubbo
写bug写bug21 天前
一文搞懂分布式服务发布和引用(Dubbo 案例解读)
java·后端·dubbo
运维&陈同学21 天前
【Dubbo03】消息队列与微服务之dubbo-admin 二进制与编译安装
linux·运维·服务器·后端·微服务·云原生·架构·dubbo