zoukankan      html  css  js  c++  java
  • spring-boot整合Dubbo分布式架构案例

    1.运行环境

    开发工具:intellij idea

    JDK版本:1.8

    项目管理工具:Maven 3.2.5

    2.项目文件目录

    3.Maven Plugin管理

    总项目 pom.xml配置代码:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>spring-boot-dubbo</groupId>
     8     <artifactId>spring-boot-dubbo</artifactId>
     9     <packaging>pom</packaging>
    10     <version>1.0-SNAPSHOT</version>
    11     <modules>
    12         <module>spring-boot-dubbo-api</module>
    13         <module>spring-boot-dubbo-provider</module>
    14         <module>spring-boot-dubbo-consumer</module>
    15     </modules>
    16 
    17     <parent>
    18         <groupId>org.springframework.boot</groupId>
    19         <artifactId>spring-boot-starter-parent</artifactId>
    20         <version>1.5.6.RELEASE</version>
    21     </parent>
    22 
    23     <properties>
    24         <java.version>1.7</java.version>
    25         <springboot.version>1.5.6.RELEASE</springboot.version>
    26     </properties>
    27 
    28     <dependencies>
    29         <!-- Spring Boot web依赖 -->
    30         <dependency>
    31             <groupId>org.springframework.boot</groupId>
    32             <artifactId>spring-boot-starter-web</artifactId>
    33         </dependency>
    34         <!-- Spring Boot test依赖 -->
    35         <dependency>
    36             <groupId>org.springframework.boot</groupId>
    37             <artifactId>spring-boot-starter-test</artifactId>
    38             <scope>test</scope>
    39         </dependency>
    40         <!-- Spring Boot dubbo依赖 -->
    41         <dependency>
    42             <groupId>io.dubbo.springboot</groupId>
    43             <artifactId>spring-boot-starter-dubbo</artifactId>
    44             <version>1.0.0</version>
    45         </dependency>
    46     </dependencies>
    47 
    48 </project>
    View Code

    provider pom.xml配置代码:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <parent>
     6         <artifactId>spring-boot-dubbo</artifactId>
     7         <groupId>spring-boot-dubbo</groupId>
     8         <version>1.0-SNAPSHOT</version>
     9         <relativePath>../pom.xml</relativePath>
    10     </parent>
    11     <modelVersion>4.0.0</modelVersion>
    12 
    13     <artifactId>spring-boot-dubbo-provider</artifactId>
    14 
    15     <dependencies>
    16         <dependency>
    17             <groupId>spring-boot-dubbo</groupId>
    18             <artifactId>spring-boot-dubbo-api</artifactId>
    19             <version>1.0-SNAPSHOT</version>
    20         </dependency>
    21     </dependencies>
    22     <build>
    23         <plugins>
    24             <plugin>
    25                 <groupId>org.springframework.boot</groupId>
    26                 <artifactId>spring-boot-maven-plugin</artifactId>
    27                 <version>${springboot.version}</version>
    28             </plugin>
    29         </plugins>
    30     </build>
    31 
    32 </project>
    View Code

    consumer pom.xml配置代码:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <parent>
     6         <artifactId>spring-boot-dubbo</artifactId>
     7         <groupId>spring-boot-dubbo</groupId>
     8         <version>1.0-SNAPSHOT</version>
     9         <relativePath>../pom.xml</relativePath>
    10     </parent>
    11     <modelVersion>4.0.0</modelVersion>
    12 
    13     <artifactId>spring-boot-dubbo-consumer</artifactId>
    14 
    15     <dependencies>
    16         <dependency>
    17             <groupId>spring-boot-dubbo</groupId>
    18             <artifactId>spring-boot-dubbo-api</artifactId>
    19             <version>1.0-SNAPSHOT</version>
    20         </dependency>
    21     </dependencies>
    22     <build>
    23         <plugins>
    24             <plugin>
    25                 <groupId>org.springframework.boot</groupId>
    26                 <artifactId>spring-boot-maven-plugin</artifactId>
    27                 <version>${springboot.version}</version>
    28             </plugin>
    29         </plugins>
    30     </build>
    31 
    32 </project>
    View Code

    4.api相关配置编写

    ExampleService接口类编写

    1 package com.goku.demo.api.service;
    2 
    3 /**
    4  * Created by nbfujx on 2017-11-23.
    5  */
    6 public interface ExampleService {
    7     String echo(String str);
    8 }
    View Code

    5.provider相关配置编写

    application.properties编写

    ## 避免和 consumer 工程端口冲突
    server.port=8081
    
    spring.dubbo.application.name=provider
    spring.dubbo.registry.address=zookeeper://localhost:2181
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=20880
    ## 本机的IP地址
    spring.dubbo.protocol.host=127.0.0.1
    spring.dubbo.scan=com.goku.demo
    ## 设置Module
    spring.dubbo.module.default=false
    View Code

    ExampleServiceImpl接口实现类编写

    package com.goku.demo.service.impl;
    
    import com.goku.demo.api.service.ExampleService;
    import com.alibaba.dubbo.config.annotation.Service;
    
    /**
     * Created by nbfujx on 2017-11-23.
     */
    @Service(version = "1.0.0")
    public class ExampleServiceImpl implements ExampleService {
    
        @Override
        public String echo(String str) {
            return "hello"+ str;
        }
    }
    View Code

    6.consumer相关配置编写

    application.properties编写

    spring.dubbo.application.name=consumer
    spring.dubbo.registry.address=zookeeper://localhost:2181
    spring.dubbo.scan=com.goku.demo
    spring.dubbo.module.default=false
    View Code

    ExampleController控制器编写

     1 package com.goku.demo.controller;
     2 
     3 import com.alibaba.dubbo.config.annotation.Reference;
     4 import com.goku.demo.api.service.ExampleService;
     5 import org.springframework.web.bind.annotation.PathVariable;
     6 import org.springframework.web.bind.annotation.RequestMapping;
     7 import org.springframework.web.bind.annotation.RestController;
     8 
     9 /**
    10  * Created by nbfujx on 2017-11-20.
    11  */
    12 @RestController
    13 public class ExampleController {
    14 
    15     @Reference(version = "1.0.0")
    16     public ExampleService exampleService;
    17 
    18     @RequestMapping("/")
    19     public String helloWorld()
    20     {
    21         return "helloWorld";
    22     }
    23 
    24     @RequestMapping("/{str}")
    25     public String echo(@PathVariable  String str)
    26     {
    27         return exampleService.echo(str);
    28     }
    29 }
    View Code

    7.在页面上运行

    http://localhost:8080/

    http://localhost:8080/str

    8.降级服务

    mock的配置可以在出现非业务异常(比如超时,网络异常等)时执行。mock的配置支持两种,一种为boolean值,默认的为false。如果配置为true,则缺省使用mock类名,即类名+Mock后缀;另外一种则是配置"return null",可以很简单的忽略掉异常。

     ExampleServiceMock缺省类编写

     1 package com.goku.demo.api.service;
     2 
     3 import org.springframework.stereotype.Service;
     4 
     5 /**
     6  * Created by nbfujx on 2017-11-27.
     7  */
     8 @Service
     9 public class ExampleServiceMock  implements ExampleService {
    10     @Override
    11     public String echo(String str) {
    12         return "hello"+ str+"this is error!";
    13     }
    14 }
    View Code

    ExampleController控制器修改

     1 package com.goku.demo.controller;
     2 
     3 import com.alibaba.dubbo.config.annotation.Reference;
     4 import com.goku.demo.api.service.ExampleService;
     5 import com.goku.demo.api.service.ExampleServiceMock;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.web.bind.annotation.PathVariable;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RestController;
    10 
    11 /**
    12  * Created by nbfujx on 2017-11-20.
    13  */
    14 @RestController
    15 public class ExampleController {
    16 
    17     @Reference(version = "1.0.0",check=false,mock="com.goku.demo.api.service.ExampleServiceMock")
    18     @Autowired
    19     public ExampleService exampleService;
    20 
    21     @RequestMapping("/")
    22     public String helloWorld()
    23     {
    24         return "helloWorld";
    25     }
    26 
    27     @RequestMapping("/{str}")
    28     public String echo(@PathVariable  String str)
    29     {
    30         return exampleService.echo(str);
    31     }
    32 }
    View Code

    测试服务未启动返回值

    http://localhost:8080/str

     

    9.GITHUB地址

    https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-dubbo

    待续

  • 相关阅读:
    散列表(Hash Table)
    MVC中TextBox事件
    AJAX控制DropDownList两级联动
    唯一标示
    检查对象属性是否有空值
    foreach枚举div控制单个显示
    JS获取DropDownList其中一项的文本值
    随便
    MVC常用
    不可用输入框
  • 原文地址:https://www.cnblogs.com/nbfujx/p/7884434.html
Copyright © 2011-2022 走看看