spring boot集成dubbo实现rpc调用

dubbo官网: http://dubbo.apache.org
dubbo github 源码地址:https://github.com/apache/incubator-dubbo
dubbo 运维项目源码地址:https://github.com/apache/incubator-dubbo-ops

 

首先,zookeeper的安装过程省略,方法翻看以往博客!!!

其次,新建一个maven空项目,然后在此空项目内新建3个module,分别为numberone(服务提供方的纯接口项目),allnumberone(服务消费方),one(服务提供方的接口具体实现项目)。

新建maven空项目方法略。

一:numberone项目代码:

public interface Cost {

        /**
         * 增加接口
         * @return
         */
        Integer add(Integer a);
}

以及maven依赖



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.2.6.RELEASE
		 
	
	com.amos.china
	numberone
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


二:服务消费项目代码:

接口:

package com.example.demo.service;

public interface GetProduct {

    /**
     * 获得总消费
     * @return
     */
    Integer getCost(Integer a);
}

实现类:

package com.example.demo.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.service.Cost;
import com.example.demo.service.GetProduct;

@Service
public class GetProImpl  implements GetProduct {

    @Reference
    private Cost costService;

    @Override
    public Integer getCost(Integer a) {
        return costService.add(a);
    }
}

controller:

package com.example.demo.controller;

import com.example.demo.service.GetProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProController {

    @Autowired
    private GetProduct getProduct;

    @RequestMapping("/add")
    public String getCost(Integer a){
        return "该产品总共消费 :"+getProduct.getCost(a);
    }

}

依赖pom:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.2.6.RELEASE
		 
	
	com.china
	allnumberone
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.projectlombok
			lombok
			true
		

		
		
			com.amos.china
			numberone
			0.0.1-SNAPSHOT
			compile
		
		
		
			com.alibaba.boot
			dubbo-spring-boot-starter
			0.2.0
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


dubbo配置文件:application.yml

dubbo:
  application:
    name:  dubbo-consumer
  registry:
    address: 106.54.220.69:2181
    # 读者请换成自己的zookeeperip
    protocol: zookeeper
    check: false
  monitor:
    protocol: register
  consumer:
    check:  false
    timeout: 3000

server:
  port: 8062

三:接口提供方实现类项目one:

依赖:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.2.6.RELEASE
		 
	
	com.china
	one
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			com.amos.china
			numberone
			0.0.1-SNAPSHOT
			compile
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
		
			com.alibaba.boot
			dubbo-spring-boot-starter
			0.2.0
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


实现类:

package com.china.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.service.Cost;

@Service
public class GetImpl implements Cost {

    @Override
    public Integer add(Integer a) {
        return a;
    }
}

dubbo配置文件:application.yml

dubbo:
  application:
    name: dubbo-provider
  registry:
    address: 106.54.220.69:2181
    # 读者请自行更改zookeeper地址
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: 30003
  monitor:
    protocol: register
  consumer:
    check: false
    timeout: 3000

server:
  port: 8061

四:完成以上操作后,启动allnumberone(服务消费)项目,以及one(服务提供方实现类)项目,在浏览器中访问 http://localhost:8062/add?a=100

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