dubbo实现的微服务案例

这里的生产者,用来对外提供服务,可配置多个同类服务构成服务集群

provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <!-- 配置服务名-->
    <dubbo:application name="demo-provider"/>

    <!-- 配置zookeeper注册地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <!--读物协议为dubbo,端口号,IP地址-->
	<dubbo:protocol name="dubbo" port="20880" host="127.0.0.1"/>

    <!--提供服务的具体实现类-->
	<bean id="testService" class="com.bx.dubbodemo.TestServiceImpl" />
	
    <!--对外提供服务的借口,及接口的实现类-->
	<dubbo:service interface="com.bx.dubbo_api.TestService" ref="testService"></dubbo:service>
   

</beans>

 提供服务的启动类

provider.java

package com.bx.dubbodemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * dubbo集群服务第一个服务
 * @author Administrator
 *
 */
public class Provider {
    /**
     * To get ipv6 address to work, add
     * System.setProperty("java.net.preferIPv6Addresses", "true");
     * before running your application.
     */
    public static void main(String[] args) throws Exception {
        /*加载容器*/
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
        /*启动容器*/
        context.start();
        System.out.println("20880:服务启动……");
        /*使系统阻塞在这里,以提供服务*/
        System.in.read(); // press any key to exit
    }
}

服务接口的实现类

TestServiceImpl.java

package com.bx.dubbodemo;

import com.bx.dubbo_api.TestService;

public class TestServiceImpl implements TestService{

	public String get(String msg) {
		// TODO Auto-generated method stub
		return "20880号技师服务中";
	}

}

服务接口类:TestService.java

package com.bx.dubbo_api;

public interface TestService {
	String get(String msg);
}

下来是消费者的相关配置

consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
     <!--服务名-->
    <dubbo:application name="demo-consumer"/>

    <!-- 要链接的zookeeper地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!--要调用的服务接口 -->
    <dubbo:reference id="testService" check="false" interface="com.bx.dubbo_api.TestService"/>

</beans>

消费类:Consumer.java:

package com.dubbodemo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bx.dubbo_api.TestService;

public class Consumer {
	 public static void main(String[] args) {
	        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml");
	        context.start();
	       TestService demoService = (TestService) context.getBean("testService"); // get remote service proxy
	        String hello = demoService.get("fuwu"); // call remote method
	        System.out.println(hello); // get result
	    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值