依赖版本:
spring-boot:2.3.12.RELEASE
spring-cloud-alibaba:2.2.7.RELEASE
spring-cloud:Hoxton.SR12
nacos:2.0.3
1.部署搭建Nacos注册中心
Linux Nacos 快速启动_nacos linux快速启动-CSDN博客
2.构建项目
源码地址
2.1 项目目录
2.2 根pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.3.12.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.ocean</groupId>
<artifactId>dubbo-cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-order</module>
<module>dubbo-user</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.version>
</properties>
<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>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Dubbo Spring Cloud Starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-context-support</artifactId>
<groupId>com.alibaba.spring</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Cloud Nacos Service Discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
</dependency>
</dependencies>
</project>
2.3 服务提供者
2.3.1 目录结构
2.3.2 pom.xml
<!-- dubbo-user pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-cloud</artifactId>
<groupId>com.ocean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-user</artifactId>
<packaging>pom</packaging>
<modules>
<module>dubbo-user-api</module>
<module>dubbo-user-provider</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
<!-- dubbo-user-api pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-user</artifactId>
<groupId>com.ocean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-user-api</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.ocean</groupId>
<artifactId>dubbo-user-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<!-- dubbo-user-provider propom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-user</artifactId>
<groupId>com.ocean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>dubbo-user-provider</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3.3 dubbo-user-provideer代码
目录
代码
UserProviderService.java
package com.ocean.provider;
public interface UserProviderService {
String getUserName();
}
2.3.4 dubbo-user-api代码
目录
代码
UserApi.java
package com.ocean.controller;
import com.ocean.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserApi {
@Resource
private UserService userService;
@GetMapping(value = "/api/user/getName")
public String getOrderInfo() {
return userService.getName();
}
}
UserProviderServiceImpl.java
package com.ocean.provider;
import com.ocean.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
@DubboService
public class UserProviderServiceImpl implements UserProviderService {
@Resource
private UserService userService;
@Override
public String getUserName() {
return userService.getName();
}
}
UserService.java
package com.ocean.service;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class UserService {
public String getName() {
return UUID.randomUUID().toString();
}
}
UserApplication.java
package com.ocean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
application.yml
server:
port: 6668
spring:
application:
name: user
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
main:
allow-bean-definition-overriding: true
dubbo:
application:
qos-enable: false
id: user
scan:
base-packages: com.ocean.provider
protocols:
dubbo:
name: dubbo
port: -1
registry:
address: spring-cloud://127.0.0.1
2.4 服务消费者
2.4.1 目录结构
2.4.2 pom.xml
<!-- dubbo-order pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-cloud</artifactId>
<groupId>com.ocean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-order</artifactId>
<packaging>pom</packaging>
<modules>
<module>dubbo-order-api</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
<!-- dubbo-order-api pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-order</artifactId>
<groupId>com.ocean</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-order-api</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.ocean</groupId>
<artifactId>dubbo-user-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.4.3 代码
OrderApi.java
package com.ocean.controller;
import com.ocean.service.OrderService;
import com.ocean.vo.OrderVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class OrderApi {
@Resource
private OrderService orderService;
@GetMapping(value = "/api/order/{id}")
public OrderVO getOrderInfo(@PathVariable("id") String id) {
return orderService.getOrderInfo(id);
}
}
OrderService.java
package com.ocean.service;
import com.ocean.provider.UserProviderService;
import com.ocean.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@DubboReference
private UserProviderService userProviderService;
public OrderVO getOrderInfo(String id) {
return new OrderVO(id, userProviderService.getUserName());
}
}
OrderVO.java
package com.ocean.vo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ToString
@Getter
@Setter
public class OrderVO {
private String orderId;
private String userName;
public OrderVO(String orderId, String userName) {
this.orderId = orderId;
this.userName = userName;
}
}
OrderApplication.java
package com.ocean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
application.yml
server:
port: 6667
spring:
application:
name: order
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
main:
allow-bean-definition-overriding: true
dubbo:
cloud:
subscribed-services: user
application:
qos-enable: false
id: order
scan:
base-packages: com.ocean.provider
protocols:
dubbo:
name: dubbo
port: -1
registry:
address: spring-cloud://127.0.0.1
https://download.csdn.net/download/qq_34253002/88522267?spm=1001.2014.3001.5501