Dubbo-Dependency
生活随笔
收集整理的這篇文章主要介紹了
Dubbo-Dependency
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Dubbo - 當當網?
public interface DependencyService {public String dependency() throws Exception; } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import bhz.dubbo.dependency.provider.DependencyService; import bhz.dubbo.sample.provider.SampleService; @Service("dependencyServiceImpl") @com.alibaba.dubbo.config.annotation.Service(interfaceClass=com.dubbo.dependency.provider.DependencyService.class, protocol={"dubbo"}, retries=0) public class DependencyServiceImpl implements DependencyService {//注入SampleService@Autowiredprivate SampleService sampleService;public String dependency() throws Exception {//這里 我們可能需要調用SampleService,也可能不需要...System.out.println(sampleService.sayHello("jack"));return "dependency exec";}}啟動時檢查?
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:annotation package="com" /><!-- 提供方應用信息,用于計算依賴關系 --><dubbo:application name="dependency-provider" /><!-- 使用zookeeper注冊中心暴露服務地址 --><dubbo:registry address="zookeeper://localhost:2181" /><!-- 用dubbo協議在20880端口暴露服務 --><dubbo:protocol name="dubbo" port="20890" /><!-- 注意這里,我們在使用DependencyService的時候,這個服務可能需要依賴某一個服務,比如SampleService 檢查級聯依賴關系 默認為true 當有依賴服務的時候,需要根據需求進行設置 --><dubbo:reference id="sampleService" check="true"interface="com.dubbo.sample.provider.SampleService" /></beans> import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "sample-provider.xml" });context.start(); System.in.read(); // 為保證服務一直開著,利用輸入流的阻塞來模擬} } import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) {try {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dependency-provider.xml" });context.start();System.in.read(); // 為保證服務一直開著,利用輸入流的阻塞來模擬} catch (Exception e) {e.printStackTrace();}} } import org.springframework.context.support.ClassPathXmlApplicationContext;import bhz.dubbo.dependency.provider.DependencyService;public class Consumer {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dependency-consumer.xml" });context.start();DependencyService dependencyService = (DependencyService) context.getBean("dependencyService");//調用該服務System.out.println(dependencyService.dependency());System.in.read();}}?
總結
以上是生活随笔為你收集整理的Dubbo-Dependency的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dubbo-HelloWorld
- 下一篇: Dubbox-REST风格