dubbo集成spring-boot

    dubbo需要一个注册中心,用的是zookeeper。集成spring-boot是为了方便使用注解开发。

 

 

    1. 安装zoopkker

     hadoop01:2181, hadoop02:2181, hadodp03:2181

 

 

    2. 编写dubbo公共接口

    HelloService

package com.dubbo.service;

import com.dubbo.model.User;

public interface HelloService {
	
	
	String sayHello(String name);

}

 

 

 

    3. 编写provider 并注册到zookeeper

      这里需要注意的是:接口实现类上面标的注解@Service是 com.alibaba.dubbo.config.annotation.Service;

      还要在启动类上面加上一个注解:@DubboComponentScan

      3.1 HelloService实现类

package com.dubbo.provider.service;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.service.HelloService;

@Service
public class HelloServiceImpl implements HelloService {

	private static Logger LOGGER = LoggerFactory.getLogger(HelloServiceImpl.class);

	@Override
	public String sayHello(String name) {

		LOGGER.info("request from consumer...param...{}", name);

		return "hello " + name;
	}


}

    3.2 dubbo配置类

 

 

package com.dubbo.provider.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;

@Configuration
public class ProviderConf {

	// 
	// 

	@Bean
	public ApplicationConfig applicationConfig() {
		ApplicationConfig applicationConfig = new ApplicationConfig();
		applicationConfig.setName("provider-test");
		return applicationConfig;
	}

	@Bean
	public RegistryConfig registryConfig() {
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setAddress("zookeeper://hadoop01:2181?backup=hadoop02:2181,hadoop03:2181");
		registryConfig.setClient("zkclient");
		return registryConfig;
	}

	@Bean
	public ProtocolConfig protocolConfig() {
		ProtocolConfig protocolConfig = new ProtocolConfig();
		protocolConfig.setName("dubbo");
		protocolConfig.setPort(20880);
		return protocolConfig;
	}

}

    3.3 provider启动类

 

 

package com.dubbo.provider;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;

@SpringBootApplication
@DubboComponentScan(basePackages = { "com.dubbo.provider.service" })
public class Provider {

	private static Logger LOGGER = LoggerFactory.getLogger(Provider.class);
	
	public static void main(String[] args) throws IOException {

		LOGGER.debug("startup provider...");

		SpringApplication.run(Provider.class, args);

		System.in.read();

	}

}

 

 

    

    4. 编写consumer调用服务

    这里需要注意:注入远程HelloService对象的时候,要用@Reference,不是用@Autowired,因为dubbo目前是使用@Reference来做的,底层用了ReferenceAnnotationBeanPostProcessor来注入@Reference标注的属性。

    4.1 controller调用远程HelloService服务

 

package com.dubbo.consumer.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.service.HelloService;

@RestController
@RequestMapping(path = "/hello", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class HelloController {

	@Reference
	private HelloService helloService;

	@RequestMapping(path = "/say", method = RequestMethod.GET)
	public String sayHello(String name) {
		return helloService.sayHello(name);
	}

}

    4.2 consumer配置类

package com.dubbo.consumer.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;

@Configuration
public class ConsumerConf {

	@Bean
	public ApplicationConfig applicationConfig() {
		ApplicationConfig applicationConfig = new ApplicationConfig();
		applicationConfig.setName("consumer-test");
		return applicationConfig;
	}

	@Bean
	public ConsumerConfig consumerConfig() {
		ConsumerConfig consumerConfig = new ConsumerConfig();
		consumerConfig.setTimeout(3000);
		return consumerConfig;
	}

	@Bean
	public RegistryConfig registryConfig() {
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setAddress("zookeeper://hadoop01:2181?backup=hadoop02:2181,hadoop03:2181");
		registryConfig.setClient("zkclient");
		return registryConfig;
	}

}

   4.3 consumer启动类

package com.dubbo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;

@SpringBootApplication
@DubboComponentScan(basePackages = { "com.dubbo.consumer.controller" })
public class Consumer {

	public static void main(String[] args) {

		SpringApplication.run(Consumer.class, args);

	}

}

 

 

   

你可能感兴趣的:(dubbo集成spring-boot)