Source code download address:
https://download.csdn.net/download/weixin_43195884/87409766
Use cases of nacos integrated dubbo
1. Create a parent project.
2. Create a sub-project API to store the service codes that depend on each module
3. Create a project that provides an interface for the subproject server
5. Create a sub-project consumer project client
5. Introduce dependencies in client project and provider project
<!--导入simpleapi依赖-->
<dependency>
<groupId>org.simpleapi</groupId>
<artifactId>simple-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot启动类的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!-- springboot启动类的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
6. Write code in the provider project. First, introduce the annotation in the main class: @EnableDiscoveryClient
7. Configure the address of dubbo and the registration address of nacos in the application.properties file
#服务名称
spring.application.name=provider
#dubbo默认的扫描包路径
dubbo.scan.base-packages=com.provider.service
#配置dubbo的端口 可以是-1 代表从20880自选端口 这里是单机 无需配置
dubbo.protocol.port=20880
#配置dubbo的名称
dubbo.protocol.name=dubbo
#设置nacos的注册地址
spring.cloud.nacos.discovery.server-addr=localhost:8848
8. Write the serviceipl implementation class in the provider project and implement the code. Note that dubbo’s service annotation needs to be used here
@Service
public class OrderServiceImpl implements OrderService {
@Override
public String getOrderStatus() {
return "订单已完成";
}
}
9. Open the client consumer project and add the annotation @enableddiscoveryclient to the main class
10. Configure the service name and nacos registration address in the cilent project
#服务名称
spring.application.name=client
#设置nacos的注册地址
spring.cloud.nacos.discovery.server-addr=localhost:8848
11. Write the controller code, note that you need to use dubbo's reference annotation here
@RestController
public class OrderController {
//使用dubbo注解 注入远程调用的接口
@Reference
OrderService orderService;
@RequestMapping("getOrderInfo")
public String getOrderInfo(){
return orderService.getOrderStatus();
}
@RequestMapping("getOrderInfo1")
public String getOrderInfo1(){
return "success";
}
}
12. Start the local nacos service, start the provider, client project, nacos use can refer to:
Nacos startup: Open the black window and enter the directory: D:\nacos\nacos\bin
Enter the command: startup.cmd -m standalone
13. The browser accesses the controller interface and returns the following information:
Use dubbo+zookeeper+springboot to realize service registration and discovery
1. The above project is still used here, and the dependencies of dubbo and zookeeper are introduced:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 引入zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12 -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
2. Configure the following information in the application.properties file:
#服务名称
spring.application.name=provider
#dubbo默认的扫描包路径
dubbo.scan.base-packages=com.provider.service
#配置dubbo的端口 可以是-1 代表从20880自选端口 这里是单机 无需配置
dubbo.protocol.port=20880
#配置dubbo的名称
dubbo.protocol.name=dubbo
##设置nacos的注册地址
#spring.cloud.nacos.discovery.server-addr=localhost:8848
#将服务注册到zookeeper服务器上
dubbo.registry.address=zookeeper://192.168.56.10:2181
3. In the service provision project, write the service class, note that the service annotation here is dubbo
import org.apache.dubbo.config.annotation.Service;
import org.simpleapi.service.OrderService;
@Service
public class OrderServiceImpl implements OrderService {
@Override
public String getOrderStatus() {
return "订单已完成";
}
}
4. Also import the above dependencies in the consumer, and at the same time, call the service interface. Note that the reference annotation used here is dubbo
import org.apache.dubbo.config.annotation.Reference;
import org.simpleapi.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
//使用dubbo注解 注入远程调用的接口
@Reference
OrderService orderService;
@RequestMapping("getOrderInfo")
public String getOrderInfo(){
return orderService.getOrderStatus();
}
@RequestMapping("getOrderInfo1")
public String getOrderInfo1(){
return "success";
}
}
5. Similarly, zookeeper registration messages also need to be configured in the application.properties file
#服务名称
spring.application.name=client
##设置nacos的注册地址
#spring.cloud.nacos.discovery.server-addr=localhost:8848
#将服务注册到zookeeper服务器上
dubbo.registry.address=zookeeper://192.168.56.10:2181
6. The browser accesses the controller interface and returns the following information: