Dubbo

dubbo的三個核心功能。面向接口的遠程方法調用,智能容錯和負載均衡,以及服務自動化實現。Dubbo是一個分佈式服務框架。致力於提供高性能和透明化的RPC遠程服務調用方法和治理方案
服務提供者
服務消費者
註冊中心
監控中心
下面通過一個例子簡單模擬一下Dubbo使用,其實學習一款新的框架最快的就是做一個小項目
模擬一個顧客買一個商品,使用main主函數來模擬web項目
1、創建一個服務提供者
link-orderservice-provider

//Order  類實現序列化接口
public class Order implements Serializable {
     private  String  id;
     private  String name;
     private  Float price;
     private  Integer amount;
     getxxx()...
     setxxx()...
     toString()...
     
public class OrderServiceImpl implements OrderService {

    @Override
    public Order createOrder(Integer userId, String name, float price, Integer amount) {
        System.out.println("服務提供者OrderService :給UserId" + userId +"生成訂單");
        Order order = new Order();
        order.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        order.setAmount(amount);
        order.setName(name);
        order.setPrice(price);
        return order;
    }
}

服務提供者的配置文件


   <!--服務提供者的配置文件 -->
    <!--聲明服務的名稱  供dubbo框架內部使用-->
    <dubbo:application name="link-orderservice-provider" />

    <!--聲明使用 的協議 訪問接口用什麼協議
        name : 協議名稱
        port 端口號 默認是20880
    -->

    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    <!--聲明服務接口  暴露服務消費者才能使用-->

    <!--registry:是否使用註冊中心(使用直連方式,不用註冊中心,賦值爲N/A)-->

    <dubbo:service interface="com.bjpowernode.service.OrderService"
                   ref="orderservice" registry="N/A">

    </dubbo:service>

    <!--聲明暴露的接口實現類對象-->

    <bean id="orderservice" class="com.bjpowernode.service.impl.OrderServiceImpl"></bean>
</beans>

主方法運行服務提供者

public class OrderServiceApplication {

    public static void main(String[] args) throws IOException {
        String config = "orderservice-provider.xml";

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        ctx.start();

        //要讓提供者處於運行狀態 , 消費者才能訪問
        System.in.read();
    }
}

運行時需要保持服務提供者處於運行狀態

2、服務的消費者(顧客)
這裏的服務消費者使用的是maven工程,需要在pomxml的導入我們上面的服務提供者的依賴。

	.....
 <dependency>
      <groupId>com.bjpowernode</groupId>
      <artifactId>link-orderservice-provider</artifactId>
      <version>1.0.0</version>
    </dependency>
    ......

調用OrderService中的createOrder方法。創建交易購買的商品信息

public class ShopServiceimpl implements ShopService {

    private OrderService orderService;

    public ShopServiceimpl setOrderService(OrderService orderService) {
        this.orderService = orderService;
        return this;
    }

    @Override
    public Order buyGoods(Integer userId, String goodName, float price, Integer amount) {
        System.out.println("消費者調用遠程方法創建訂單 userId :" + userId );
        Order order = orderService.createOrder(userId, goodName, price, amount);

        return order;
    }
}

服務消費者的Dubbo的配置文件


<!--服務的名稱-->
<dubbo:application name="link-main-web"></dubbo:application>

    <dubbo:reference interface="com.bjpowernode.service.OrderService"
            id="remoteOrderService"
            url="dubbo://localhost:20880"
            registry="N/A"
    ></dubbo:reference>

    <!--聲明shopService對象-->
    <bean id="shopService" class="com.bjpowernode.service.impl.ShopServiceimpl">
        <property name="orderService" ref="remoteOrderService" />
     </bean>

消費者依然使用main方法模擬web

public class ConsumeApplication {

    public static void main(String[] args) {

        String config  = "show-consume.xml";

        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        ((ClassPathXmlApplicationContext) ctx).start();

        ShopService service = (ShopService) ctx.getBean("shopService");

        Order order = service.buyGoods(1, "iPai", 5000f, 1);

        System.out.println("消費者購買的商品信息爲 order"+ order);
6
.....................
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章