1. What we want to achieve
Provider: the service provider that exposes the service. I create a Module named news provider to implement it.
Consumer: the service consumer that calls the remote service. I create a Module named news consumer to implement it.
Registry: the Registration Center for service registration and discovery, which is implemented by Zookeeper. Please refer to my previous article for specific implementation.
Monitor: the monitoring center that counts the call times and call times of the service. For the installation of Dubbo client, please refer to my previous article.
Container: service running container.
The Provider provides services and uploads them to the Register. The Monitor can manage the uploaded services. The Consumer can get the relevant information of the services provided by the Provider through the Register and consume
2. Specific implementation
1. Create a general Maven project dubbosservice, which has three sub modules: service interface module news service, which defines the service specification; service provider module news provider, which relies on the service interface module to realize its specification and provide services; service consumer module news consumer, which depends on the service interface module, obtains the corresponding services from the registration center through the service interface.
Project pom.xml
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <!--zkclient:Simplify primitives zk operation--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!--curator:ZK Connection management SESSION Handling of some abnormal problems such as failure--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.8.0</version> </dependency> </dependencies>
pom.xml for news provider and news consumer modules
<dependencies> <dependency> <groupId>org.staybug</groupId> <artifactId>news-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
2. The news service module defines the service interface
package org.staybug.service; import java.util.List; public interface NewService { List<String> getTop100News(); }
3. The news provider module implements the service interface and provides external services
Create the spring-news-provider.xml configuration file in the resources directory, and configure the following information
<dubbo:application name="news-provider" owner="zxs" organization="staybug"></dubbo:application> <! -- define registry address -- > <dubbo:registry protocol="zookeeper" address="192.168.2.10:2181"></dubbo:registry> <! -- define the service local occupation port. The defau lt port is 20880 -- > <dubbo:protocol name="dubbo" port="10084"></dubbo:protocol> <! -- publish services through dubbo Service provider exposes service configuration Interface: service interface name ref: service implementation reference -- > <dubbo:service interface="org.staybug.service.NewService" ref="newsServiceImpl"/> <bean id="newsServiceImpl" class="org.staybug.service.impl.NewServiceImpl"/>
Implement service interface
public class NewServiceImpl implements NewService { public List<String> getTop100News() { List<String> newsTitles = new ArrayList<String>(); newsTitles.add("Your article has entered the stage of manual review, please wait patiently, if it fails to pass the review within 24 hours, please contact customer service, thank you."); return newsTitles; } }
External services
public class ProviderApp { public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-news-provider.xml"); //Start the Spring container context.start(); try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } } }
4. News consumer module for consumption
Create the spring-news-consumer.xml configuration file in the resources directory, and configure the following information
<!--Configure consumer's application information--> <dubbo:application name="news-consumer" owner="zxs" organization="staybug"/> <!--Define the address of the registration center that the consumer service needs to access, which is the same as that of the producer--> <dubbo:registry protocol="zookeeper" address="192.168.2.10:2181"/> <!--Reference producer service, service consumer reference service configuration 1.Find the interface service name from the registry as org.staybug.service.NewService Producer service address for: dubbo://192.168.159.1:10080/org.staybug.service.NewService?anyhost=true &application=news-provider&dubbo=2.6.2&generic=false&interface=org.staybug.service.NewService &methods=getTop100News&organization=staybug&owner=zxs&pid=6996&side=provider×tamp=1588247739218 2.Resolve production service address, get ip,Port, interface name, method name 3.Select one of the producers and initiate RPC Call, return results --> <dubbo:reference interface="org.staybug.service.NewService" id="newsService"/>
Consume
public class ConsumerApp { public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-news-consumer.xml"); NewService newService= context.getBean(NewService.class); while (true){ System.out.println(); List<String> top100News = newService.getTop100News(); for (String newItem : top100News){ System.out.println(newItem); } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
5. The console will print the following information