dubbo教程-----上手案例(2)
一. 所需环境zookeeper作为dubbo的注册中心,dubbo服务提供方和消费方都需要在zookeeper注册中心注册。二. dubbo admin去github官网https://github.com/apache/dubbo,下载代码,修改配置文件,配置zookeeper地址启动即可三. 生产者provider引入依赖<properties>...
·
一. 所需环境
zookeeper作为dubbo的注册中心,dubbo服务提供方和消费方都需要在zookeeper注册中心注册。
二. dubbo admin
去github官网https://github.com/apache/dubbo,
下载代码,修改配置文件,配置zookeeper地址启动即可
三. 生产者provider
引入依赖
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.1</dubbo.version>
<curator.version>2.8.0</curator.version>
<zookeeper.version>3.4.6</zookeeper.version>
</properties>
<dependencies>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
定义接口
public interface DemoService {
String sayHello(String name);
}
实现方法
import org.apache.dubbo.config.annotation.Service;
@Service
@Component
public class DefaultDemoService implements DemoService {
/**
* The default value of ${dubbo.application.name} is ${spring.application.name}
*/
@Value("${dubbo.application.name}")
private String serviceName;
@HystrixCommand
@Override
public String sayHello(String name) {
return String.format("[%s] : Hello, %s", serviceName, name);
}
}
application.yml
spring:
application:
name: sentinel-provider
server:
port: 8081
dubbo:
application:
id: demo-provider
name: demo-provider
version: 1.0.0
qos-enable: false
#开启包扫描,可替代 @EnableDubbo 注解
scan:
basePackages: com.dubbo.demo
protocol:
name: dubbo
port: 21880
registry:
id: registry
address: 120.24.72.198:2181
protocol: zookeeper
四.消费者conmuser
定义接口
public interface DemoService {
String sayHello(String name);
}
调用方法
@RestController
@RequestMapping("/test")
public class DemoController {
@Reference
private DemoService demoService;
@HystrixCommand(fallbackMethod = "test1")
@GetMapping("/test")
public void test() {
System.out.println(demoService.sayHello("wou"));
}
public void test1() {
System.out.println("这是容错回调!");
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献4条内容
所有评论(0)