spring-boot統合dubb

22231 ワード

spring-boot統合dubb
まず、zookeeperをインストールすることによって、私がここで使っているdockerは、dockerを持っています。この世界は簡単です。
//             ,docker      zookeeper   ,
//             
  docker-compose up -d 
  • compn-appi共通appiモジュール
  • server-providerサービス提供モジュール
  • server-custome消費モジュール
  • 共通アプリモジュール
    ここで簡単に書いたのはハローの使い方です。
        String hello(String message);
    
    サービス提供モジュール
    maven依存
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.spring.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>2.0.0version>
            dependency>
            
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>3.4.8version>
            dependency>
            <dependency>
                <groupId>com.101tecgroupId>
                <artifactId>zkclientartifactId>
                <version>0.10version>
            dependency>
            
            <dependency>
                <groupId>com.niezhilianggroupId>
                <artifactId>common-apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
    
    クラスを開始
    @SpringBootApplication
    @EnableDubboConfiguration
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    
    設定ファイル
    server:
      port: 8080
    spring:
      dubbo:
        application:
          #     ,    
          name: server-provider
        # zookeeper  ,        
        registry:
          address: zookeeper://127.0.0.1:2181
        #      
        protocol:
          # dubbo  ,    
          name: dubbo
          #        (   20880,              )
          port: 20880
        #        ,      
        server: true
        #             
        scan: com.niezhiliang.spring.boot.dubbo.server.provide.serviceImpl
    
    暴露の方法
    //     dubbo      spring   
    @Service(interfaceClass = HelloService.class)
    @Component
    public class HelloServiceImpl implements HelloService {
    
        @Override
        public String hello(String message) {
            return "hello,"+message;
        }
    }
    
    サービス消費モジュール
    maven依存
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.spring.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>2.0.0version>
            dependency>
            
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>3.4.8version>
            dependency>
            <dependency>
                <groupId>com.101tecgroupId>
                <artifactId>zkclientartifactId>
                <version>0.10version>
            dependency>
            
            <dependency>
                <groupId>com.niezhilianggroupId>
                <artifactId>common-apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
    
    設定ファイル
    server:
      port: 8888
    
    spring:
      dubbo:
        application:
          #     ,    
          name: server-consumer
          # zookeeper  ,        
        registry:
          address: zookeeper://127.0.0.1:2181
        protocol:
          # dubbo  ,    
          name: dubbo
        #             
        scan: com.niezhiliang.spring.boot.dubbo.server.customer.controller
    
    クラスを開始
    @SpringBootApplication
    @EnableDubboConfiguration
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    
    サービス提供モジュールのメソッドコードを呼び出します。
    @RestController
    public class IndexController {
        @Reference//dubbo    
        private HelloService helloService;
    
        @RequestMapping(value = "hello")
        public String hello() {
            return helloService.hello("suyu");
        }
    }
    
    
    参考記事:https://mrbird.cc/Spring-Boot-Dubbo-Zookeeper.html
    プロジェクトのソースコード:https://github.com/niezhiliang/spring-boot-dubbo