Spring Boot 集成Dubbo

Spring Boot 集成Dubbo

更多干货

  • 分布式实战(干货)

  • spring cloud 实战(干货)

  • mybatis 实战(干货)

  • spring boot 实战(干货)

  • React 入门实战(干货)

  • 构建中小型互联网企业架构(干货)

  • python 学习持续更新

  • ElasticSearch 笔记

  • kafka storm 实战 (干货)

  • scala 学习持续更新

  • RPC

  • 深度学习

  • GO 语言 持续更新

 

使用Spring Boot 与Dubbo集成.这里使用的是XML而不是注解

代码地址:https://github.com/csy512889371/learndemo/tree/master/ctoedu-springboot-dubbo

  • ctoedu-springboot-dubbo-consumer 消费端
  • ctoedu-springboot-dubbo-provider 提供端

更多干货

  • 分布式实战(干货)
  • spring cloud 实战(干货)
  • mybatis 实战(干货)
  • spring boot 实战(干货)
  • React 入门实战(干货)
  • 构建中小型互联网企业架构(干货)

 

一、提供端

1、pom.xml



    4.0.0

    cn.ctoedu
    ctoedu-springboot-dubbo-provider
    1.0-SNAPSHOT
    jar

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.7.RELEASE
         
    

    
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

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

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

        
            com.alibaba
            dubbo
            2.8.4
            
                
                    spring
                    org.springframework
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.4.6
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        

        
            com.github.sgroschupf
            zkclient
            0.1
        

    

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

2、Application.java

package cn.ctoedu;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;

import java.util.concurrent.CountDownLatch;

@SpringBootApplication
@ImportResource({"classpath:dubbo-provider.xml"})
public class Application {

	private static final Logger logger = Logger.getLogger(Application.class);

	@Bean
	public CountDownLatch closeLatch() {
		return new CountDownLatch(1);
	}

	public static void main(String[] args) throws InterruptedException {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);
//		logger.info("项目启动!");
//		CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class);
//		closeLatch.await();
	}

}

3、ComputeService.java

package cn.ctoedu.service.impl;

import cn.ctoedu.service.ComputeService;


public class ComputeServiceImpl implements ComputeService {

    public Integer add(int a, int b) {
        return a + b;
    }

}

4、application.yml

server:
  port: 8010
#ZooKeeper
dubbo:
  registry:
    address: localhost:2181
logging:
  level:
    root: INFO

5、dubbo-provider.xml




    
    

    
    

    
    

    
    

    
    

二、消费端

1、application.java

package cn.ctoedu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource({"classpath:dubbo-consumer.xml"})
public class Application {

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

}

2、pom.xml



    4.0.0

    cn.ctoedu
    ctoedu-springboot-dubbo-consumer
    1.0-SNAPSHOT
    jar

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.7.RELEASE
         
    

    
        UTF-8
        1.8
    

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

        
            com.alibaba
            dubbo
            2.8.4
            
                
                    spring
                    org.springframework
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.4.6
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        

        
            com.github.sgroschupf
            zkclient
            0.1
        

    

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


3、dubbo-consumer.xml




    
    

    
    

    
    

4、ApplicationTests 测试类

package cn.ctoedu;

import cn.ctoedu.service.ComputeService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {

	@Autowired
	ComputeService computeService;

	@Test
	public void testAdd() throws Exception {
		System.out.println("cn.ctoedu: Dubbo消费结果为:。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。"+computeService.add(100, 200));
		Assert.assertEquals("compute-service:add", new Integer(3), computeService.add(1, 2));
		//Assert.assertEquals("compute-service:add", new Integer(5), computeService.add(1, 2));
	}

}

你可能感兴趣的:(【架构】,【springboot】,【dubbo】,【构建高可用架构】)