方法のAPI注釈とプロバイダに

1.provider

輸入com.alibaba.dubbo.config.annotation.Service。
輸入facade.EchoService。
輸入com.alibaba.dubbo.rpc.RpcContext; 
輸入java.text.SimpleDateFormatの。
輸入java.util.Date; 

@Service 
パブリッククラスEchoServiceImpl実装EchoService { 

    パブリック文字列のエコー(文字列メッセージ){ 
    文字列今=新しいてSimpleDateFormat( "HH:MM:SS")形式(新しいDate())。
    System.out.println( "[" +今+ "]ハロー"メッセージ+ + "消費者からの要求" + RpcContext.getContext()getRemoteAddressString()。)。
    メッセージを返します。
    } 
}

 Configurationクラス:

輸入com.alibaba.dubbo.config.ApplicationConfig。
輸入com.alibaba.dubbo.config.ProtocolConfig。
輸入com.alibaba.dubbo.config.ProviderConfig。
輸入com.alibaba.dubbo.config.RegistryConfig。
輸入com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo。
輸入org.springframework.context.annotation.AnnotationConfigApplicationContext; 
輸入org.springframework.context.annotation.Bean。
輸入org.springframework.context.annotation.Configuration。

インポートにjava.io.IOException; 

パブリッククラスAnnotationProvider { 
    公共の静的な無効メイン(文字列[]引数)はIOExceptionが{スロー 
        AnnotationConfigApplicationContextコンテキスト=新しいAnnotationConfigApplicationContext(ProviderConfiguration.class)。
        context.start();
        System.in.read(); 
    } 

    @Configuration 
    @EnableDubbo(scanBasePackages = "プロバイダ")
    静的クラスProviderConfiguration { 
        @Bean 
        公共ProviderConfig providerConfig(){ 
            )(新しいProviderConfigを返します。
        } 

        @Bean 
        公共ApplicationConfig applicationConfig(){ 
            =新しいApplicationConfig ApplicationConfig applicationConfig()。
            applicationConfig.setName(「エコーアノテーション提供者」)。
            applicationConfigを返します。
        } 
        公共RegistryConfig registryConfig(){

        @Bean
            RegistryConfig registryConfig =新しいRegistryConfig(); 
            registryConfig.setProtocol( "飼育係"); 
            registryConfig.setAddress( "localhost"を); 
            registryConfig.setPort(2181)。
            registryConfigを返します。
        } 

        @Bean 
        公共ProtocolConfig protocolConfig(){ 
            =新しいProtocolConfig ProtocolConfig protocolConfig()。
            protocolConfig.setName( "ダボ")。
            protocolConfig.setPort(20880)。
            protocolConfigを返します。
        } 
    } 

}

 開始:

 

 2.同じ消費者:

輸入com.alibaba.dubbo.config.annotation.Reference。
輸入facade.EchoService。
輸入org.springframework.stereotype.Component。

@Component 
パブリッククラスEchoConsumerBasedAnnotation { 
    @Reference 
    プライベートEchoService echoService。

    パブリック文字列エコー(文字列名){ 
        echoService.echo(名前)を返します。
    } 
}

 

輸入com.alibaba.dubbo.config.ApplicationConfig。
輸入com.alibaba.dubbo.config.ConsumerConfig。
輸入com.alibaba.dubbo.config.RegistryConfig。
輸入com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo。
輸入org.springframework.context.annotation.AnnotationConfigApplicationContext; 
輸入org.springframework.context.annotation.Bean。
輸入org.springframework.context.annotation.ComponentScan; 
輸入org.springframework.context.annotation.Configuration。

インポートにjava.io.IOException; 

パブリッククラスAnnotationConsuner {  
    公共の静的な無効メイン(文字列[]引数)はIOExceptionが{スロー
        AnnotationConfigApplicationContextコンテキスト=新しいAnnotationConfigApplicationContext(ConsumerConfiguration.class)。
        context.start();
        EchoConsumerBasedAnnotation echoService = context.getBean(EchoConsumerBasedAnnotation.class)。
        文字列こんにちは= echoService.echo( "Hello World"の); 
        System.out.println( "結果:" +こんにちは)。
        System.in.read(); 
    } 

    @Configuration 
    @EnableDubbo(scanBasePackages = "API")
    @ComponentScan( "API")
    静的クラスConsumerConfiguration { 
        @Bean 
        公共ApplicationConfig applicationConfig(){ 
            ApplicationConfig applicationConfig =新しいApplicationConfig()。
            applicationConfig.setName(「エコー注釈消費者」); 
            applicationConfigを返します。
        }
 
        @Bean 
        公共ConsumerConfig consumerConfig(){
            新しいConsumerConfigを返します(); 
        } 

        @Bean 
        公共RegistryConfig registryConfig(){ 
            =新しいRegistryConfig RegistryConfig registryConfig()。
            registryConfig.setProtocol( "飼育係"); 
            registryConfig.setAddress( "localhost"を); 
            registryConfig.setPort(2181)。
            registryConfigを返します。
        } 
    } 
}

 

 

おすすめ

転載: www.cnblogs.com/lccsblog/p/11521632.html