Preparing for Integration
1, Alibaba offered dubbo integrated springboot open source projects;
3, we will use the jar package provided by the integration of the project;
<!--添加dubbo集成springboot依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
dubbo development generally recommended three projects:
Dubbo project development service interface
Dubbo project development service provider
Dubbo Consumer Project Development Services
Dubbo service interface development project to project maven [common]
According to Dubbo official development proposals to create an interface project, which only defines interfaces and model classes
Defined interface class
package com.joinlabs.dubbo.service; import com.joinlabs.dubbo.model.User; /** * Created by Administrator on 2018/10/26/026. */ public interface UserService { public User findById(int id); public String findUserName(int id); }
Define model classes
package com.joinlabs.dubbo.model;
/**
* Created by Administrator on 2018/10/26/026.
*/
public class User {
private int id;
private String name;
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Interface package for the project, using the tools to package maven
After successfully packaged the jar package will be saved to the local repository maven maven in the form of, service providers and consumers to facilitate project referenced in pom.xml
Dubbo development service providers [project] springboot
1, Springboot create a project and configuring the related dependencies;
2, integrated with added springboot dubbo starting dependence:
<!--添加dubbo集成springboot依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
3, the configuration information dubbo in the core configuration file application.properties Springboot in:
# Built-in port after the Tomcat server startup server.port = 8080 #Dubbo configuration spring.dubbo.appname = springboot-dubbo- Provider spring.dubbo.registry = ZooKeeper: // localhost: 2181
The use of the registry as a zookeeper, zookeeper is necessary to add the client jar package:
<!-- zookeeper客户端依赖start --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <!-- zookeeper客户端依赖end -->
4, write the interface implementation class of Dubbo, go try the above interfaces project service interfaces:
I want to rely on the introduction of an interface jar package in pom.xml
<dependency>
<groupId>com.joinlabs.dubbo</groupId>
<artifactId>01springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Implementing Classes:
Package com.joinlabs.dubbo.service.impl; Import com.alibaba.dubbo.config.annotation.Service; Import com.joinlabs.dubbo.model.User; Import com.joinlabs.dubbo.service.UserService; Import org.springframework .stereotype.Component; / ** . * 2018/10/26/026 the Created by Administrator oN * / @Service (the interfaceClass . UserService = class ) // prior to the annotation of Dubbo is equivalent to <dubbo: service interface = ""> </ Dubbo:-Service> @Component // the annotation is a spring, or using org.springframework.stereotype.Service @ public class UserServiceImpl the implements UserService { @Override public the User the findById ( int ID) { the User User = new new the User (); // analog data, can also be integrated into the database query data mybatis user.setId (ID); user.setName ( "name" + ID); return User; } @Override public String findUserName ( int ID) { return "name" + ID; } }
5, the main entrance to write a program to start Dubbo Service Provider:
package com.joinlabs.dubbo; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration //开启dubbo配置支持 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Installation and start-zookeeper
Download the installation package
Unzip to c root directory
Modifying profile names zookeeper-3.4.10 \ conf \ zoo_sample.cfg to zoo.cfg
Edit the configuration file content
for
Into the zookeeper-3.4.10 \ bin directory, and start zkServer.cmd, do not close the console window
You can see the process through jsp command
Through built-in client to connect
Open a new console, execute the command zkCli.cmd
Dubbo develop consumer service project] [springboot
1, Springboot create a project and configuring the related dependencies;
2, integrated with added springboot dubbo starting dependence:
<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
3, the configuration information dubbo in the core configuration file application.properties Springboot in:
# WEB service port server.port = 9090 # Dubbo configuration spring.dubbo.appname = springboot-dubbo- Consumer spring.dubbo.registry = ZooKeeper: // 192.168.91.129:2181
The use of the registry as a zookeeper, zookeeper is necessary to add the client jar package:
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
4, write a Controller class, call the remote service Dubbo:
@Controller public class the UserController { @Reference // using annotations dubbo references remote dubbo service Private UserService userService; @ RequestMapping ( "/ sayHi" ) public @ResponseBody sayHi String () { return userService.sayHi ( "the Boot dubbo the Spring .. .... " ); } }
5, the main entrance to write a program to start Dubbo Service Provider:
@SpringBootApplication @EnableDubboConfiguration //开启dubbo配置支持 public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }