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

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

1)父工程pom.xml



  4.0.0

  com.imooc
  dubbo-demo
  pom
  1.0-SNAPSHOT

  
    UTF-8
    11
    11
  

  
    api
    consumer
    provider
  



2)api

pom.xml



  4.0.0

  com.imooc
  dubbo-demo-api
  1.0-SNAPSHOT

  
    UTF-8
    11
    11
  


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




  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.3.RELEASE
  
  4.0.0

  com.imooc
  dubbo-demo-provider

  
    UTF-8
    11
    11
  

  
    
      io.dubbo.springboot
      spring-boot-starter-dubbo
      1.0.0
    
    
      com.imooc
      dubbo-demo-api
      1.0-SNAPSHOT
    
    
      org.springframework.boot
      spring-boot-starter-web
      RELEASE
      compile
    


  


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)";
    }

}

运行:

【SpringBoot+dubbo+zk】实现服务之间rpc通信_第1张图片

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

【SpringBoot+dubbo+zk】实现服务之间rpc通信_第2张图片

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

总结dubbo的使用:

1.api中写接口

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

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

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

你可能感兴趣的:(#,rpc,dubbo)