A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


spring-boot-dubbo-api
这Dome比较简单,只写了一个接口;
                package com.cookie.dubbo.api.service;
import java.util.Map;
/**
* author : 万波
* Date : 2019/7/28
*/
public interface AccountService {
public String getAccountById(String id );
}
使用idea依次执行clean , install ;
spring-boot-dubbo-provider 服务提供者;
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入dubbo的场景启动器-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!-- 引入zookeeper的客户端 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--引入公共api-->
<dependency>
<groupId>com.cookie</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
application.yml:
server :
port : 8002
dubbo:
application:
name: spring-boot-dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181
scan:
base-packages: com.cookie.dubbo.provider.service
AccountServiceImpl:
package com.cookie.dubbo.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.cookie.dubbo.api.service.AccountService;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* author : 万波
* Date : 2019/7/28
*/
@Component
@Service
public class AccountServiceImpl implements AccountService {
@Override
public String getAccountById(String id) {
return " it is provider ! ";
}
}
注意: @Service是引入的com.alibaba.dubbo.config.annotation.Service
spring-boot-dubbo-consumer 服务消费者:
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 引入zookeeper的客户端 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--引入公共api-->
<dependency>
<groupId>com.cookie</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
application.xml:
server :
port : 8003
dubbo :
application :
name : spring-boot-dubbo-consumer
registry :
address: zookeeper://127.0.0.1:2181
scan :
base-packages : com.cookie.dubbo.consumer.controller
AccountController :
package com.cookie.dubbo.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.cookie.dubbo.api.service.AccountService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* author : 万波
* Date : 2019/7/28
*/
@RequestMapping("account")
@RestController
public class AccountController {
@Reference
private AccountService accountService ;
@RequestMapping("")
public String getAccountById(String id ){
return accountService.getAccountById(id);
}
}
一次启动 provider和consumer,postman调用接口:


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马