【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去引用各个服务的实现即可,实现所谓的微服务。

相关推荐
王道长服务器 | 亚马逊云4 天前
一个迁移案例:从传统 IDC 到 AWS 的真实对比
java·spring boot·git·云计算·github·dubbo·aws
妄小闲7 天前
百度竞价推广:搜索竞价信息流推广代运营
dubbo
Sam-August8 天前
【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者?
java·spring cloud·dubbo
syty20208 天前
K8s是什么
容器·kubernetes·dubbo
程序员小潘8 天前
Dubbo3序列化安全机制导致的一次生产故障
dubbo
LuiChun9 天前
docker desktop更新到【4.45.0 】后,旧容器镜像都丢失了
docker·容器·dubbo
程序员小潘12 天前
Dubbo3.3 Triple协议处理东西向流量
dubbo
敲上瘾13 天前
Docker 容器核心指令与数据库容器化实践
linux·运维·服务器·docker·容器·eureka·dubbo
YF云飞13 天前
Dubbo分布式服务框架全解析
java·dubbo
@才华有限公司14 天前
vscode翻译插件
ide·vscode·dubbo