文章目录
- 1. 模块划分设计
- 2. 创建父工程
- 3. 创建公共接口
- 4. 服务端
- 5. 客户端
- 6. nacos
- 7. 测试
- 8. 码云开源地址
1. 模块划分设计
模块名 | 工程名 | 端口 |
---|---|---|
父工程 | nacos-dubbo | 无 |
服务端 | nacos-dubbo-provider | 9000 |
消费端 | nacos-dubbo-consumer | 8000 |
公共接口 | nacos-dubbo-interface | 无 |
2. 创建父工程
nacos-dubbo
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><modules><module>nacos-dubbo-interface</module><module>nacos-dubbo-provider</module><module>nacos-dubbo-consumer</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></properties><dependencies><!--服务注册发现--><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-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><dependencyManagement><dependencies><!--spring-cloud-alibaba 版本控制--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
3. 创建公共接口
package com.cloud.alibaba.dubbo.service;public interface DubboService {String hello(String name);
}
4. 服务端
package com.cloud.alibaba.dubbo.service.impl;import com.cloud.alibaba.dubbo.service.DubboService;/*** dubbo 服务端*/
@org.apache.dubbo.config.annotation.DubboService
public class DubboServiceImpl implements DubboService {@Overridepublic String hello(String name) {return "hello " + name;}
}
package com.cloud.alibaba.dubbo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class);}
}
server:port: 9000
spring:application:name: nacos-dubbo-providercloud:nacos:discovery:server-addr: http://localhost:8848config:server-addr: http://localhost:8848file-extension: yaml
# Dubbo服务配置
dubbo:scan:base-packages: com.cloud.alibaba.dubbo.service.implprotocol:name: dubboport: -1registry:address: spring-cloud://localhost
<dependency><groupId>com.gblfy</groupId><artifactId>nacos-dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency>
5. 客户端
package com.cloud.alibaba.dubbo.controller;import com.cloud.alibaba.dubbo.service.DubboService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class DubboController {@DubboReferenceprivate DubboService dubboService ;@GetMapping("/hello")public String hello (String name) {return dubboService.hello(name) ;}
}
package com.cloud.alibaba.dubbo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class);}
}
server:port: 8000
spring:application:name: nacos-dubbo-clientcloud:nacos:discovery:server-addr: http://localhost:8848config:server-addr: http://localhost:8848
# Dubbo服务配置
dubbo:protocol:name: dubboport: -1registry:address: spring-cloud://localhostcloud:subscribed-services: nacos-dubbo-provider
<dependency><groupId>com.gblfy</groupId><artifactId>nacos-dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency>
6. nacos
7. 测试
请求地址
http://localhost:8000/hello?name=gblfy