您好,登錄后才能下訂單哦!
原創(chuàng)文章,歡迎轉載。轉載請注明:轉載自IT人故事會,謝謝!
原文鏈接地址:『高級篇』docker之課程管理dubbo入門操練(14)接下來我們即將開始說課程管理,課程服務他是基于dubbo實現(xiàn)的,所以先來預熱下,dubbo,對不熟悉的dubbo的老鐵進行一下講解。 源碼:https://github.com/limingios/dubbo.git
高性能的基于java的,RPC框架。dubbo是阿里巴巴開源的一個項目,就像大多的RPC框架,dubbo的思想圍繞一個服務,指定一個方法的參數和返回值,這個方法可以被遠程調用的,在服務端,會實現(xiàn)這個接口,會運行dubbo的服務用來處理客戶端的調用,在客戶端,會有一個存根它提供和服務端想通的方法。其實這些概念用java的術語:首先要定義一個接口,這個接口在服務端和客戶端公用,服務端會完成這個接口的實現(xiàn),客戶端通過接口的描述來調用服務端。
用了本地調用。
provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方應用信息,用于計算依賴關系 -->
<dubbo:application name="demo-provider" />
<!-- 使用multicast廣播注冊中心暴露服務地址 -->
<dubbo:registry register="false" />
<!-- 用dubbo協(xié)議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一樣實現(xiàn)服務 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.idig8</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
</dependencies>
</project>
package com.idig8.springboot.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
對外提供的maven坐標如下:
<dependency>
<groupId>com.idig8</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<!-- springboot dubbo starter -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<!-- api dependence -->
<dependency>
<groupId>com.idig8</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
package com.idig8.springboot.dubbo.demo.provider;
import com.alibaba.dubbo.config.annotation.Service;
import com.idig8.springboot.dubbo.demo.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
}
spring.dubbo.application.name=demo-provider
#這里使用廣播的注冊方式,
#如果有Can't assign address異常需要加vm參數:
#-Djava.net.preferIPv4Stack=true
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.idig8.springboot.dubbo.demo.provider
package com.idig8.springboot.dubbo.demo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainProvider {
public static void main(String[] args) {
SpringApplication.run(MainProvider.class,args);
}
}
<!-- springboot parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<!-- springboot web starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot dubbo starter -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<!-- api dependency -->
<dependency>
<groupId>com.idig8</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
package com.idig8.springboot.dubbo.demo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import com.idig8.springboot.dubbo.demo.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoConsumerController {
@Reference
private DemoService demoService;
@RequestMapping("/sayHello")
public String sayHello(@RequestParam String name) {
return demoService.sayHello(name);
}
}
server.port=8080
#dubbo config
spring.dubbo.application.name=demo-consumer
#這里使用廣播的注冊方式,
#如果有Can't assign address異常需要加vm參數:
#-Djava.net.preferIPv4Stack=true
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.scan=com.idig8.springboot.dubbo.demo.consumer
package com.idig8.springboot.dubbo.demo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
PS:dubbo的入門也就到這里,從spring 和springboot 對dubbo的整合。
流程基本之前也說,api 建立接口,provider 實現(xiàn)接口, consumer 調用接口。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。