如今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