It’s been a while since I started using Dubbo, and I’ve learned a lot about dubbo during the development and practice of the project. Today we finally decided to ditch the original Netty framework and move to Dubbox

Dubbox is a version of Dubbo maintained by Dangdang after Alibaba stopped updating dubbo. Basically fully compatible with the original Dubbo mode in use. It also supports higher versions of Spring and efficient serialization libraries such as FST.

Another important point is that DubBox supports restful calls, which means that HTTP services for apps can be integrated into the framework without having to deploy tomcat to provide restful calls for apps.

I reviewed the Dubbox demo in detail for this change. And the following modifications were made

1. Use ZooKeeper as the registry.

Last time we talked about switching to ZooKeeper as the registry at the right time, so now is the time

Zookeeper installation is actually quite simple, download and configure zoo. CFG, as long as the following two lines, specify the data directory and log directory

dataDir=/root/zookeeper/data
dataLogDir=/root/zookeeper/log
Copy the code

Then perform

zookeeper/bin/zkServer.sh start
Copy the code

You can simply start an instance.

And since the configuration file was used before, simply modify the configuration file:

Dubbo. Registry = zookeeper: / / 192.168.3.9:2181Copy the code

Re-designate a registry and have fun.

It’s worth noting that Dubbo’s support for Redis is, as documented, erratic. Most of the time you cannot unregister a service in Redis when you close it. With the switch to ZooKeeper, this problem was solved.

2. Change the boot mode

The original initiator was to start a Netty service and then start an instance of Spring from the Netty service to perform Dubbo. It’s kind of a full circle. To ensure service availability in case dubbo is not yet reliable.

Now that dubbo is basically stable, I decided to switch to a simpler startup mode:

public class Application {

    private static Logger logger = LoggerFactory.getLogger(Application.class);
    public static String serverName = "";
    private static volatile boolean running = true;
    public static GenericApplicationContext applicationContext;
    public static ChannelFuture future;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(GenericApplicationContext applicationContext) {
        Application.applicationContext = applicationContext;
    }

    public static void startup(){
        Application.applicationContext.start();
        logger.info("service "+serverName+" start");

        synchronized (Application.class) {
            while (running) {
                try {
                    Application.class.wait();
                } catch (Throwable e) {
                }
            }
        }
    }
    public static void shutdown(){
        running = false; }}Copy the code

Seems to write a lot of, in fact, only a sentence:

Application.applicationContext.start();
Copy the code

Is a standard way to start spring applications. The following loop ensures that the program does not exit. Until the flag position is false.

In order to ensure that the call method is relatively unchanged, another initiator is provided to perform some initialization operations:

Public class DubboBootstrap {public DubboBootstrap(String serverName){// Get the spring context when initializing the initiator. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class); / / to add context to a global variable is easy to use Application. SetApplicationContext (context); Application.serverName = serverName; } /** * netty starts * @throws Exception */ public void run() throws Exception {application.startup (); }}Copy the code

This leaves only one line on each service instance’s initiator:

new DubboBootstrap(server_name).run();
Copy the code

It saves the trouble of manually specifying the service and the IP address of the server. Leave it all to configuration files and dubbox reliably

# # # 3, other

More than a month into the project, the main work is still poor CRUD business logic. When you can’t keep up with the current end, you have time to study technical issues.

In fact, the project is quite a mess of the project, lack of staff, technology is not good, business is not skilled, basically all the pits of start-up companies step over one by one.

But the quickest way to gain knowledge is to stick to it. Speaking of the gains in this project, many are not specific code can describe, but a kind of overall control of the improvement. A breakthrough in your code style.

It was my first attempt to break out of the coding style of my old employer and learn to add some of my own understanding.

Alas, anyway the whole background on my own a person, to their refueling ~