目录结构如下:
一、创建maven工程
父工程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> <groupId>com.zyf</groupId> <artifactId>cloudexample</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>service-provider</module> <module>service-customer</module> </modules> <name>cloud-example</name> <description>SpringCloud demo for Nacos</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.2.7.RELEASE</spring-boot.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <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.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、创建service-provider
<?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"> <parent> <artifactId>cloudexample</artifactId> <groupId>com.zyf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service-provider</artifactId> <packaging>pom</packaging> <modules> <module>service-provider-api</module> <module>service-provider-service</module> </modules> </project>
service-provider下创建service-provider-api
<?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"> <parent> <artifactId>service-provider</artifactId> <groupId>com.zyf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service-provider-api</artifactId> </project>
创建接口:
public interface DubboTestService { String test(); }
service-provider下创建service-provider-service
<?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"> <parent> <artifactId>service-provider</artifactId> <groupId>com.zyf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service-provider-service</artifactId> <dependencies> <dependency> <groupId>com.zyf</groupId> <artifactId>service-provider-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>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> </dependencies> </project>
api接口实现:
import com.zyf.service.api.DubboTestService; public class DubboTestServiceImpl implements DubboTestService { @Override public String test() { return "dubbotest响应"; } }
servcie-provider-service的启动类及配置文件:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ServiceProviderStarter { public static void main(String[] args) { SpringApplication.run(ServiceProviderStarter.class, args); } }
server: port: 8999 spring: application: name: service-provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848 cluster-name: DEFAULT #集群默认DEFAULT #namespace: xxxxxxx #命名空间 做隔离 main: allow-bean-definition-overriding: true #出现重名直接复写 dubbo: registry: address: nacos://${spring.cloud.nacos.discovery.server-addr} scan: base-packages: com.zyf.service.api protocol: name: dubbo port: 20882 consumer: check: false #检查是否依赖服务 application: qos-enable: false #dubbo 服务是否开启 cloud: subscribed-services: ''#订阅服务,不配置默认订阅注册中心所有服务
三、创建调用方:service-customer
<?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"> <parent> <artifactId>cloudexample</artifactId> <groupId>com.zyf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service-customer</artifactId> <dependencies> <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>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.zyf</groupId> <artifactId>service-provider-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
application配置:
server: port: 8081 spring: application: name: service-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 config: server-addr: ${spring.cloud.nacos.discovery.server-addr} #配置中心地址 file-extension: yaml #文件后缀名 很根据本定文件名称拼接 dubbo: cloud: subscribed-services: 'service-provider'#订阅要调用的服务
启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CuseomerStarter { public static void main(String[] args) { SpringApplication.run(CuseomerStarter.class,args); } }
restapi测试:
import com.zyf.service.api.DubboTestService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Reference private DubboTestService dubboTestService; @GetMapping("testDubbo") @ResponseBody public String test1(){ String test = dubboTestService.test(); return test; } }
运行:
先运行nacos服务端:这里使用nacos1.2.1服务端
启动ServiceProviderStater再启动customer