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



2、修改配置文件并启动

解压后:

在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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
2、api模块

在api模块中就一个接口
3、server模块

pom.xml
<dependencies>
<dependency>
<groupId>com.shyf.dubboA</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.shyf.dubboA</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
3、application.yml
server:
port: 9093
4、spring-dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application id="member-service" name="member-service"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol id="dubbo" name="dubbo" port="12345"/>
<dubbo:reference id="memberService" interface="com.shyf.service.MemberService" />
</beans>
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、访问

网友评论