zoukankan      html  css  js  c++  java
  • Spring Boot和Dubbo整合

    provider端

    1. POM依赖
    <dependencies>
    
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-web</artifactId>
    	</dependency>
    
    	<dependency>
    		<groupId>org.projectlombok</groupId>
    		<artifactId>lombok</artifactId>
    		<optional>true</optional>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-test</artifactId>
    		<scope>test</scope>
    	</dependency>
    
    	<!--interfaces-->
    	<dependency>
    		<groupId>com.lovefly</groupId>
    		<artifactId>pms-interfaces</artifactId>
    	</dependency>
    
    	<!--dubbo-->
    	<dependency>
    		<groupId>io.dubbo.springboot</groupId>
    		<artifactId>spring-boot-starter-dubbo</artifactId>
    	</dependency>
    </dependencies>
    
    1. service实现
    package com.lovefly.pms.services.service;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.lovefly.pms.interfaces.service.TestService;
    
    @Service
    public class TestServiceImpl implements TestService {
        @Override
        public String echo(String input) {
    
            return input + ": pong from service";
        }
    }
    
    1. application.properties
    #server
    server.port=8001
    
    #dubbo
    spring.dubbo.registry.group=pms
    spring.dubbo.application.name=pms-services
    spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=20880
    spring.dubbo.scan=com.lovefly.pms.services.service
    spring.dubbo.consumer.retries=0
    spring.dubbo.consumer.timeout=30000
    spring.dubbo.consumer.check=false
    

    consumer端

    1. POM依赖
    <dependencies>
    
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-thymeleaf</artifactId>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-web</artifactId>
    	</dependency>
    
    	<dependency>
    		<groupId>org.projectlombok</groupId>
    		<artifactId>lombok</artifactId>
    		<optional>true</optional>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-test</artifactId>
    		<scope>test</scope>
    	</dependency>
    
    	<!--interfaces-->
    	<dependency>
    		<groupId>com.lovefly</groupId>
    		<artifactId>pms-interfaces</artifactId>
    	</dependency>
    	<!--dubbo-->
    	<dependency>
    		<groupId>io.dubbo.springboot</groupId>
    		<artifactId>spring-boot-starter-dubbo</artifactId>
    	</dependency>
    
    </dependencies>
    
    1. consumer实现
    package com.lovefly.pms.portal.controller;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.lovefly.pms.interfaces.service.TestService;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api/echo")
    public class EchoController {
    
        @Reference
        private TestService echoService;
    
        @GetMapping("")
        public String echo(@RequestParam("input") String input){
            String pong = echoService.echo(input);
            return pong;
        }
    }
    
    1. application.properties
    #server
    server.port=8000
    
    #dubbo
    spring.dubbo.registry.group=pms
    spring.dubbo.application.name=pms-portal
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=20880
    spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
    spring.dubbo.scan=com.lovefly.pms.portal
    spring.dubbo.consumer.retries=0
    spring.dubbo.consumer.timeout=30000
    spring.dubbo.consumer.check=false
    
    1. 测试
    • 启动zookeeper
    • 使用curl或PostMan测试服务调用
    curl -X GET http://localhost:8000/api/echo/?input=test
    #输出:
    test: pong from service
    

    将目录添加到github项目中

    1. 进入工作目录
    2. 执行以下命令
    $ mvn clean
    $ git init
    $ git add remote origin https://github.com/fuhongwei041/lovefly-pms.git
    $ git branch --set-upstream-to=origin/master master
    $ git pull origin master
    $ # 编辑.gitignore文件
    $ git add .
    $ git commit -m "first commit"
    

    项目地址为https://github.com/fuhongwei041/lovefly-pms.git

    参考资料

  • 相关阅读:
    mybatis总结(五)(延迟加载)
    mybatis总结(四)(mybatis的动态sql)
    mybatis总结(三)(resultMap和高级映射-级联)
    mybatis总结(二)(mybatis的基本增删改查实例说明)
    mybatis总结(一)(mybatis的基本定义介绍)
    法门扫地僧简历经验分享
    法门扫地僧面试宝典第五版
    关于https不支持http的解决方案
    浏览器渲染原理
    前端面试宝典第三版
  • 原文地址:https://www.cnblogs.com/fuhongwei041/p/7457714.html
Copyright © 2011-2022 走看看