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

发布时间:2024年01月04日

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

文章来源:https://blog.csdn.net/themagickeyjianan/article/details/135391612
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。