SpringBoot7 -- 简单集成motan服务治理框架

目前在项目里集成的motan服务只是简单的使用。

前面使用了多数据源,使用motan之后就将多数据源废弃了。

motan和dubbo差不多,相比下motan更简单易用,两者使用方式很类似。

首先定义API架包,创建API项目,创建成功后,打包到公司私服,这样,在服务消费方和服务提供方pom中引用,规约了实体类和接口,此处具体略。


服务提供方:


在提供方pom文件中引入api架包。

集成时遇到了日志的问题,耽误了不少时间,主要是日志级别为TRACE会无限报错,改为INFO之后就正常启动了

pom

<dependency>
    <groupId>com.xxx</groupId>
    <artifactId>api架包</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.15</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<!-- motan -->
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-core</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-transport-netty</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-registry-zookeeper</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-protocol-yar</artifactId>
    <version>1.1.0</version>
</dependency>
<!-- only needed for spring-based features -->
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-springsupport</artifactId>
    <version>1.1.0</version>
</dependency>

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>


<!--邮件报警包-->
<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>3.0.8</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.5.0-b01</version>
</dependency>

motan.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">

<!-- zookeeper -->
<motan:registry regProtocol="zookeeper" name="registry" address="192.168.7.251,192.168.7.252,192.168.7.253:2181"/>

    <bean id="xxxService" class="服务接口实现类"/>

    <!-- RPC服务 -->
    <motan:service interface="服务接口"
                   group="组名" module="模块" version="1.0" ref="xxxService"
                   registry="registry" export="服务端口"/>

</beans>

启动类

@SpringBootApplication
@ImportResource(locations={"classpath*:motan.xml"})
@MapperScan(value = "mapper包路径")
public class ServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

MotanSwitcherRunner

@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...");
    }
}

此处用到了CommandLineRunner,目的是为了在项目启动之后运行motan的注册服务功能。


服务消费方

除了引入必要的motan依赖,motan.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">

<!-- zookeeper -->
<motan:registry regProtocol="zookeeper" name="registry" address="192.168.7.251,192.168.7.252,192.168.7.253:2181"/>

    <!-- 配置接口调用 -->
    <motan:referer id="xxxService"
                   interface="服务接口"
                   group="组名" module="模块" version="1.0"
                   registry="registry" requestTimeout="5000"/>
 
</beans>

这样在消费方直接按照正常的注入方式注入bean即可,就可以使用接口里声明的方法调用了。

motan里的标签参数参考

motan参数参考

有时间整理下motan的简单实现原理理解,待续

猜你喜欢

转载自blog.csdn.net/qq_36781718/article/details/80178622
今日推荐