Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use lightweight distributed RPC Framework motan

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, Xiaobian introduces in detail "how to use lightweight distributed RPC framework motan" with detailed content, clear steps and proper handling of details. I hope that this article "how to use lightweight distributed RPC framework motan" can help you solve your doubts.

Overview

Motan is a high-performance, easy-to-use distributed remote service invocation (RPC) framework.

Function

Support for integration through spring configuration, providing distributed invocation capabilities for services without writing additional code.

Support the integration of consul, zookeeper and other configuration service components to provide service discovery and governance capabilities in the cluster environment.

Advanced service scheduling capabilities such as dynamic custom load balancer and cross-room traffic adjustment are supported.

Optimize based on high concurrency and high load scenarios to ensure high availability of RPC services in production environment.

Simple invocation example add dependency com.weibogroupId > motan-coreartifactId > 0.1.1version > dependency > com.weibogroupId > motan-transport-nettyartifactId > 0.1.1version > dependency > com.weibogroupId > motan-springsupportartifactId > 0.1.1version > dependency > org.springframeworkgroupId > spring-contextartifactId > 4.2.4.RELEASEversion > create a public interface for callers and servers

Src/main/java/quickstart/FooService.java

Package quickstart; public interface FooService {public String hello (String name);} write business interface logic, create and start RPC Server

Src/main/java/quickstart/FooServiceImpl.java

Package quickstart; public class FooServiceImpl implements FooService {public String hello (String name) {System.out.println (name + "invoked rpc service"); return "hello" + name;}}

Src/main/resources/motan_server.xml

Src/main/java/quickstart/Server.java

Package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Server {public static void main (String [] args) throws InterruptedException {ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("classpath:motan_server.xml"); System.out.println ("server start...");}}

Executing the main function in the Server class will start the Motan service and listen on port 8002.

Create and execute RPC Client

Src/main/resources/motan_client.xml

Src/main/java/quickstart/Client.java

Package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {public static void main (String [] args) throws InterruptedException {ApplicationContext ctx = new ClassPathXmlApplicationContext ("classpath:motan_client.xml"); FooService service = (FooService) ctx.getBean ("remoteService"); System.out.println (service.hello ("motan"));}}

Executing the main function in the Client class makes a remote call and outputs the result.

Cluster invocation example

Using Motan in a clustered environment requires relying on external service discovery components, which currently support consul or zookeeper.

Use CONSUL as the registry

Consul installation and startup

Installation (official documentation)

# here take linux as an example, wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zipunzip consul_0.6.4_linux_amd64.zipsudo mv consul / bin

Launch (official documentation)

Test environment startup: consul agent-dev

Ui background http://localhost:8500/ui

Motan-Consul configuration

Add motan-registry-consul dependencies in server and client

Com.weibogroupId > motan-registry-consulartifactId > 0.1.1version >

Add consul registry definitions to the configuration files of server and client, respectively.

The configuration of Motan client and server is changed to be discovered by registry service.

Client

Server

After the server program starts, you need to explicitly call the heartbeat switch to register with consul.

MotanSwitcherUtil.setSwitcherValue (MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

Go to the ui backend to check whether the service provides calls normally.

Start client and invoke the service

Use ZOOKEEPER as the registry

ZooKeeper installation and startup

Installation and startup of stand-alone version

Wget http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gztar zxvf zookeeper-3.4.8.tar.gzcd zookeeper-3.4.8/conf/cp zoo_sample.cfg zoo.cfgcd.. / sh bin/zkServer.sh startMotan-ZooKeeper configuration

Add motan-registry-zookeeper dependencies in server and client

Com.weibogroupId > motan-registry-zookeeperartifactId > 0.1.1version >

Add zookeeper registry definitions to the configuration files of server and client, respectively.

Zookeeper is a single node

Zookeeper multi-node cluster

The configuration of Motan client and server is changed to be discovered by registry service.

Client

Server

After the server program starts, you need to explicitly call the heartbeat switch to register with zookeeper.

MotanSwitcherUtil.setSwitcherValue (MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

Start client and invoke the service

After reading this, the article "how to use lightweight distributed RPC Framework motan" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report