dubbo 快速啟動-結(jié)合spring注解使用做微服務(wù)

開始

是在之前的項目上
https://blog.csdn.net/ko0491/article/details/85166785

provider更改

新建spring.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 當(dāng)我們需要使用 注解時 開啟注解 -->
    <context:annotation-config />
    <!--配置掃描包 -->
    <context:component-scan base-package="com.ghgcn.dubbo.service" />
        <!--導(dǎo)入dubbo配置文件 -->
    <import resource="service-provider.xml"/>
</beans>

service-provider改變

<?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">

    <dubbo:application name="demo-provider" />

    <!-- 注冊中心地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />

    <!-- 聲明協(xié)議與商品 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 聲明服務(wù)的實現(xiàn)類 舊的方式-->
    <!-- <bean id="greetingService" class="com.ghgcn.dubbo.service.impl.GreetingServiceImpl" 
        /> -->

    <!-- 聲明服務(wù) spring -->
    <dubbo:service interface="com.ghg.dubbo.api.GreetingService"
        ref="greetingService" protocol="dubbo" validation="false" />
</beans>
在這里插入圖片描述

在provicer實現(xiàn)的service上加注解@Servcie


在這里插入圖片描述
package com.ghgcn.dubbo.service.impl;

import org.springframework.stereotype.Service;

import com.ghg.dubbo.api.GreetingService;

@Service("greetingService") //名稱
public class GreetingServiceImpl implements GreetingService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }

}

main啟動類改變

第一版

package com.ghgcn.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

   public static void main(String[] args) throws IOException {
       // com.alibaba.dubbo.container.Main.main(args);
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
               new String[]{ "META-INF/spring/service-provider.xml" });
       context.start();
       System.out.println("provider啟動了");
       System.in.read(); // press any key to exit
   }

}

第二版

package com.ghgcn.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) throws IOException {
        // com.alibaba.dubbo.container.Main.main(args);
        //
        // ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
        // new String[]{ "META-INF/spring/service-provider.xml" });
        /**
         * 使用spring文件
         */
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{ "META-INF/spring/spring.xml" });
        context.start();
        System.out.println("provider啟動了");
        System.in.read(); // press any key to exit
    }

}

在這里插入圖片描述

正常啟動

consumer改造

新建spring.xml


在這里插入圖片描述

spring.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 當(dāng)我們需要使用 注解時 開啟注解 -->
    <context:annotation-config />
    <!--配置掃描包 -->
    <context:component-scan base-package="com.ghgcn.consumer" />
    <!--導(dǎo)入dubbo引入配置文件 -->
    <import resource="dubbo-client.xml" />
</beans>

dubbo-client.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">

    <dubbo:application name="dubbo-consumer"  />

    <!-- 注冊中心 -->
    <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />

    <!-- 聲明要引用的服務(wù) -->
    <dubbo:reference id="greetingService" check="false" interface="com.ghg.dubbo.api.GreetingService" />
</beans>

新建helloService與實現(xiàn)類

在這里插入圖片描述
package com.ghgcn.consumer.service;

public interface HelloService {

    String sayHello(String name);
}

package com.ghgcn.consumer.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ghg.dubbo.api.GreetingService;
import com.ghgcn.consumer.service.HelloService;

/**
 * 加注解service
 * 
 * @author Administrator
 */
@Service
public class HelloServiceImpl implements HelloService {

    /**
     * 可以作 @Resource @Autowired等psring注解
     */
    @Autowired
    private GreetingService greetingService;

    @Override
    public String sayHello(String name) {

        return greetingService.sayHello(name);
    }

}

main方法啟動更改

第一版

package com.ghgcn.consumer;

import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ghg.dubbo.api.GreetingService;

public class Main {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{ "sping/dubbo-client.xml" });
        context.start();
        GreetingService greetingService = (GreetingService) context.getBean("greetingService"); // get remote service

        while (true) {
            try {
                Thread.sleep(1000);
                System.out.println(greetingService.sayHello("劉楠* " + new Date()));
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

        }
    }

}

第二版

package com.ghgcn.consumer;

import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ghgcn.consumer.service.HelloService;

public class Main2 {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{ "sping/spring.xml" });
        context.start();
        HelloService helloService = context.getBean(HelloService.class);

        while (true) {
            try {
                Thread.sleep(1000);
                System.out.println(helloService.sayHello("劉楠* " + new Date()));
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

        }
    }

}


在這里插入圖片描述

啟動正常

在這里插入圖片描述

在這里插入圖片描述

結(jié)合spring可以做微服務(wù)很方便
github :https://github.com/ln0491/dubbo-demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末萎津,一起剝皮案震驚了整個濱河市杆融,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌杈笔,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件累铅,死亡現(xiàn)場離奇詭異跃须,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)娃兽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門菇民,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人投储,你說我怎么就攤上這事第练。” “怎么了玛荞?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵娇掏,是天一觀的道長。 經(jīng)常有香客問我勋眯,道長婴梧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任凡恍,我火速辦了婚禮志秃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嚼酝。我一直安慰自己浮还,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布闽巩。 她就那樣靜靜地躺著钧舌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪涎跨。 梳的紋絲不亂的頭發(fā)上洼冻,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機(jī)與錄音隅很,去河邊找鬼撞牢。 笑死,一個胖子當(dāng)著我的面吹牛叔营,可吹牛的內(nèi)容都是我干的屋彪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼绒尊,長吁一口氣:“原來是場噩夢啊……” “哼畜挥!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起婴谱,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤蟹但,失蹤者是張志新(化名)和其女友劉穎躯泰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體华糖,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡麦向,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了客叉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磕蛇。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖十办,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情超棺,我是刑警寧澤向族,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站棠绘,受9級特大地震影響件相,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜氧苍,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一夜矗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧让虐,春花似錦紊撕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至惭缰,卻和暖如春浪南,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背漱受。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工络凿, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人昂羡。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓絮记,卻偏偏與公主長得像,于是被迫代替她去往敵國和親紧憾。 傳聞我的和親對象是個殘疾皇子到千,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容