目录
springboot-guide/springboot-dubbo.md at master · CodingDocs/springboot-guide · GitHub
注:版本为3.0以上的dubbo已经无法被1.8jdk所支持了
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
<!--ZooKeeper客户端-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.1</version>
</dependency>
需要连接zookeeper,需要提供dubbo协议(name,port)
server.port=8333
spring.application.name=dubbo-provider
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.jsq.service
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
## Dubbo Registry
dubbo.registry.address=zookeeper://localhost:2181
服务接口
package com.jsq.service;
public interface HelloService {
public String sayHello(String s);
}
服务实现类
package com.jsq.service;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0")//标记为dubbo的服务
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String s) {
return "Hello "+s;
}
}
package com.jsq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
不需要对dubbo配置
# 配置端口
server.port=8330
服务接口(与生产者的相同)
package com.jsq.service;
public interface HelloService {
public String sayHello(String s);
}
package com.jsq.controller;
import com.jsq.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/dubbo")
public class HelloController {
@DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")//标记连接的dubbo中注册的服务
private HelloService helloService;
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
String world = helloService.sayHello(name);
return world;
}
}
package com.jsq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}