定义的Service(Api和Xml方式)
public interface UserService {
String getUsername();
}
public class UserServiceImpl implements UserService {
@Override
public String getUsername() {
System.out.println("调用getUsername=========");
return "pine";
}
}
1.直接调用Api
生产者
public class ProviderApi {
public static void main(String[] args) throws IOException {
ServiceConfig<UserService> config = new ServiceConfig<>();
config.setApplication(new ApplicationConfig("api-provider"));
config.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
config.setInterface(UserService.class);
config.setRef(new UserServiceImpl());
config.export();
System.out.println("first-dubbo-provider is running.");
//等待控制台输入
System.in.read();
}
}
消费者
public class ConsumerApi {
public static void main(String[] args) {
ReferenceConfig<UserService> reference = new ReferenceConfig<>();
reference.setApplication(new ApplicationConfig("api-consumer"));
reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
reference.setInterface(UserService.class);
UserService orderService = reference.get();
String username = orderService.getUsername();
System.out.println("result: " + username);
}
}
2.xml配置文件配置
生产者
public class ProviderXml {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/dubbo-server.xml");
ctx.start();
System.out.println("---------dubbo启动成功--------");
// 保证服务一直开着
synchronized (ProviderXml.class) {
try {
ProviderXml.class.wait();
} catch (Throwable e) {
}
}
}
}
dubbo-server.xml
<?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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--全局配置-->
<dubbo:provider timeout="3000" />
<!-- 服务提供方应用名称, 方便用于依赖跟踪 -->
<dubbo:application name="xml-server" />
<!-- 使用本地zookeeper作为注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--name指示使用什么协议监听端口:dubbo/rmi/rest-->
<dubbo:protocol name="dubbo" port="20882" />
<!-- 通过xml方式配置为bean, 让spring托管和实例化 -->
<bean id="userService" class="com.pine.service.provider.UserServiceImpl"/>
<!-- 声明服务暴露的接口,并暴露服务 -->
<dubbo:service interface="com.pine.service.provider.UserService" ref="userService" />
</beans>
消费者
public class ConsumerXml {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/dubbo-client.xml");
ctx.start();
System.out.println("---------dubbo启动成功--------");
// get remote service proxy
UserService orderService = (UserService) ctx.getBean("userService");
String username = orderService.getUsername();
System.out.println(" ConsumerXml result: " + username);
}
}
dubbo-client.xml
<?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">
<dubbo:application name="xml-clint" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:reference id="userService" interface="com.pine.service.provider.UserService" />
</beans>
3.注解方式
生产者
public class ProviderPro {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.out.println("dubbo 启动成功");
System.in.read();
}
@Configuration
@EnableDubbo(scanBasePackages = "com.pine.service.provider")
@PropertySource("classpath:/dubbo-provider.properties")
static class ProviderConfiguration {
}
}
provider包
public interface UserService {
String getUsername();
}
//这里的@Service是dubbo的service
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public String getUsername() {
System.out.println("调用getUsername=========");
return "pine";
}
}
dubbo-provider.properties
dubbo.application.name=pro-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20883
消费者
public class ConsumerPro {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
ConsumerUserServiceImpl serviceConsumer = context.getBean(ConsumerUserServiceImpl.class);
String username = serviceConsumer.getUsername();
System.out.println("result: " + username);
}
@Configuration
@EnableDubbo(scanBasePackages = "com.pine.service.consumer")
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(value = {"com.pine.service.consumer"})
static class ConsumerConfiguration {
}
}
consumer包
import com.alibaba.dubbo.config.annotation.Reference;
import com.pine.service.provider.UserService;
import org.springframework.stereotype.Component;
@Component("annotatedConsumer")
public class ConsumerUserServiceImpl {
@Reference
private UserService userService;
public String getUsername(){
System.out.println("调用到ConsumerUserServiceImpl.getUsername()");
return userService.getUsername();
}
}
dubbo-consumer.properties
dubbo.application.name=pro-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=3000