新版本使用Dubbo和zookeeper时需要注意的地方,使用的是Apache Dubbo
1、配置zkclint引入 curator 的jar包,引入dubbo-dependencies-bom
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>2.7.7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
2、启动类加注解:@EnableDubbo(两个工程都要加)
@SpringBootApplication
@EnableDubbo
public class ProviderTicketApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderTicketApplication.class, args);
}
}
3、在生产者注解在service层时原有的@Server已经变为@DubboService
@Component
@DubboService
public class TickerServiceImpl implements TicketService{
@Override
public String getTicket() {
return "《厉害了,我的国》";
}
}
4、在消费者注解在service层时原有的@Reference已经变为@DubboReference
@Service//使用的是这个import org.springframework.stereotype.Service;
public class UserService {
//
@DubboReference
TicketService ticketService;
public void hello(){
String ticket = ticketService.getTicket();
System.out.println("买到票了"+ticket);
}
}
学习项目发布到github,https://github.com/SirZcom/springboot-dubbo.git
网友评论