spring boot 集成 dubbo 2.X

原创
10/14 20:09
阅读数 31

一、依赖准备  pom.xml 配置

	<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.8</version>
	</dependency>


	<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.11</version>
	</dependency>

 

二、服务端代码

(1)api准备

package com.liveme.dubbo.api;

public interface SayHello {

	public String sayHello(String name);
	
}

实现类

package com.liveme.dubbo.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.liveme.dubbo.api.SayHello;

@Service
public class SayHelloImpl implements SayHello {

	public String sayHello(String name) {
		System.out.println("greeting service received: " + name);
		return "hello :" + name;
	}

}

 

启动类

package com.liveme;

import java.util.concurrent.CountDownLatch;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

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

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws InterruptedException {

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				ProviderConfiguration.class);
		context.start();

		System.out.println("dubbo service started");
		new CountDownLatch(1).await();

	}

	@Configuration
	@EnableDubbo(scanBasePackages = "com.liveme.dubbo.impl")
	@PropertySource("classpath:/dubbo-provider.properties")
	static class ProviderConfiguration {
	}
}

 

配置文件:

#
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements.  See the NOTICE file distributed with
#   this work for additional information regarding copyright ownership.
#   The ASF licenses this file to You under the Apache License, Version 2.0
#   (the "License"); you may not use this file except in compliance with
#   the License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#
dubbo.application.name=zookeeper-demo-provider
#dubbo.registry.address=zookeeper://${zookeeper.address:10.61.153.47}:${zookeeper.port:2181}
dubbo.registry.address=zookeeper://10.61.153.47:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.provider.token=false

 

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部