1. 서비스 제공자(Provider) 설정
먼저 서비스 제공자 역할을 할 웹 애플리케이션을 생성합니다. 프로젝트 구조는 다음과 같이 구성합니다.
1.1 POM 설정
pom.xml 파일에 필요한 Maven 의존성을 추가합니다.
<dependencies>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- Dubbo Core -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<!-- ZooKeeper Client -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-provider-app</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port> <!-- Tomcat 포트 -->
<path>/</path> <!-- 웹 애플리케이션 경로 -->
</configuration>
</plugin>
</plugins>
</build>
1.2 서비스 인터페이스 정의
외부에 노출할 서비스 인터페이스를 정의합니다.
package com.example.service;
// 제공할 서비스 인터페이스
public interface GreetingService {
// 서비스 메소드
String greet(String name);
}
1.3 서비스 인터페이스 구현
정의된 인터페이스를 실제 로직으로 구현합니다.
package com.example.service.impl;
import com.example.service.GreetingService;
public class GreetingServiceImpl implements GreetingService {
@Override
public String greet(String name) {
return "Hello, " + name + " from Dubbo!";
}
}
1.4 Dubbo 서비스 설정 (XML)
Spring Bean 설정 파일(예: dubbo-provider.xml)에 Dubbo 서비스 설정을 추가합니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1. 애플리케이션 정보 설정 -->
<dubbo:application name="dubbo-greeting-provider"/>
<!-- 2. ZooKeeper 등록 센터 설정 -->
<dubbo:registry protocol="zookeeper" address="your_zookeeper_host:2181"/>
<!-- 3. Dubbo 프로토콜 및 포트 설정 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 4. 서비스 인터페이스 등록 및 구현 빈 참조 -->
<bean id="greetingService" class="com.example.service.impl.GreetingServiceImpl"/>
<dubbo:service interface="com.example.service.GreetingService" ref="greetingService" timeout="30000"/>
</beans>
참고: your_zookeeper_host:2181 부분을 실제 ZooKeeper 서버 주소로 변경해야 합니다.
1.5 Web.xml 설정
web.xml 파일에 Spring Context Loader Listener를 설정하여 Dubbo 설정을 로드합니다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Spring 설정 파일 로드 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Maven의 Tomcat 플러그인을 사용하여 애플리케이션을 배포하고 실행합니다.
2. Dubbo 관리자 콘솔
Dubbo 관리자 콘솔은 등록된 서비스 및 제공자 정보를 시각적으로 확인할 수 있는 웹 기반 도구입니다. 사용 방법은 다음과 같습니다.
- 현재 실행 중인 서비스 제공자 애플리케이션을 중지합니다.
- Dubbo 관리자 애플리케이션 WAR 파일을 서버의 Tomcat
webapp디렉토리에 배포합니다. - Tomcat 서버를 다시 시작합니다.
- 웹 브라우저를 통해
http://server-ip:8080/dubbo-admin/(버전에 따라 경로가 다를 수 있습니다)로 접속합니다. - 기본 로그인 정보 (ID/PW:
root/root)로 접속합니다.
3. 서비스 소비자(Consumer) 설정
서비스를 호출할 클라이언트 애플리케이션을 설정합니다. 일반적인 Java 프로젝트로 구성할 수 있습니다.
3.1 POM 설정
서비스 제공자와 유사하게 필요한 Maven 의존성을 추가합니다.
<dependencies>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- Dubbo Core -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<!-- ZooKeeper Client -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
3.2 서비스 인터페이스 준비
서비스 소비자는 제공자 측에서 정의한 서비스 인터페이스(com.example.service.GreetingService)가 필요합니다. 이는 별도의 모듈로 관리하거나 공유 라이브러리로 제공될 수 있습니다.
3.3 Spring 서비스 연동 설정 (XML)
Spring Bean 설정 파일(예: dubbo-consumer.xml)에 Dubbo 서비스 참조 설정을 추가합니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1. 애플리케이션 정보 설정 -->
<dubbo:application name="dubbo-greeting-consumer"/>
<!-- 2. ZooKeeper 등록 센터 설정 -->
<dubbo:registry protocol="zookeeper" address="your_zookeeper_host:2181"/>
<!-- 3. 원격 서비스 참조 설정 -->
<dubbo:reference interface="com.example.service.GreetingService" id="greetingService"/>
</beans>
참고: your_zookeeper_host:2181 부분을 실제 ZooKeeper 서버 주소로 변경해야 합니다.
3.4 서비스 호출 테스트
애플리케이션 메인 클래스에서 Spring 컨텍스트를 로드하고 참조된 Dubbo 서비스를 호출합니다.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.service.GreetingService;
public class ConsumerApp {
public static void main(String[] args) {
// Spring 컨텍스트 로드
ApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
// Dubbo 서비스 빈 참조
GreetingService greetingService = (GreetingService) context.getBean("greetingService");
// 원격 서비스 호출
String result = greetingService.greet("World");
System.out.println("Service Response: " + result);
}
}