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

Build a dubbo+zookeeper platform from scratch

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

First of all, take a look at the general website architecture with the development of business, the logic is becoming more and more complex, the amount of data is getting larger and larger, and there are more and more interactions.

Second, when there are more and more services, what service governance do we need to do?

Finally, there is the architecture diagram of dubbo.

The choice of registry

Dubbo supports several types of registries:

Multicast registry

Zookeeper registry

Redis registry

Simple registry

Here we choose zookeeper. In fact, the pros and cons of the type can be viewed in detail.

For the installation of 1:zookeeper, the fresh run command of docker is still used to install zookeeper.

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

2: to install zkui, you can refer to the project address of zkui. It provides a management interface to perform CRUD operations on the node values of zookeepr, as well as security certification. You can complete the installation by following the steps below.

Mvn clean install, you need to install java environment and maven environment before execution, and a jar file will be generated after successful execution.

Copy the config.cfg to the directory where the jar file was generated in the previous step, and then 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 latter &, which means not to quit.

Test, http://localhost:9090, if you can see the following page, it means that the zookeeper installation is running normally.

Here is the process of creating a dubbo service and using a dubbo service:

Dubbo provider, create a java project, note the following:

Package dependency, introduce the following three main packages, mainly spring,dubbo and zkclient

Org.springframework spring-context ${spring-framework.version} com.alibaba dubbo 2.4.10 spring org.springframework Com.101tec zkclient 0.3

Define the interface, and here, for demonstration, simply define an interface that returns the product name.

Public interface IProduct {String getProductName ();}

Interface implementation

@ Servicepublic class ProductService implements IProduct {public String getProductName () {return "jim";}}

Service startup function

Load configuration file

Call context.start ()

There are many ways to perform an operation that does not exit 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 profile, what you need to point out here is:

The definition of dubbo:service is in conjunction with dubbo:annotation,ref= "productService" and is a specified id. The actual implementation class is completed by annotation scanning, and the implementation class is not specified in the configuration file, which will be reflected in the later consumer configuration file.

In dubbo:application, you can specify the implementation interface of logger.

In dubbo:protocol, you can specify whether to start the access log, which is very helpful for troubleshooting online problems sometimes.

Dubbo consumers

The configuration of the consumer profile is much simpler than that of the provider:

Specify the name of the consumer, which is optional and does not require a match associated with the provider doing the task.

Specify the contract type, zookeeper address.

Specify the referenced service interface, and notice that the id here is the same as the ref value defined by the service provider.

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 manage dubbo services, but I did not download it successfully in the link provided by the official documentation. Although the download was completed from other places, I temporarily encountered some problems in installation and deployment, which need to be solved by formalities.

Normally, you should see the following interface:

Management provider

Manage consumers

Service governance

After the above steps, you can start the server as well as the client to verify. The above simply builds a dubbo environment and implements a service interface for hello world. There are many best practices to provide to make good use of dubbo, such as service governance:

Local stub

Local camouflage

Result caching

Multiple editions

Service degradation

.

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

Network Security

Wechat

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

12
Report