如今java後端開發大多用springboot來簡化環境搭建,如今一直使用的是springcloud和k8s有關的東西,之前用過dubbo,但那會兒的開發環境搭建流程較爲繁瑣,並且不支持rest調用。如今簡化了很多搭了一下以後發現確實比之前要方便不少了。dubbo因爲是rpc調用的,速度上來講是確定比springcloud要快一些的,不過如今也支持rest調用了,案例中兩種方式都會有。不過springcloud感受各方面的支持來講要更友好一些,因此各有千秋吧。html
教程鏈接 https://nacos.io/zh-cn/docs/quick-start.html java
根據鏈接中的地址教程搭建便可,而後訪問對應的地址http://localhost:8848/nacos/index.html#/login git
將地址端口換爲設置的便可,若是出現以下界面表明成功,帳號密碼默認爲nacos/nacosgithub
點擊finish便可完成建立web
點擊新建module便可完成建立 具體建立過程略。。可到文末的源碼地址下載查看具體項目,建立完的項目以下便可spring
只須要在dubbo-service中引入依賴便可,下面的module模塊會繼承依賴,即dubbo-service的pom.xml文件以下所示apache
記住不能使用springboot過高的版本,由於目前是不支持tomcat9的。高版本的springboot會嵌入tomcat9進去後端
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <packaging>pom</packaging> 7 <modules> 8 <module>common-api</module> 9 <module>provider</module> 10 <module>consumer</module> 11 </modules> 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.0.6.RELEASE</version> 16 <relativePath/> <!-- lookup parent from repository --> 17 </parent> 18 <groupId>com.dubbo</groupId> 19 <artifactId>dubbo-service</artifactId> 20 <version>1.0-SNAPSHOT</version> 21 22 <properties> 23 <java.version>1.8</java.version> 24 <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version> 25 <netty-all.version>4.0.35.Final</netty-all.version> 26 </properties> 27 28 <dependencies> 29 <!--springboot有關--> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-actuator</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-web</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 48 <!--dubbo有關--> 49 <dependency> 50 <groupId>com.alibaba</groupId> 51 <artifactId>dubbo-registry-nacos</artifactId> 52 <version>2.6.6</version> 53 </dependency> 54 <dependency> 55 <groupId>com.alibaba.nacos</groupId> 56 <artifactId>nacos-client</artifactId> 57 <version>0.6.2</version> 58 </dependency> 59 <dependency> 60 <groupId>com.alibaba</groupId> 61 <artifactId>dubbo-common</artifactId> 62 <version>2.6.6</version> 63 </dependency> 64 <dependency> 65 <groupId>com.alibaba</groupId> 66 <artifactId>dubbo-registry-api</artifactId> 67 <version>2.6.6</version> 68 </dependency> 69 <dependency> 70 <groupId>com.alibaba</groupId> 71 <artifactId>dubbo</artifactId> 72 <version>2.6.6</version> 73 </dependency> 74 <dependency> 75 <groupId>com.alibaba.boot</groupId> 76 <artifactId>dubbo-spring-boot-starter</artifactId> 77 <version>0.2.1.RELEASE</version> 78 </dependency> 79 <dependency> 80 <groupId>com.alibaba.boot</groupId> 81 <artifactId>dubbo-spring-boot-autoconfigure</artifactId> 82 <version>0.2.1.RELEASE</version> 83 </dependency> 84 <dependency> 85 <groupId>io.netty</groupId> 86 <artifactId>netty-all</artifactId> 87 <version>${netty-all.version}</version> 88 </dependency> 89 <!--公用api--> 90 <dependency> 91 <groupId>com.dubbo</groupId> 92 <artifactId>common-api</artifactId> 93 <version>1.0-SNAPSHOT</version> 94 </dependency> 95 <!-- rest有關 --> 96 <dependency> 97 <groupId>org.jboss.resteasy</groupId> 98 <artifactId>jaxrs-api</artifactId> 99 <version>3.0.12.Final</version> 100 </dependency> 101 <dependency> 102 <groupId>org.jboss.resteasy</groupId> 103 <artifactId>resteasy-client</artifactId> 104 <version>3.0.12.Final</version> 105 </dependency> 106 107 </dependencies> 108 109 <build> 110 <plugins> 111 <plugin> 112 <groupId>org.springframework.boot</groupId> 113 <artifactId>spring-boot-maven-plugin</artifactId> 114 </plugin> 115 </plugins> 116 </build> 117 118 </project>
這兒注意在dubbo-service中引入了本身建立的common-api 這樣的話provider和consumer項目均可以直接調用api
UserDTO爲測試傳輸類瀏覽器
public class UserDTO implements Serializable { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserDTO{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
UserService爲公用api接口
public interface UserService { UserDTO getUserById( String id); }
該模塊爲服務提供方,所以須要實現公用api中的類,服務結構以下
dubbo:
application:
name: user-provider
id: user-provider
registry:
address: nacos://127.0.0.1:8848
protocols:
dubbo:
name: dubbo
port: -1
server: tomcat
rest:
name: rest
port: 8888
server: tomcat
spring:
application:
name: provider-demo
main:
allow-bean-definition-overriding: true
server:
port: 8085
@SpringBootApplication @EnableDubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class,args); } }
package com.dubbo.provider.service.impl; import com.alibaba.dubbo.config.annotation.Service; import dubbo.api.UserService; import dubbo.entity.UserDTO; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; /** * @Description: * @author: zhoum * @Date: 2019-06-26 * @Time: 9:50 */ @Path("/user") @Service(protocol = {"rest","dubbo"}) public class UserServiceImpl implements UserService { @Path("{id}") @Produces(MediaType.APPLICATION_JSON) @GET @Override public UserDTO getUserById(@PathParam("id") String id) { UserDTO u= new UserDTO(); u.setUsername("用戶名:"+id); u.setPassword("12346"); return u; } }
點擊詳情查看便可看到分別提供了dubbo協議和rest協議的服務,到此服務發佈成功
dubbo:
application:
name: user-consumer
id: user-consumer
registry:
address: nacos://127.0.0.1:8848
protocols:
dubbo:
name: dubbo
port: -2
server: tomcat
rest:
name: rest
port: 8889
server: tomcat
spring:
application:
name: consumer-demo
main:
allow-bean-definition-overriding: true
server:
port: 8090
@SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
@Configuration public class WebConfig { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
package com.dubbo.consumer.controller; import com.alibaba.dubbo.config.annotation.Reference; import dubbo.api.UserService; import dubbo.entity.UserDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.HashMap; /** * @Description: * @author: zhoum * @Date: 2019-06-26 * @Time: 9:55 */ @RestController @RequestMapping("/consumer") public class ConsumerContoller { @Autowired private RestTemplate restTemplate; @Reference private UserService userService; @GetMapping("/user") public UserDTO getUser(){
//rest方式調用 UserDTO user = restTemplate.getForObject("http://localhost:8888/user/123" , UserDTO.class , new HashMap<>()); System.out.println(user);
//傳統方式調用 UserDTO wo = userService.getUserById("wo"); return wo; } }
到此服務提供者與消費者均正常發佈
上面步驟中 兩個模塊都啓動後調用consumer中的ConsumerController中的接口
在瀏覽器中訪問 http://localhost:8090/consumer/user
同時控制檯也打印出了
到此,服務正常註冊與發現,經過dubbo協議進行調用和rest調用都已經成功
最後項目的github地址爲: https://github.com/hetutu5238/zmc-dubbo