准备
因为使用springboot2.0,需要jdk1.8
然后需要对springboot基本了解
搭建注册中心(zookerper)
1、下载windows版本的zookerper
下载地址:https://zookeeper.apache.org/releases.html
2、修改配置文件并启动

image.png
解压后:
在conf文件夹下面将zoo.sample.cfg修改为zoo.cfg文件,
然后到bin目录下,启动zkServer.cnd就可以了
看一下zkEnv.cmd文件,这也是为什么刚才要修改为zoo.cfg文件,因为这里默认找的那个文件,所以你换成其他文件名也可以,只要和这里对应就行
启动成功,默认端口2181
安装dubbo-admin(注册中心的可视化管理平台)
1、下载dubbo-admin
https://gitee.com/shyf2019/dubbo-admin
如果熟悉git,也可以用git命令下载,这个也是从github上面复制过来的
2、导入eclipse
因为它是maven项目,所以导入一个maven项目到eclipse,等待下载好他的依赖
3、修改配置文件,主要是zookerper地址
4.启动项目后看效果
访问localhost:7001
服务注册
写一个member服务,注册到zookerper上面
1、聚合项目member的目录结构
父模块member两个子模块api和server
2、父模块member
pom.xml
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
2、api模块
在api模块中就一个接口
3、server模块
pom.xml
com.shyf.dubboA
api
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.alibaba.spring.boot
dubbo-spring-boot-starter
2.0.0
com.101tec
zkclient
0.10
application.yml
server:
port: 9091
spring:
dubbo:
scan:
basePackages: com.shyf.service
application:
id: member-service
name: member-service
protocol:
id: dubbo
name: dubbo
port: 12345
registry:
id: my-registry
address: zookeeper://127.0.0.1:2181
MemberServerImpl.java
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Service
@Component
public class MemberServiceIMpl implements MemberService{
@Override
public String getMember(String s) {
return "hello "+s;
}
}
启动类MemberApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
@SpringBootApplication
@ComponentScan("com.shyf")
@EnableDubboConfiguration
public class MemberApp {
public static void main(String[] args) {
SpringApplication.run(MemberApp.class, args);
}
}
4、启动server子模块
查看dubbo-admin
5、发布api模块
在dubbo中,服务的消费者需要依赖,服务生产者的服务接口
maven install api
服务调用
创建一个order项目,maven项目,作为服务的消费者
1、项目目录
2、pom.xml
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
com.shyf.dubboA
api
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.alibaba.spring.boot
dubbo-spring-boot-starter
2.0.0
com.101tec
zkclient
0.10
3、application.yml
server:
port: 9093
4、spring-dubbo.xml
5、OrderController.java
@RestController
public class OrderController {
@Autowired
private MemberService memberService;
@RequestMapping("/getOrder")
public Object getOrder(){
return memberService.getMember("ly");
}
}
6、启动类OrderApp.java
@SpringBootApplication
@EnableDubboConfig
@ImportResource(locations={"spring-dubbo.xml"})
public class OrderApp {
public static void main(String[] args) {
SpringApplication.run(OrderApp.class, args);
}
}
7、启动
查看dubbo-admin
8、访问
demo下载
https://gitee.com/shyf2019/dubbo-demo