1, Installing zookeeper
1. Copy the new configuration file and modify the content in the configuration
2. Start zookeeper
3. Connect to zookeeper using zkCli.cmd
Test passed
2, Install the console
1. Package under Dubbo admin
2. Enter the following command
3. Put the generated jar package in the root directory
4. Log in at localhost:7001
5. Enter the account and password to log in. The default is root
3, Build test environment
1. Create three modules
The Gmail interface is introduced into the other two modules
Add dependencies to the pom in the other two modules
(1)gmail-interface
1) UserAddress
package com.hei.gmall.bean; import java.io.Serializable; public class UserAddress implements Serializable { private Integer id; private String userAddress; //User address private String userId; //User id private String consignee; //consignee private String phoneNum; //Telephone number private String isDefault; //Default address Y - yes N - no public UserAddress() { super(); // TODO Auto-generated constructor stub } public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) { super(); this.id = id; this.userAddress = userAddress; this.userId = userId; this.consignee = consignee; this.phoneNum = phoneNum; this.isDefault = isDefault; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getConsignee() { return consignee; } public void setConsignee(String consignee) { this.consignee = consignee; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getIsDefault() { return isDefault; } public void setIsDefault(String isDefault) { this.isDefault = isDefault; } }
2)OrderService
package com.hei.gmall.service; public interface OrderService { /** * Initialize order */ public void initOrder(String userId); }
3)UserService
package com.hei.gmall.service; import com.hei.gmall.bean.UserAddress; import java.util.List; public interface UserService { /** * Return all shipping addresses according to user id * @param userId * @return */ public List<UserAddress> getUserAddressList(String userId); }
(2)order-service-consumer
OrderServiceImpl
package com.hei.gmall.service.impl; import com.hei.gmall.bean.UserAddress; import com.hei.gmall.service.OrderService; import com.hei.gmall.service.UserService; import java.util.List; /** * 1,Register the service provider with the registry (expose the service) * 1),Import dubbo dependency (2.6.2) * 2), * * 2,Let consumers go to the registry to subscribe to the service address of the service provider */ public class OrderServiceImpl implements OrderService { UserService userService; @Override public void initOrder(String userId) { //1. Query user's address List<UserAddress> addressList = userService.getUserAddressList(userId); System.out.println(addressList); } }
(3)user-service-provider
4, The provider introduces the Dubbo dependency for registration
1. Create a configuration file under the resource of the user 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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--1,Specify the current service/Application name(The same service has the same name. Do not have the same name as other services)--> <dubbo:application name="user-service-provider"></dubbo:application> <!--2,Specify the location of the registry--> <!--<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry> <!--3,Specify communication rules (Communication Protocol)? Service port)--> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!--4,Expose services for others to call ref Point to the real implementation object of the service--> <dubbo:service interface="com.hei.gmall.service.UserService" ref="userServiceImpl"></dubbo:service> <!--Service implementation--> <bean id="userServiceImpl" class="com.hei.gmall.service.impl.UserServiceImpl"></bean> </beans>
2. Create a startup class
package com.hei.gmall; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class MainApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml"); ioc.start(); System.in.read(); } }
3. Start test class
(1) Console effect
(2) Page display effect
Provider
application
5, Consumer introduces Dubbo dependency for registration
1. Introducing dependencies in pom
2. Create counter.xml under resource
<?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://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--Packet scanning--> <context:component-scan base-package="com.hei.gmall.service.impl"/> <!--Specify the current service/Application name(The same service has the same name. Do not have the same name as other services)--> <dubbo:application name="order-service-consumer"></dubbo:application> <!--Specify the location of the registry--> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!--Call the remote exposed service and generate the remote service proxy--> <dubbo:reference interface="com.hei.gmall.service.UserService" id="userService"></dubbo:reference> </beans>
3. Add two annotations to OrderServiceImpl and modify the code
package com.hei.gmall.service.impl; import com.hei.gmall.bean.UserAddress; import com.hei.gmall.service.OrderService; import com.hei.gmall.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * 1,Register the service provider with the registry (expose the service) * 1),Import dubbo dependency (2.6.2) * 2), * * 2,Let consumers go to the registry to subscribe to the service address of the service provider */ @Service public class OrderServiceImpl implements OrderService { @Autowired UserService userService; @Override public void initOrder(String userId) { System.out.println("user id: "+userId); //1. Query user's address List<UserAddress> addressList = userService.getUserAddressList(userId); for (UserAddress userAddress : addressList) { System.out.println(userAddress.getUserAddress()); } } }
4. Write a startup method
package com.hei.gmall; import com.hei.gmall.service.OrderService; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class MainApplication { @SuppressWarnings("resource") public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml"); OrderService orderService = applicationContext.getBean(OrderService.class); //Call the method to query the data orderService.initOrder("1"); System.out.println("Call complete..."); System.in.read(); } }
@SuppressWarnings notes - morganlin - blog Park
5. Testing
6, Install and start the easy console
localhost:8080 , You can see a monitoring center.
Configure the following contents in the xml of the service provider and consumer, and start the service provider and consumer startup classes again.
<!--dubbo-monitor-simple Configuration discovered by monitoring center--> <dubbo:monitor protocol="registry"></dubbo:monitor> <!--<dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>-->
7, Dubbo and SpringBoot integration
Boot user service provider
Create Maven project boot-user-service-provider Service provider
Import the following dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.hei.gmall</groupId> <artifactId>gmail-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies>
hold user-service-provider The service in gets into this project.
Note that the void in the interface package needs to be changed to List for the returned by this method
@Service @Component public class UserServiceImpl implements UserService { public List<UserAddress> getUserAddressList(String userId) { UserAddress address1 = new UserAddress(1, "Songling Building 2, Gongyi City, Zhengzhou, Henan Province F", "1", "safely", "150360313x", "Y"); UserAddress address2 = new UserAddress(2, "Shayang Road, Shahe Town, Changping District, Beijing", "1", "Love words", "1766666395x", "N"); return Arrays.asList(address1,address2); } }
to configure application.properties
dubbo.application.name=boot-user-service-provider dubbo.registry.address=127.0.0.1:2181 dubbo.registry.protocol=zookeeper dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #Connect to the monitoring center dubbo.monitor.protocol=registry
BootProviderApplication startup class configuration
@EnableDubbo //Enable annotation based dubbo @SpringBootApplication public class BootProviderApplication { public static void main(String[] args) { SpringApplication.run(BootProviderApplication.class, args); } }
Final package directory
Boot order service consumer
Create Maven project boot-order-service-consumer Service consumers
Import the following dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.hei.gmall</groupId> <artifactId>gmail-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies>
Copy the service in the order service consumer project to the current project.
@Service public class OrderServiceImpl implements OrderService { @Autowired UserService userService; @Override public List<UserAddress> initOrder(String userId) { System.out.println("user id: "+userId); //1. Query user's address List<UserAddress> addressList = userService.getUserAddressList(userId); return addressList; } }
Create OrderController controller
@Controller public class OrderController { /** * Initialize order */ @Autowired OrderService orderService; @RequestMapping("/initOrder") @ResponseBody public List<UserAddress> initOrder(@RequestParam("uid")String userId) { return orderService.initOrder(userId); } }
Create application.properties configuration
server.port=8081 dubbo.application.name=boot-order-service-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 #Connection monitoring center registry protocol dubbo.monitor.protocol=registry
BootConsumerApplication Start class creation
@EnableDubbo //Enable annotation based dubbo @SpringBootApplication public class BootConsumerApplication { public static void main(String[] args){ SpringApplication.run(BootConsumerApplication.class,args); } }
Final package directory
After configuration, start zookeeper registry and monitoring.
Start the service provider and consumer of the spring boot configuration
Enter localhost:7001 in the browser to view the results
Address information found
The springboot integration configuration of duboo is completed.