In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how springboot uses dubbo and zookeeper. I hope you will get something after reading this article. Let's discuss it together.
Create a service interface module
The interface project provides only interfaces, not implementations, and is used in later providers and consumers.
Only the specific implementation class needs to be written in the module that uses the interface, which avoids repeatedly writing the interface in each module.
Introduce dependency packages into the interface
Org.projectlombok lombok true
To create an entity class, be sure to implement the Serializable interface, otherwise it cannot be transferred between dubbo protocols
@ Data@AllArgsConstructorpublic class User implements Serializable {private String name;}
Create an interface
Public interface UserService {List getAll (); List getAll2 ();}
Create a service provider
Introduce dependency
Com.yls common-api 1.0-SNAPSHOT org.springframework.boot spring-boot-starter com.alibaba.boot dubbo-spring-boot-starter 0.2.0
Modify the configuration file
# Service name dubbo.application.name=provider1# registry address dubbo.registry.address=39.97.234.52:2181,39.97.234.52:2182,39.97.234.52:2183# registry type dubbo.registry.protocol=zookeeper# version number dubbo.application.version=3# Dubbo Protocol# protocol name dubbo.protocol.name=dubbo# service exposed port dubbo.protocol.port=20880
Implement the service interface
/ / exposure service / / the @ Service here is provided by Dubbo and is not required in spring @ Service (version = "${dubbo.application.version}") @ Componentpublic class UserImpl implements UserService {@ Override public List getAll () {User user1 = new User ("Zhang San"); User user2 = new User ("lisi"); List list = Arrays.asList (user1, user2); return list;}}
Start the service
/ / @ EnableDubbo is equivalent to configuring the package where the dubbo.scan.base-packages// scanning implementation class is located in the configuration file, registering Bean@EnableDubbo@SpringBootApplicationpublic class ProviderApplication {public static void main (String [] args) {SpringApplication.run (ProviderApplication.class,args);}}
Create consumers
Introduce dependency
Org.springframework.boot spring-boot-starter-web com.alibaba.boot dubbo-spring-boot-starter 0.2.0 com.yls common-api 1.0-SNAPSHOT
Modify the configuration file
# registry dubbo.registry.address=39.97.234.52:2181,39.97.234.52:2182,39.97.234.52:2183dubbo.registry.protocol=zookeeper#dubbo application name dubbo.application.name=consumer1
Implement the consumption interface
/ / the Service of the service consumer is @ Servicepublic class OrderImpl implements OrderService of spring {/ / access the remote service using @ Reference provided by dubbo / / version @ Reference (version = "3") private UserService userService; @ Override public List init () {List list = userService.getAll (); list.forEach (item-> System.out.println (item.getName ()); return list;}}) of the corresponding service provider.
Create controller
Controllerpublic class OrderController {@ Autowired private OrderService orderService; @ ResponseBody @ RequestMapping ("/ init") public List init () {return orderService.init ();}}
Start the service consumer
@ EnableDubbo@SpringBootApplicationpublic class ConsumerApplication {public static void main (String [] args) {SpringApplication.run (ConsumerApplication.class, args);}}
Dubbo related supplement
Dubbo uses local caching. If the registry is down, dubbo can work normally. Even if there is no registry, dubbo can communicate directly through dubbo.
/ / use @ Reference provided by dubbo to access the remote service / / version corresponding service provider's version / / url: if there is no registration center, you can configure the dubbo protocol port of the service provider through url to connect directly to @ Reference (version = "3", url = "127.0.0.1 version 20880") private UserService userService
After reading this article, I believe you have a certain understanding of "how springboot uses dubbo and zookeeper". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.