doubbo实例

一、创建生产者

  yml文件 

dubbo:
application:
name: user-service-provider
registry:
address: ip地址:2181 //zookeeper地址 如为集群则中间用逗号隔开
protocol: zookeeper
timeout: 20000
protocol:
name: dubbo
port: 20882
monitor:
protocol: registry
config-center:
timeout: 20000 
  
  pom文件
<dubbo.version>2.7.5</dubbo.version>
<curator.version>2.12.0</curator.version>

<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>

生产者代码

 
package com.web.oneself.service;

import com.web.oneself.dao.SysUser;



public interface SysUserService {
    public SysUser getUser();
}
package com.web.oneself.service.Impl;


import org.apache.dubbo.config.annotation.Reference;
import com.web.oneself.dao.SysUser;
import com.web.oneself.mapper.SysUserMapper;
import com.web.oneself.service.SysUserService;
import com.web.oneself.util.LogUtilManager;
import org.apache.dubbo.config.annotation.Service;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Service  //该service为dubbo的注解
@Component
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public void getUser() {
        System.out.println("调用成功!");
    }
}
package com.web.oneself;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@MapperScan("com.web.oneself.mapper")  //相当于@Mapper
@SpringBootApplication
//将路径下的bean都扫描到spring容器中
@ComponentScan("com.web")
@EnableDubbo(scanBasePackages="com.web.oneself.service.Impl")
/*@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)*/  //不添加数据库启动springboot
public class OneselfApplication {

    public static void main(String[] args) {
        SpringApplication.run(OneselfApplication.class, args);
    }

}

  创建消费者

  pom文件相同,yml文件如下

dubbo:
application:
name: order-service-consumer
registry:
address: zookeeper://47.107.170.12:2181
timeout: 20000
monitor:
protocol: registry
config-center:
timeout: 20000

  消费者代码如下
package com.web.oneself.service;

import com.web.oneself.SysUser;

public interface OrderService {
    public SysUser getUser();
}
package com.web.oneself.service.impl;

import com.web.oneself.SysUser;
import com.web.oneself.service.OrderService;


import com.web.oneself.service.SysUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Reference(loadbalance="random",timeout=1000) //从doubbo获取注入
    SysUserService SysUserService;
    @Override
    public SysUser getUser() {
        return SysUserService.getUser();
    }
}
package com.web.oneself;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@EnableDubbo
/*@SpringBootApplication*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DubboApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboApplication.class, args);
    }

}
package com.web.oneself.controller;
import com.web.oneself.service.OrderService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @Autowired
    OrderService orderService;

    @ResponseBody
    @RequestMapping(value = "/hi", method = RequestMethod.POST)
    public void sayHello() {orderService.getUser();
    }
}

  启动俩个程序则能开始调用,注意俩边service路径相同。

  

posted @ 2020-10-12 10:48  MAX+  阅读(126)  评论(0)    收藏  举报