Author: vangoleo website: www.vangoleo.com/iris-java/

background

Time flies, entered ali middleware team has been nearly two years. During this period, I was fortunate to participate in the topic group of the 4th Middleware Performance Challenge, and prepared the preliminary topic with the theme of “Dubbo Mesh”. Organized Dubbo offline Meetup with the team; The best practices and methodology of middleware infrastructure accumulated in Alibaba’s double 11 years will be provided for developers and enterprises through ali Cloud’s commercial products. I’m glad to have had such an unforgettable experience. In retrospect, joining the middleware team had something to do with one of my Github projects. I’m going to share this project with you today.

Q: What is a middleware team? A: Alibaba middleware Technology Department, one of the world’s top Java technology teams, originated from taobao platform architecture group. It is A technical team that grows up with Alibaba’s e-commerce business and Double Eleven, and solves complex business scenarios, rapid business growth, high-concurrency flood promotion peaks, and endless stability problems. Products include high distributed RPC service framework, high reliable distributed message middleware, distributed data layer, mass data storage, real-time calculation, system performance optimization, the architecture of multiple products in the field of high availability and so on several big, these products support alibaba group (taobao, Tmall, bargain, rookie), 1688, all of the trading and non trading system, Enron weathered the challenges of 91.7 billion deals on Nov 11. Our open source middleware components, Dubbo, Rocketmq, Nacos, Tengine, Seata, etc., are used by many enterprises and individuals.

Invitations from middleware

In 2017, I led a team on a microservice refactoring of the back-end architecture. Dubbo framework was used for selection. Thanks to Dubbo’s high performance, ease of use and high scalability, the microservice transformation is going smoothly and the company’s business is becoming more stable. I was also very interested in Dubbo, hoping to learn more about this excellent RPC framework. I researched the source code for Dubbo and wrote a mini version of Dubbo from scratch. Just in time ali restarted the Dubbo project and it became an Apache incubator project (officially an Apache project when the text was written). Dubbo’s new website has a Wanted: Who’s Using Dubbo page, and I left a message to like Dubbo. It contains the project address for the mini Dubbo.

iris

The project address for Dubbo mini is github.com/vangoleo/ir… . I named it Iris.

Iris is a lightweight, microkernel plus plug-in mechanism, Java-based RPC framework. Provides service registration, discovery, load balancing, support for API calls, Spring integration and Spring Boot starter use.

Has the following features:

  • Network communication: Netty4.
  • Registry: extensible with ETCD support.
  • Dynamic proxy: byte-buddy.
  • Serialization: Protobuff(Protostuff).
  • You can break away from Spring and provide API calls. It implements the IoC container itself.
  • Integrate with Spring, provide XML, Java configuration.
  • Spring Boot Starter is available (Dubbo did not officially support Spring Boot Starter when the project was developed).
  • SPI mechanism is provided to realize the micro kernel plus plug-in architecture. Implementation extensible, developers can develop components for IRIS that are integrated into IRIS in the form of plug-ins. The loading of the plug-in using another microcontainer framework is shown in the coco project. The project was forked at Ali’s Cooma.

Description: IRIS is my personal study project. Although it is small, it has all the functions and covers the basic functions of RPC framework. It just implements zero to one, but there’s a lot of work to do to get from one to 100.

How to use

Iris supports the following uses:

  • Native API form that does not rely on Spring and can be used by non-Spring projects.
  • Spring configuration and good integration with Spring.
  • Spring Boot configuration provides a Spring Boot starter for automatic configuration and fast startup.

Use the API

The Iris core code does not depend on Spring and can be used without Spring. Step 1: Start the ETCD registry and write an interface IHelloService

public interface IHelloService {
    String hello(String name);
}
Copy the code

Step 2: Write an implementation of IHelloService

public class HelloService implements IHelloService {
    @Override
    public String hello(String name){
        return "Hello, "+ name; }}Copy the code

Step 3: Start the Server

IRegistry registry = new EtcdRegistry("http://127.0.0.1:2379");
RpcServer server = new RpcServer(registry)
        .port(2017)
        .exposeService(IHelloService.class,new HelloService());
server.run();
Copy the code

Step 4: Start the client

RpcClient client = new RpcClient(registry);
IHelloService helloService = client.create(IHelloService.class);
String s = helloService.hello("leo");
System.out.println(s);   // hello, leo
Copy the code

Step 6: Start the server. After the server is started, the server will go to the ETCD registry to register the service. The client works normally immediately.

Spring configuration

Step 1: Write the Service provider, exposing the Service with a custom annotation @Service and specifying the interface of the Service through interfaceClass. The @service annotation is provided by Iris, not Spring.

@Service(interfaceClass = IHelloService.class)
public class HelloService implements IHelloService {
    @Override
    public String hello(String name) throws Exception {
        return "hello"+ name; }}Copy the code

Step 2: write a service consumer service consumer that references the remote service via @reference as if it were a local SpringBean. The underlying SpringBean encapsulation and Rpc calls are transparent to the developer. The experience is the same as Dubbo.

public class Baz {

