分布式项目

分布式项目

  • 编码问题
    • 编写common(公共项目)
    • 编写provider(提供者)
    • 编写customer(消费者)

编码问题

编写common(公共项目)

  1. 创建Maven项目 quickstart

  2. pom文件 添加上传私服地址


    
      fengqing
      http://nexus.local.com/nexus/content/repositories/fengqing/
    
    
      snapshots
      http://localhost:8081/nexus/content/repositories/snapshots/
    
  
  1. 编写bean 不要忘记
    implements Serializable

  2. 编写service接口

编写provider(提供者)

  1. 创建spring-boot项目
  2. 编写pom文件(添加依赖)
  
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
            mysql
            mysql-connector-java
            5.1.22
        
       
            com.alibaba
            dubbo
            2.8.4
        


        
            io.dubbo.springboot
            spring-boot-starter-dubbo
            1.0.0
        
        
            cn.m2
            qqcommon
            1.0-SNAPSHOT
        
		
            log4j
            log4j
            1.2.16
            compile
        
  1. application.properties编写
server.port=8093
spring.dubbo.application.name=spring-boot-starter-dubbo-demo-provider
spring.dubbo.registry.address=zookeeper://localhost:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=cn.m2.qqprovider


spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bbs
spring.datasource.username=root
spring.datasource.password=123

##指定mpper文件的位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
##为实体类起别名
mybatis.type-aliases-package=cn.m2.bean
  1. 编写mapper接口
  2. 编写mapper.xml文件


  1. 编写service实现(对公共类接口进行实现)
    类上的注解导入的包是import com.alibaba.dubbo.config.annotation.Service;
    方法上的注解导入的是import javax.annotation.Resource;
@Service(interfaceClass = cn.m2.service.RpcBbsSortService.class)
public class BbsSortServiceImpl implements RpcBbsSortService {

    @Resource
    private BbsSortMapper bbsSortMapper;
  1. 最后不要忘记在main方法上面加上注解MapperScan 用来扫描mapper包下的注解
@SpringBootApplication
@MapperScan("cn.m2.qqprovider.mapper")
public class QqproviderApplication {

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

}

编写customer(消费者)

  1. 创建spring-boot项目
  2. 编写pom文件(添加依赖)
  
            com.alibaba
            dubbo
            2.8.4
        

        
            io.dubbo.springboot
            spring-boot-starter-dubbo
            1.0.0
        

        
            cn.m2
            qqcommon
            1.0-SNAPSHOT
        

        
            com.alibaba
            fastjson
            1.2.28
        
  1. application.properties编写
spring.dubbo.application.name=spring-boot-starter-dubbo-demo-consumer
spring.dubbo.registry.address=zookeeper://localhost:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.scan=cn.m2.qqcustomer
  1. 编写service接口
  2. 编写service实现(对本地接口进行实现 内部调用公共类接口进行实现)
    类上的注解调用的是import org.springframework.stereotype.Service;
    属性上的注解调用的是com.alibaba.dubbo.config.annotation.Reference;
@Service("bbsSortService")
public class BbsSortServiceImpl implements BbsSortService {

    @Reference
    public RpcBbsSortService rpcBbsSortService;
  1. 编写controller 测试运行
package cn.m2.qqcustomer.controller;

import cn.m2.bean.BbsSort;
import cn.m2.qqcustomer.service.BbsSortService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("bbsSort")
public class BbsSortController {

    @Autowired
    private BbsSortService bbsSortService;

    @RequestMapping("/list")
    public String list() {
        List bbsSortList = bbsSortService.list();
        String bbsSortJSON = JSONArray.toJSONString(bbsSortList);
        System.out.println(bbsSortJSON);
        return bbsSortJSON;
    }
}

测试运行先运行zookeeper,再运行dubbo部署的tomcat,运行提供者,运行消费者

你可能感兴趣的:(分布式项目)