Build a dubbo + zookeeper platform from scratch, dubbozookeeper

Source: Internet
Author: User

Build a dubbo + zookeeper platform from scratch, dubbozookeeper

 

This article mainly describes how to build a dubbo + zookeeper platform from the beginning, and briefly introduces the role of dubbo service.

  • First of all, let's take a look at the evolution of General website architectures with the development of business, the increasingly complex logic, the growing amount of data, and the increasing interaction of conventional solutions.

  • Second, what service governance should we do when there are more and more services?

  • The architecture of dubbo


Registration Center Selection


Dubbo supports multiple types of registration centers:

  • Multicast Registration Center
  • Zookeeper Registration Center
  • Redis Registration Center
  • Simple Registration Center

Here we select zookeeper. For more information about the advantages and disadvantages of the type, see the document.


1: The installation of zookeeper is still based on the rare run Command docker to install zookeeper.

docker run -dit --name zookeeper  --hostname  zookeeper-host  -v /data:/data -p 2181:2181 jplock/zookeeper:latest 


2: Install zkui. You can refer to the zkui Project address to install it. It provides a management interface that allows you to perform CRUD operations on zookeepr node values and also provides security authentication, follow these steps to complete the installation.

  • Mvn clean install: install the java environment and maven environment before execution. After successful execution, a jar file is generated.
  • Copy config. cfg to the directory where the jar file generated in the previous step is located, and modify the zookeeper address in the configuration file.
  • Execute jar. (nohup java-jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar &), pay attention to the following &, which means not to exit.
  • Test: http: // localhost: 9090. If the following page is displayed, the installation of zookeeper is normal.


The following process creates the dubbo service and uses the dubbo service:

Dubbo provider to create a java project. Note the following:

  • Package dependency. You can introduce the following three major packages, mainly spring, dubbo, and zkclient.
<dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring-framework.version}</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.4.10</version>            <exclusions>                <exclusion>                    <artifactId>spring</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.3</version>        </dependency>
  • Defines the interface. For demonstration, a simple interface that returns the product name is defined.
public interface IProduct {    String getProductName();}
  • Interface implementation
@Servicepublic class ProductService implements IProduct{    public String getProductName() {               return "jim";    }}
  • Service Startup Function
    • Load the configuration file
    • Call context. start ()
    • There are many ways to execute an operation without exiting the program.
public class App {    private final static Logger logger = LoggerFactory.getLogger(App.class);    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                "classpath*:applicationContext.xml");        context.start();        logger.info("dubbo service begin to start");        try {            System.in.read();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
  • Service configuration file, which must be pointed out here:
    • Dubbo: the definition of service works with dubbo: annotation, ref = "productService", which is a specified id. The actual implementation class is completed by annotation scanning, the implementation class is not specified in the configuration file, which will be reflected in the consumer configuration file later.
    • Dubbo: in application, you can specify the logger implementation interface.
    • Dubbo: In protocol, you can specify whether to enable access logs. This is very helpful for troubleshooting online problems.
 <?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:dubbo="http://code.alibabatech.com/schema/dubbo"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd          http://code.alibabatech.com/schema/dubbo          http://code.alibabatech.com/schema/dubbo/dubbo.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util.xsd"        >    <context:property-placeholder location="classpath*:config.properties"/>    <dubbo:application name="jim" logger="slf4j" />    <dubbo:registry protocol="zookeeper" address="192.168.21.128:2181" />    <dubbo:protocol accesslog="true" name="dubbo" port="20880" />    <dubbo:annotation package="jim" />    <dubbo:service interface="jim.IProduct" ref="productService"/>    <context:component-scan base-package="jim" />    <import resource="redis-context.xml"/></beans>


Dubbo consumer

  • The configuration of the consumer configuration file is much simpler than that of the provider:
    • Specify the consumer name. This parameter is optional and does not need to be associated with the provider for tasks.
    • Specify the protocol type, zookeeper address.
    • Specify the referenced service interface. Note that the id here is the same as the ref value defined by the service provider.
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <dubbo:application name="consumer-of-jim-app"  />    <dubbo:registry protocol="zookeeper" address="192.168.21.128:2181"/>    <dubbo:reference interface="jim.IProduct" id="productService" /></beans>
  • Interface annotation definition and interface call
@Controllerpublic class HomeController {    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);    @Autowired    private IProduct productService;    @RequestMapping(value = "/", method = RequestMethod.GET)    public String home(Locale locale, Model model) {        logger.info("Welcome home! The client locale is {}.", locale);               String productName=this.productService.getProductName();        model.addAttribute("name", productName);        return "home";    }}

Dubbo admin

There is a UI tool that can be used to manage dubbo services. Unfortunately, I did not download the tool from the link provided by the official documentation, and the download was completed from other places, however, some problems have been encountered during installation and deployment.

Normally, you can see the following interface:

  • Management provider
  • Manage consumers

Service governance



After the above steps, you can start the server and the client to verify. The above is just a simple dubbo environment and a hello world Service Interface. to make good use of dubbo, there are many best practices available, such as service governance:

  • Local stub
  • Local camouflage
  • Result Cache
  • Multi-version
  • Service downgrade
  • ......

     

Reference:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.