SpringBoot整合Dubbo

 

Dubbo是Alibaba開發的一款分佈式服務治理框架。spring

  優勢:
            一、表現層和服務層進行了分離,加入一個註冊中心進行了解耦。表現層不會直接去調用服務層,而是經過服務發現的方式進行調用。
            二、由於兩層的解耦,服務層重啓後,表現層無需改動。
 
相關的概念:
        集羣:將一個服務部署到N臺機器上,這就是當前服務的集羣
        分佈式:將一個工程,按照相關的業務拆分紅多個工程,部署在不一樣的服務器上
        分佈式集羣:首先根據業務進行分佈式開發,而後根據請求量,對不一樣的業務進行集羣處理
 
        負載均衡:均勻的將用戶請求負載給後臺服務集羣
 

 

Dubbo的原理springboot

 SpringBoot整合Dubbo服務器

整合服務提供者app

1.引入Dubbo依賴負載均衡

<!-- dubbo和springboot的整合依賴 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>

2.配置application.yml框架

dubbo:
application:
name: class_service
registry:
address: zookeeper://192.168.59.131:2181
protocol:
port: 20881

3.給提供服務的實現類上添加@Service註解(Dubbo包下的@Service註解)分佈式

@Service
public class StuServiceImpl implements IStuService {

@Autowired
private StuMapper stuMapper;
}

4.啓動類上配置包的掃描spring-boot

@SpringBootApplication(scanBasePackages = "com.qq")
@MapperScan("com.qq.dao")
@DubboComponentScan("com.qq.serviceimpl")
public class StuServiceApplication {

public static void main(String[] args) {
SpringApplication.run(StuServiceApplication.class, args);
}
}

整合服務的消費者
1.添加依賴
2.配置application.yml
3.註解(Dubbo包下的@Reference註解)
@Controller
@RequestMapping("/stu")
public class StuController {

@Reference
private IStuService stuService;

@Reference private IClassService classService; @RequestMapping("/list") public String list(ModelMap map){ //遠程調用服務的提供者 List<Student> studentList = stuService.queryAll(); map.put("studentList",studentList); return "stulist"; }
相關文章
相關標籤/搜索