赞
踩
|-- dubbo-demo (父级工程)
|-- dubbo-demo-core (基础工程)
|-- dubbo-consumer (消费者)
|-- dubbo-provider (生产者)
SpringBoot版本:2.2.x
Dubbo版本:2.7.3
Nacos版本:1.1.4
dubbo-demo父工程版本控制:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.9.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <compiler.plugin.version>3.8.1</compiler.plugin.version> <war.plugin.version>3.2.3</war.plugin.version> <jar.plugin.version>3.1.2</jar.plugin.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <dubbo-spring-boot-starter.version>2.7.3</dubbo-spring-boot-starter.version> <nacos-client.version>1.1.4</nacos-client.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.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos-client.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> </dependencies> </dependencyManagement>
Consumer和Provider的Maven依赖如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>dubbo-demo-core</artifactId> <version>1.0.0</version> </dependency> </dependencies>
Provider配置如下:
server.port=8081
spring.application.name=dubbo-provider-demo
# Dubbo
dubbo.scan.base-packages=com.nari.dubbo.provider.service
dubbo.application.name=${spring.application.name}
# 禁用QOS同一台机器可能会有端口冲突现象
#dubbo.application.name.qos-enable=false
#dubbo.application.name.qos-accept-foreign-ip=false
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
## Dubbo Registry
dubbo.registry.address=nacos://localhost:8848
Consumer配置如下:
server.port=8082
spring.application.name=dubbo-consumer-demo
#dubbo.application.name=${spring.application.name}
## Dubbo Registry
dubbo.registry.address=nacos://localhost:8848
#dubbo.consumer.timeout=4000
package com.nari.dubbo.core;
public interface DemoService {
String sayHello(String name);
}
在启动类上面不要忘记加上@EnableDubbo注解
package com.nari.dubbo.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
实现DemoService接口,注意这里的@Serivce引用的是dubbo的包
package com.nari.dubbo.provider.service; import com.nari.dubbo.core.DemoService; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.Service;//dubbo包下 import org.springframework.beans.factory.annotation.Value; @Slf4j @Service(version = "${dubbo-demo.version}") public class DemoServiceImpl implements DemoService { @Value("${dubbo.application.name}") private String serviceName; @Override public String sayHello(String name) { log.info("provider被调用"); return String.format("[%s] : Hello, %s", serviceName, name); } }
和Provider工程的启动类一样,加上@EnableDubbo注解
编写测试接口:
package com.nari.dubbo.consumer.controller; import com.nari.dubbo.core.DemoService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/test") public class TestController { @Reference(version = "${dubbo-demo.version}") private DemoService demoService; @GetMapping(value = "/test/{name}") public String sayHello(@PathVariable("name") String name) { return demoService.sayHello(name); } }
启动Provider工程和Consumer工程,这个时候Nacos会有对应的服务:
使用浏览器请求consumer:
http://localhost:8082/test/test/测试
provider控制台打印日志,说明调用成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。