    @Reference(interfaceClass = IHelloService.class)
    private IHelloService helloService;

    public void hello(String name) throws Exception { System.out.println(helloService.hello(name)); }}Copy the code

Step 3: Configure the Spring Bean to configure the service provider, using XML or Java Code for this example.

<bean id="registry" class="com.leibangzhu.iris.registry.EtcdRegistry">
        <constructor-arg name="registryAddress" value="http://127.0.0.1:2379"></constructor-arg>
    </bean>

    <bean id="server" class="com.leibangzhu.iris.server.RpcServer">
        <constructor-arg name="registry" ref="registry"></constructor-arg>
    </bean>

    <bean id="serviceAnnotationBeanPostProcessor" class="com.leibangzhu.iris.spring.ServiceAnnotationBeanPostProcessor"></bean>

<bean id="helloService" class="com.leibangzhu.iris.spring.HelloService"></bean>
Copy the code

Step 4: Configure the service consumer, either in XML or Java Code for this example.

<bean id="registry" class="com.leibangzhu.iris.registry.EtcdRegistry">
    <constructor-arg name="registryAddress" value="http://127.0.0.1:2379"></constructor-arg>
</bean>

<bean id="client" class="com.leibangzhu.iris.client.RpcClient">
    <constructor-arg name="registry" ref="registry"></constructor-arg>
</bean>

<bean id="referenceAnnotationBeanPostProcessor" class="com.leibangzhu.iris.spring.ReferenceAnnotationBeanPostProcessor"></bean>

<bean id="foo" class="com.leibangzhu.iris.spring.Baz"></bean>
Copy the code

Spring the Boot configuration

While using the native Spring configuration is still a bit cumbersome, you can use Spring Boot for a better development experience. Step 1: Write the service provider

@Service(interfaceClass = IHelloService.class)
public class HelloService implements IHelloService {
    @Override
    public String hello(String name) throws Exception {
        return "Hello, " + name + ", from com.leibangzhu.iris.springboot.HelloService"; }}Copy the code

Step 2: Write the service consumer

@Component
public class Foo {

    @Reference(interfaceClass = IHelloService.class)
    private IHelloService helloService;

    public String hello(String name) throws Exception {
        returnhelloService.hello(name); }}Copy the code

Step 3: Configure the service provider in the application.properties file

Iris. Registry. Address = http://127.0.0.1:2379 iris. Server enable =true
iris.server.port=2017
iris.annotation.package=com.leibangzhu.iris.springboot
Copy the code

Step 4: Configure the service consumer in the application.properties file

Iris. Registry. Address = http://127.0.0.1:2379 iris. The client. The enable =true
Copy the code

There is no need to manually configure spring beans when using SpringBoot. The SpringBoot Starter provided by Iris will automatically configure these spring beans.

Why iris

Iris is named after Van Gogh’s painted iris. I like painting, and Van Gogh is my favorite painter, so I named the project with van Gogh’s iris.

Why do you like Van Gogh? In fact, what attracted me was not The painter Van Gogh, but the pious, kind and fanatical Christian Van Gogh. To quote one of my friends from years ago:

These days, I listen to a biography of Van Gogh every night before I go to bed. In fact, I didn’t know about Van Gogh before, so I was glad to hear this biography of Jiang Xun and understand such a wonderful life. Van Gogh sold only one painting in his lifetime, and at a very low price, but after his death, his paintings became the greatest works of art. Why van Gogh’s sunflowers burn so intensely, many people say it is because of his love of art, and some even say it is because of van Gogh’s visual disability. That’s not true. In his short 37-year life, which was really only the last four years of his life as a painter, Van Gogh’s more important role was that of a devout, fanatical Christian. As a Christian, van Gogh should not see such a group of people and do nothing. As a Christian, he should see the suffering in the world and be able to bear it, which is called salvation. Didn’t Jesus come to earth for redemption? Didn’t Jesus be crucified for redemption? What is the point of Jesus Christ’s existence if there is no redeeming part of life? He took off his splendid black robe and white scarf, took up his shovel, and went down with them into the mine. Because in the face of such a group of people, gorgeous preaching language can not help them, only to really feel their life, can help and save these poor people. Van gogh is the church of the chair, to house injured miners in the mine, all their own food to miners, such a has a deep humanitarian concern, is received by the church fire book, church believe a pastor should be dressed in black robes, speak the language very grandiose sermons, van gogh did not maintain the dignity of the church. It was the biggest shock van Gogh ever received. When Van Gogh was on the verge of despair, what saved him was art, painting. What kind of brush could paint Al so passionately sunflower, and what kind of mental torture would make him cut off his ear, what do van Gogh’s so many self-portraits portend? And what kind of life did he create starry Night, his greatest painting of his life, in the cabin where he was imprisoned in an asylum? Whether crows in a wheat field, van Gogh’s last painting, predicted the end of his life. This was the life of a man called Vincent Van Gogh.

My github account, vangoleo, is a combination of vangogh + Leo.

Finally, I have attached some of my own drawings. I don’t know if you can recognize them

This article is posted by www.vangoleo.com