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 realize distributed Development and Application by springBoot+dubbo+zookeeper

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you springBoot+dubbo+zookeeper how to achieve distributed development applications, I hope you will learn something after reading this article, let's discuss it together!

Environment building

Project structure diagram:

1. First of all, let's do a good job of the server.

Pom.xml

Org.apache.dubbo dubbo-spring-boot-starter 2.7.3 com.github.sgroschupf zkclient 0.1 org.apache.curator curator-framework 2.12.0 org.apache. Curator curator-recipes 2.12.0 org.apache.zookeeper zookeeper 3.4.14 org.slf4j slf4j-log4j12 Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test

Application.properties

Server.port=8001# service application name dubbo.application.name=provider-server# registry address dubbo.registry.address=zookeeper://127.0.0.1:2181# which services are to be registered dubbo.scan.base-packages=com.wlm.service

TicketService

Package com.wlm.service;public interface TicketService {public String getTicket ();}

TicketServiceImpl.java

Package com.wlm.service;import org.apache.dubbo.config.annotation.Service;import org.springframework.stereotype.Component;//zookeeper: service registration and discovery @ Service / / can be scanned. Try not to use Service to annotate public class TicketServiceImpl implements TicketService {@ Override public String getTicket () {return "salted fish _ turn over";}} 2. Start the server

Open the server, do not click on it, and let it run in the background:

Execute the jar package on the server side. We need to monitor the server side later:

Run the server code above, and then check it in the browser

Http://localhost:7001/

3. Let's do the client again.

Pom.xml

Org.apache.dubbo dubbo-spring-boot-starter 2.7.3 com.github.sgroschupf zkclient 0.1 org.apache.curator curator-framework 2.12.0 org.apache. Curator curator-recipes 2.12.0 org.apache.zookeeper zookeeper 3.4.14 org.slf4j slf4j-log4j12 Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test

Application.properties

Server.port=8002# consumers where to get services, need to expose their name dubbo.application.name=consumer-server# registry address, can be on any computer! Dubbo.registry.address=zookeeper://127.0.0.1:2181

TicketService

Package com.wlm.service;public interface TicketService {public String getTicket ();}

UserService.java

Package com.wlm.service;import org.apache.dubbo.config.annotation.Reference;import org.springframework.stereotype.Service;@Service / / put into the container, pay attention to distinguish between public class UserService {/ / if you want to get the ticket provided by provider-server, to go to the registry to get the service @ Reference / / reference, pom coordinates, you can define the same path of the interface name TicketService ticketService; public void buyTicket () {String ticket = ticketService.getTicket () System.out.println ("get in the registry = >" + ticket);}}

ConsumerServerApplicationTests.java

Package com.wlm;import com.wlm.service.UserService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass ConsumerServerApplicationTests {@ Autowired UserService userService; @ Test void contextLoads () {userService.buyTicket ();}

Run ConsumerServerApplicationTests.java result: get the "salted fish _ turn over" from the server

4. Summary

Premise: the zookeeper service has been enabled

1. First, the provider provides services.

Import dependency

Configure the address of the registry, as well as the service discovery name, and the package to scan

Add an annotation @ Service to the service you want to register

two。 How consumers spend

Import dependency

Configure the address of the registry and configure your own service name

Remotely inject the service @ Reference

After reading this article, I believe you have a certain understanding of "how springBoot+dubbo+zookeeper implements distributed development applications". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

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