目录
1-Zookeeper注册中心安装
2-SpringBoot服务项目搭建
2.1-公共接口模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.dubbo.api</groupId>
<artifactId>01-springboot-dubbo-demo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo-demo-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.demo.dubbo.api;
public interface HelloDemoService {
String getHello();
}
2.2-生产服务模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo.dubbo.provider.service</groupId>
<artifactId>01-dubbo-provider-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo-demo-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--依赖api模块-->
<dependency>
<groupId>com.demo.dubbo.api</groupId>
<artifactId>01-springboot-dubbo-demo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入dubbo的依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
#应用服务端口
server.port=8081
#当前服务/应用的名字
dubbo.application.name=dubbo-demo-provider
#zk注册中心协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
#通信规则(通信协议和端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心(这里面我没有启动监控中心)
#dubbo.monitor.protocol=registry
//
package com.demo.dubbo.provider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo//dubbo必须配置注解@EnableDubbo
public class DubboProviderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderServiceApplication.class, args);
System.out.println("提供者启动成功!");
}
}
package com.demo.dubbo.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.demo.dubbo.api.HelloDemoService;
@Service//@Service注解要用dubbo的注解,如使用spring的注解那么dubbo将不能获取
public class HelloDemoServiceImpl implements HelloDemoService {
@Override
public String getHello() {
return "Hello this is Provider!";
}
}
2.3-消费服务模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo.dubbo.consumer</groupId>
<artifactId>springboot-dubbo-deom-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo-demo-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--依赖api模块-->
<dependency>
<groupId>com.demo.dubbo.api</groupId>
<artifactId>01-springboot-dubbo-demo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入dubbo的依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#应用服务端口
server.port=8080
#当前服务/应用的名字
dubbo.application.name=dubbo-demo-consumer
#zk注册中心协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
#通信规则(通信协议和端口)
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20880
#连接监控中心(这里面我没有启动监控中心)
#dubbo.monitor.protocol=registry
package com.demo.dubbo.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class SpringbootDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDubboConsumerApplication.class, args);
System.out.println("消费者启动成功!");
}
}
package com.demo.dubbo.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.demo.dubbo.api.HelloDemoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "dubbo/demo")
public class HelloController {
@Reference//@Reference必须是dubbo的注解
private HelloDemoService helloService;
@RequestMapping(value = "/getHello",method = RequestMethod.GET)
@ResponseBody
public String getHello(){
return helloService.getHello();
}
}
2.4-服务启动请求
3-dubbo-admin的安装运行
com.alibaba.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2181 for service com.alibaba.dubbo.monitor.MonitorService