First, environmental preparation
1. Install ZooKeeper in docker environment
# Install zookeeper # #
# docker pull zookeeper
# # # to start the zookeeper
# docker run --name zookeeper-01 --restart always -d -p 2181:2181 zookeeper
# ## Deploy dubo-admin service monitoring platform
# Download dubo-admin-2.6.6. war package and deploy tomcat container in docker environment
# docker pull tomcat
# ## Pull Tomcat from docker hub and upload the war package to a directory like /home/dubo-admin-2.6.0.war on the host machine
# ## Start Tomcat and check tomcat startup with your browser at http://host_path:8080. Remember to change the ZK IP in the Dubbo-admin configuration file to the IP of the ZK container, not the IP of the host machine (sinkhole).
# Check the DOCker container IP
# docker inspect 364c77111927| grep IPAddress
# docker run -it --name dubbo-admin -d --rm -p 8080:8080 tomcat
# Enter the Docker container and check the Tomcat container path
# docker exec -i -t <docker_name> bash
# Copy the host machine's war package into the docker container:
#Copy from host to containerSudo docker cp host_path: containerID:container_path# ## Copy from container to host
# docker cp containerID:container_path host_path
# For example, after replication and automatic deployment, visit the dubo-admin service management platform http://host_path:8080/dubbo-admin-2.6.0
#Docker cp/home/dubbo - admin - server. War 603140 acbd3c: / usr /local/tomcat/webapps
# Otherwise, you will need to redeploy the image on the next startup
docker commit -m "message" -a "author" ${CONTAINER_ID} ${NEW_IMAGE_NAME}
Copy the code
Ii. Project configuration
Server Configuration
1. Add dubbo dependencies to POM files
<! -- Spring Boot Dubbo dependency -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
Copy the code
2. Application. Properties configuration
# Dubbo service provider configuration
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper:/ / 127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.wxx.demo.dubbo
Copy the code
The address and port configured by ZK are the ZK constructed by the local machine above. You can modify the following configuration if you have your own ZK. The configuration is explained as follows: Spring. Dubbo. Application. The application name name spring. The dubbo. Registry. The center address registered address spring. The dubbo. Protocol. The name protocol name Spring. The dubbo. Protocol. The port protocol port spring. The dubbo. Scan dubbo service class 3, service provider code package directory
@Component// Put the service into the Spring container
@Service(version = "1.0.0")// Use the service annotation provided by Dubbo to provide the version number through version
public class MallOrderServiceImpl implements MallOrderService {
@Autowired
private MallOrderMapper orderMapper;
@Override
public MallOrder getOrderById(String orderId) {
return orderMapper.selectByPrimaryKey(orderId);
}
@Override
public Integer saveOrder(MallOrder order) {
returnorderMapper.insert(order); }}Copy the code
The MallOrder entity implements the serialization interface
Client Configuration
1. Add dependencies. 2
Avoid conflicts with server project ports
server.port=8081
## Dubbo service consumer configuration
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper:/ / 127.0.0.1:2181
spring.dubbo.scan=com.wxx.demo.dubbo
Copy the code
3. Consumer code
@Component
public class DubboConsumerService {
@Reference(version = "1.0.0")// @reference (version = "1.0.0") use this annotation to subscribe to the Dubbo service with the interface version of 1.0.0.
MallOrderService mallOrderService ;
public void printCity(a) {
String orderId="TD106986521"; MallOrder order = mallOrderService .getOrderById(orderId); System.out.println(order .toString()); }}Copy the code
Sum up the pits
1, use Dubbo annotation development, must pay attention to use
spring.dubbo.scan=com.wxx.demo.dubbo// Scan the package implemented by the service, otherwise the service cannot be registered
Copy the code
Dubbo-admin can not see the registered service. In addition to the general configuration on the Internet, check the dubbo-admin startup log to see whether there is a connection timeout error. Another is whether the Dubo-admin configuration file is configured with the IP of the ZK container rather than the IP of the host machine