springboot结合服务化框架motan

简介:

springboot极大地提升了java应用的开发体验,感觉特别酸爽。服务化框架可谓是大型系统必用,比较古典的是阿里开源的dubbo,可惜很早就不更新了,研究其代码来看,感觉不够轻量,幸运的是去年微博开源了自己的轻量级服务化框架motan。本文阐述下如何在springboot下用motan。

服务方:

工程结构代码 收藏代码

whatsmars-motan  
  |-src  
    |-main  
      |-java  
        |-com.weibo.motan.demo.service  
      |-impl  
        MotanDemoServiceImpl.java  
      APP.java  
      MotanDemoService.java  
      |-resource  
        |-spring  
      motan_demo_server.xml  
    log4j.properties  
  pom.xml  

依赖:
Xml代码 收藏代码

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  
    <modelVersion>4.0.0</modelVersion>  
  
    <artifactId>whatsmars-motan</artifactId>  
  
    <properties>  
        <!-- The main class to start by executing java -jar -->  
        <start-class>com.weibo.motan.demo.service.App</start-class>  
        <motan.version>0.1.1</motan.version>  
    </properties>  
  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.5.2.RELEASE</version>  
    </parent>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
        </dependency>  
        <!--weibo motan-->  
        <dependency>  
            <groupId>com.weibo</groupId>  
            <artifactId>motan-core</artifactId>  
            <version>${motan.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.weibo</groupId>  
            <artifactId>motan-transport-netty</artifactId>  
            <version>${motan.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.weibo</groupId>  
            <artifactId>motan-registry-consul</artifactId>  
            <version>${motan.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.weibo</groupId>  
            <artifactId>motan-registry-zookeeper</artifactId>  
            <version>${motan.version}</version>  
        </dependency>  
  
        <!-- dependencies blow were only needed for spring-based features -->  
        <dependency>  
            <groupId>com.weibo</groupId>  
            <artifactId>motan-springsupport</artifactId>  
            <version>${motan.version}</version>  
        </dependency>  
    </dependencies>  
  
    <build>  
        <finalName>${project.artifactId}</finalName>  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
                <filtering>true</filtering>  
            </resource>  
        </resources>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <dependencies>  
                    <dependency>  
                        <groupId>org.springframework</groupId>  
                        <artifactId>springloaded</artifactId>  
                        <version>1.2.6.RELEASE</version>  
                    </dependency>  
                </dependencies>  
            </plugin>  
        </plugins>  
    </build>  
</project>  

demo_motan_server.xml
Xml代码 收藏代码

<?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:motan="http://api.weibo.com/schema/motan"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
       http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">  
  
    <!-- 业务具体实现类 -->  
    <!-- <bean id="motanDemoServiceImpl" class="com.weibo.motan.demo.service.impl.MotanDemoServiceImpl"/> -->  
  
    <!-- 注册中心配置 使用不同注册中心需要依赖对应的jar包。如果不使用注册中心,可以把check属性改为false,忽略注册失败。-->  
    <!--<motan:registry regProtocol="local" name="registry" />-->  
    <!--<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>-->  
    <motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"/>  
  
    <!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。-->  
    <motan:protocol id="demoMotan" default="true" name="motan"  
                    maxServerConnection="80000" maxContentLength="1048576"  
                    maxWorkerThread="800" minWorkerThread="20"/>  
  
    <!-- 通用配置,多个rpc服务使用相同的基础配置. group和module定义具体的服务池。export格式为“protocol id:提供服务的端口”-->  
    <motan:basicService export="demoMotan:8002"  
                        group="motan-demo-rpc" accessLog="false" shareChannel="true" module="motan-demo-rpc"  
                        application="myMotanDemo" registry="registry" id="serviceBasicConfig"/>  
  
    <!-- 具体rpc服务配置,声明实现的接口类。-->  
    <motan:service interface="com.weibo.motan.demo.service.MotanDemoService"  
                   ref="motanDemoService" export="demoMotan:8001" basicService="serviceBasicConfig">  
    </motan:service>  
    <motan:service interface="com.weibo.motan.demo.service.MotanDemoService"  
                   ref="motanDemoService" export="demoMotan:8002" basicService="serviceBasicConfig">  
    </motan:service>  
  
</beans>  

启动类:
Java代码 收藏代码

@SpringBootApplication  
@EnableAutoConfiguration  
@ImportResource(locations={"classpath*:spring/*server.xml"})  
public class App {  
  
    public static void main(String[] args) {  
        SpringApplication.run(App.class, args);  
    }  
  
}  

下面这个很重要:
Java代码 收藏代码

@Component  
@Order(value = 1)  
public class MotanSwitcherRunner implements CommandLineRunner {  
    @Override  
    public void run(String... args) throws Exception {  
        // 在使用注册中心时要主动调用下面代码  
        MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);  
        System.out.println("server start...");  
    }  
}  

服务接口:
Java代码 收藏代码

public interface MotanDemoService {  
  
    String hello(String name);  
}  

服务实现:
Java代码 收藏代码

@Service("motanDemoService")  
public class MotanDemoServiceImpl implements MotanDemoService {  
  
    public String hello(String name) {  
        System.out.println(name);  
        return "Hello " + name + "!";  
    }  
  
}  

就这么简单,先启动zookeeper,再启动App就可发布motan服务,当应用中既要发布服务,又要引用服务时,可以将注册中心配置单独放在一个配置文件里。另外,不管是dubbo,还是motan,对注解的支持都不是特别好用,所以还是建议采用xml配置。关于消费方怎么用,详细代码见https://github.com/javahongxi/whatsmars的whatsmars-motan-demo 模块。

原文链接:[http://wely.iteye.com/blog/2382243]

相关文章
|
Java 关系型数据库 MySQL
idea搭建SpringBoot项目框架的两种方式
idea搭建SpringBoot项目框架的两种方式
idea搭建SpringBoot项目框架的两种方式
|
开发框架 JavaScript 前端开发
SpringBoot + Ant Design Pro Vue实现动态路由和菜单的前后端分离框架
Ant Design Pro Vue默认路由和菜单配置是采用中心化的方式,在 router.config.js统一配置和管理,同时也提供了动态获取路由和菜单的解决方案,并将在2.0.3版本中提供,因到目前为止,官方发布的版本为2.0.2,所以本文结合官方提供的解决方案结合SpringBoot后台权限管理进行修改,搭建一套完整的SpringBoot +Vue前后端分离框架。
946 0
|
SQL 存储 Java
【2021软件创新实验室暑假集训】SpringBoot框架
【2021软件创新实验室暑假集训】SpringBoot框架
【2021软件创新实验室暑假集训】SpringBoot框架
|
Java 开发工具 git
插件式编程SBP框架极简教程(基于SpringBoot)
插件式编程SBP框架极简教程(基于SpringBoot)
500 0
插件式编程SBP框架极简教程(基于SpringBoot)
|
XML Java 数据库连接
详解SpringBoot整合Mybatis框架
详解SpringBoot整合Mybatis框架
详解SpringBoot整合Mybatis框架
|
缓存 Java Spring
初窥spring boot 源码之框架初始化
从Spring boot 框架初始化深入理解框架是怎么实现的
175 0
初窥spring boot 源码之框架初始化
|
SQL XML Java
SpringBoot2.x系列教程29--SpringBoot整合JPA框架实现数据库CRUD操作
前言 在上一章节中,壹哥 带大家在Spring Boot中整合了Mybatis框架,虽然Mybatis进行数据库操作已经很简单了,但是作为一个程序员,我们就得有追求极致的精神,就要想一下,还有没有第3种方式可以进行数据库操作呢? 其实是有的! 所以在这一章节中,我会继续带领大家学习在Spring Boot中整合JPA框架,利用JPA来实现数据库的操作。 一. JPA简介 1. JPA概念 JPA是Sun官方提出的Java持久化规范,是Java Persistence API的简称,中文名‘Java持久层API’,它本质上是一种ORM规范。 JPA通过 JDK 5.0 的 注解或XML 两
176 0
|
SQL XML Java
SpringBoot2.x系列教程28--SpringBoot中整合Mybatis框架实现数据库CRUD操作
前言 在上一章节中,我带大家在Spring Boot中整合了JdbcTemplate,利用JdbcTemplate感觉会比较麻烦,所以我让各位思考有没有更简单易用的实现方式。那么接下来在这一章节中,我会继续带领大家学习在Spring Boot中整合Mybatis框架,利用Mybatis来实现数据库的操作。 一. Mybatis简介 1. MyBatis概述 MyBatis是一款优秀的持久层框架,它本来是Apache的一个开源项目iBatis。在2010年的时候,这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis,在2013年
285 0
|
Dubbo Java 应用服务中间件
Springboot 最简单的整合Dubbo框架实战案例
Springboot 最简单的整合Dubbo框架实战案例
191 0
Springboot 最简单的整合Dubbo框架实战案例
|
JSON Java 数据格式
Springboot 整合Swagger 2框架 让接口查看及调试更加优雅
Springboot 整合Swagger 2框架 让接口查看及调试更加优雅
308 0
Springboot 整合Swagger 2框架 让接口查看及调试更加优雅