编写pom.xml继承SpringBoot父项目。
对于dubbo的配置,如果是ssm项目需要一个比较麻烦的步骤,具体的可以去查.springboot直接添加依赖即可
zookeeper 去了哪里,被最后两个依赖封装了,在里面.直接用就行
所以配置一个带web的一个不带web,就是为了给孩子用嘛
<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.0modelVersion>
<groupId>org.zhen77groupId>
<artifactId>DubboParentartifactId>
<packaging>pompackaging>
<version>1.0.0version>
<modules>
<module>apimodule>
<module>providermodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.10.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.2.0version>
dependency>
dependencies>
dependencyManagement>
project>
之后所有的都是module模块
创建cn.zhen77.dubbo.service.DemoDubboService接口
记不记得当时手写rpc的时候我们遇到了麻烦,就是service层和provider层的方法命名冲突
我们当时一个叫service,一个是My…service,既然用Dobbo就用其命名就好了;
public interface DemoDubboService {
public String demo(String param);
}
注意:不需要依赖web,如果依赖spring-boot-start-web还需要考虑端口问题。
<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>
<artifactId>DubboParentartifactId>
<groupId>org.zhen77groupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>providerartifactId>
<dependencies>
<dependency>
<groupId>org.zhen77groupId>
<artifactId>apiartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
dependency>
dependencies>
project>
新建application.yml
dubbo:
application:
name: dubbo-cunsumer
registry:
address: zookeeper://192.168.91.128:2181
protocol:
port: 20884
新建cn.zhen77.dubbo.service.impl.DemoDubboServiceImpl
注意:注解是apache的注解。是区分Provider还是Consumer的重要依据
package cn.zhen77.dubbo.service.impl;
import cn.zhen77.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;
/**
* @author : zhen77
* @date: 2021/3/14 - 03 - 14 - 22:09
* @Description: cn.zhen77.dubbo.service.impl
* @version: 1.0
* 重点来了 ,拿小本本记号,这里是接口的实现类没错吧,一定要加@Service
* 但是 一定要不要加入Spring的@Service 一定要加入 dubbo的@Service
* 到了Consumer去添加spring的注解
* 只有这样,才能到注册中心去注册方法;
* 并且,启动器还要实现@enableDubbo注解
*/
@Service
public class DemoDubboServiceImpl implements DemoDubboService {
@Override
public String demo(String param) {
System.out.println("执行demo....");
return param+"123";
}
}
新建cn/zhen77/ProviderApplication.java
必须要有@EnableDubbo注解,否则Dubbo不生效。
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
创建consumer项目
<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>
<artifactId>DubboParentartifactId>
<groupId>org.zhen77groupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>demoConsumerartifactId>
<dependencies>
<dependency>
<groupId>org.zhen77groupId>
<artifactId>apiartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
dependency>
dependencies>
project>
新建application.yml
dubbo:
application:
name: dubbo-cunsumer
registry:
address: zookeeper://192.168.91.128:2181
新建cn.zhen77.service.DemoService
新建cn.zhen77.service.impl.DemoServiceImpl
调用服务使用@Reference注解,不要倒错包了,有两个。
public interface DemoService {
String consumerDemo();
}
package cn.zhen77.service.impl;
import cn.zhen77.dubbo.service.DemoDubboService;
import cn.zhen77.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
/**
* @author : zhen77
* @date: 2021/3/14 - 03 - 14 - 22:43
* @Description: cn.zhen77.service.impl
* @version: 1.0
*/
/*一定要从spring中去注入
* 现在使用dubbo有一些变化了,以前我们使用注解注入的时候在创建对象时使用的是@Autowired
* 而现在替换成@reference 而且是apache dubbo下的 这个注解简化了RMI远程调用的代码
* */
@Service
public class DemoServiceImpl implements DemoService {
@Reference
private DemoDubboService demoDubboService;
@Override
public String consumerDemo() {
return demoDubboService.demo("哈喽");
}
}
新建控制器cn.zhen77.controller.DemoController
package cn.zhen77.controller;
import cn.zhen77.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping("/demo")
@ResponseBody
public String demo(){
String result = demoService.consumerDemo();
return result;
}
}
新建com.msb.ConsumerApplication
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
先开启linux系统,连接Xshell, 运行zookeeper,启动Provider,再启动Consumer
结果如下