前:
为什么我要用Springboot和Dubbo来搭建这个demo,因为springboot自身和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。同时它集成了大量常用的第三方库配置,Spring Boot应用中这些第三方库几乎可以是零配置的开箱即用(out-of-the-box),大部分的 Spring Boot 应用都只需要非常少量的配置代码(基于 Java 的配置),不需要配置提供者和消费者的xml文件,开发者能够更加专注于业务逻辑。并且项目不需要发送到tomcat中使用SpringApplication.run就可以在Idea中充当容器启动服务。
1、Springboot空项目创建
2、springboot-dubbo-provider模块创建
如创建空项目时在项目下创建模块,创建后模块中的pom.xml中追加dubbo和zookeeper的依赖
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
在项目的resources中application.properties中定义参数
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.example.springboot.dubbo.provider
server.port=8082
创建demo接口version是重点,不能漏
com.example.springboot.dubbo.api.SayHello
package com.example.springboot.dubbo.api;
public interface SayHello {
String sayHello(String name);
}
实现接口
con.example.springboot.dubbo.provider.SayHelloImpl
package com.example.springboot.dubbo.provider;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.springboot.dubbo.api.SayHello;
@Service(version = "1.0.0")
public class SayHelloImpl implements SayHello {
@Override
public String sayHello(String name){
return "Hello " + name + "!";
}
}
启动服务提供者
package com.example.springboot.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
public class server {
public static void main(String[] args) {
SpringApplication.run(server.class, args);}
}
3、springboot-dubbo-consumer模块创建
与provider模块一样的创建方式,配置pom.xml,追加dubbo依赖和provider
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
配置resources中application.properties
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.example.springboot.dubbo.consumer
server.port=8083
创建consumer接口,version是重点。
com.example.springboot.dubbo.api.SayHello
package com.example.springboot.dubbo.api;
public interface SayHello {
String sayHello(String name);
}
实现接口
package com.example.springboot.dubbo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.springboot.dubbo.api.*;
import org.springframework.stereotype.Component;
@Component
public class SayService {
@Reference
(version = "1.0.0")
SayHello sayHello;
public String say (String name) {
return sayHello.sayHello(name);
}
}
启动服务消费者
package com.example.springboot.dubbo.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Client {
@Autowired
SayService sayService;
@RequestMapping("/hello")
public String say(@RequestParam ("name") String name) {
return sayService.say(name);
}
public static void main(String[] args) throws Exception{
ConfigurableApplicationContext context = SpringApplication.run(Client.class, args);
}
}
启动消费者,查看dubbo admin
调用消费者端口,传入参数,成功回显。