创建工程
父工程 pom(主要实现版本仲裁)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.12</version> <relativePath/> </parent>
<groupId>com.juzi</groupId> <artifactId>dubbo-demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>dubbo-api</module> <module>dubbo-api-provider</module> <module>dubbo-api-consumer</module> </modules>
<properties> <java.version>11</java.version> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring.boot.version>2.7.12</spring.boot.version> <spring-cloud.version>2021.0.8</spring-cloud.version> <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version> <dubbo.version>3.0.12</dubbo.version> </properties>
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> </dependencies> </dependencyManagement>
</project>
|
dubbo-api模块
pom.xml(不需要引入任何依赖)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.juzi</groupId> <artifactId>dubbo-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<artifactId>dubbo-api</artifactId>
</project>
|
User.java
package com.juzi.api.pojo;
import lombok.Data;
import java.io.Serializable;
@Data public class User implements Serializable {
private static final long serialVersionUID = -5266734237094421690L; private String name; }
|
UserService.java
(只提供接口,不提供实现)
package com.juzi.api.service;
import com.juzi.api.pojo.User;
public interface UserService { String getUserName(User user);
String sayHello(String name); }
|
dubbo-api-provider模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.juzi</groupId> <artifactId>dubbo-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<artifactId>dubbo-api-provider</artifactId>
<dependencies> <dependency> <groupId>com.juzi</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
</project>
|
application.yml
server: port: 8211 spring: application: name: dubbo-api-provider cloud: nacos: discovery: server-addr: 192.168.0.101:8848 dubbo: registry: id: nacos-registry address: nacos://192.168.0.101:8848 protocol: name: dubbo port: -1 scan: base-packages: com.juzi.dubbo.service
|
启动类(一定要加上@EnableDubbo注解)
package com.juzi.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @EnableDubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
|
UserServiceImpl.java
(加上@DubboService注解,暴露服务)
package com.juzi.dubbo.service;
import com.juzi.api.pojo.User; import com.juzi.api.service.UserService; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboService;
@Slf4j @DubboService public class UserServiceImpl implements UserService {
@Override public String getUserName(User user) { log.info("getUserName: {}", user); return user.getName(); }
@Override public String sayHello(String name) { log.info("sayHello: {}", name); return "hello " + name; } }
|
dubbo-api-consumer模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.juzi</groupId> <artifactId>dubbo-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<artifactId>dubbo-api-consumer</artifactId>
<dependencies> <dependency> <groupId>com.juzi</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency>
</dependencies>
</project>
|
application.yml
server: port: 8221 spring: cloud: nacos: discovery: server-addr: 192.168.0.101:8848 register-enabled: false application: name: dubbo-api-consumer dubbo: registry: id: nacos-registry address: nacos://192.168.0.101:8848 protocol: name: dubbo port: -1
|
启动类(加上@EnableDubbo注解)
package com.juzi.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
|
UserController.java
package com.juzi.dubbo.controller;
import com.juzi.api.pojo.User; import com.juzi.api.service.UserService; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class UserController {
@DubboReference private UserService userService;
@GetMapping("/user") public String getUserName(User user) { return userService.getUserName(user); }
@GetMapping("/say") public String sayHello(String name) { return userService.sayHello(name); } }
|
启动工程
1)启动nacos,配置合适的nacos注册地址
2)启动dubbo-api-provider模块
3)启动dubbo-api-consumer模块
4)访问地址:
- http://localhost:8221/say/name=xxx
- http://localhost:8221/user/name=xxx
注意事项
- Dubbo版本尽量选用高版本,原因看issue1、[issue2](