Spring boot 入门(一):快速搭建Spring boot项目
本文接着上一篇写的《java微服务(一):dubbo-admin控制台的使用》,上篇文章介绍了docker,zookeeper环境的安装,并参考dubbo官网演示了dubbo-admin控制台的使用。上篇文章已经搭建好zookeeper服务注册中心,本片文章主要搭建服务消费者和服务提供者。按照微服务的原则,本文将demo分为3部分:服务接口、服务消费者、服务消费者。
服务接口:定义了系统所需要的全部接口。
服务提供者:主要是对接口的实现。
服务消费者:对接口的使用
1.dubbo介绍
节点 | 角色说明 |
---|---|
provider |
暴露服务的服务提供方 |
consumer |
调用远程服务的服务消费方 |
registry |
服务注册与发现的注册中心 |
monitor |
统计服务的调用次数和调用时间的监控中心 |
container |
服务运行容器 |
dubbo 架构具有以下几个特点,分别是连通性、健壮性、伸缩性、以及向未来架构的升级性。
调用关系说明
- 服务容器负责启动,加载,运行服务提供者。
- 服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
dubbo 架构具有以下几个特点,分别是连通性、健壮性、伸缩性、以及向未来架构的升级性。
更详细的介绍,请参考官网:
2.服务接口
通过idea创建一个jar工程,创建工程的过程可以参考《spring boot 入门(一):快速搭建spring boot项目》,此工程的目的只是简单的定义接口,所以这里直接创建jar包,不是maven工程。创建好了后,新建一个接口。以下是我创建的接口:
其中userservice代码如下:
1 package com.edu.hello.dubbo.service.user.api; 2 3 public interface userservice { 4 string sayhi(); 5 }
创建完接口后,需要把接口install到本地仓库,供服务消费者和服务提供者使用
在terminal直接指向mvn clean install或者直接在lifecycle目录下点击install进行安装,出现如下页面,表示安装成功
3.服务提供者
服务提供者主要是对接口的实现,用相同的方法创建一个maven工程,创建好后的maven工程目录如下:
其中userservicelmpl是多接口的实现,代码如下:
1 package com.edu.hello.dubbo.service.user.provider.api.impl; 2 3 import com.alibaba.dubbo.config.annotation.service; 4 import com.edu.hello.dubbo.service.user.api.userservice; 5 import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand; 6 import com.netflix.hystrix.contrib.javanica.annotation.hystrixproperty; 7 import org.springframework.beans.factory.annotation.value; 8 9 /** 10 * @classname userserviceimpl 11 * @deccription todo 12 * @author dz 13 * @date 2019/8/31 11:20 14 **/ 15 @service(version = "${user.service.version}") 16 public class userserviceimpl implements userservice { 17 18 @value("${dubbo.protocol.port}") 19 private string port; 20 21 /*@hystrixcommand(commandproperties = { 22 @hystrixproperty(name = "circuitbreaker.requestvolumethreshold", value = "10"), 23 @hystrixproperty(name = "execution.isolation.thread.timeoutinmilliseconds", value = "2000") 24 })*/ 25 @override 26 public string sayhi() { 27 return "say hello, i am from " + port; 28 } 29 }
其中@hystrixcommand注解在后面熔断器中会讲到,这里先注释。
yml配置如下:
1 spring: 2 application: 3 name: hello-dubbo-service-user-provider 4 5 user: 6 service: 7 version: 1.0.0 8 9 dubbo: 10 scan: 11 basepackages: com.edu.hello.dubbo.service.user.provider.api 12 application: 13 id: hello-dubbo-service-user-provider 14 name: hello-dubbo-service-user-provider 15 qos-port: 22222 16 qos-enable: true 17 protocol: 18 id: dubbo 19 name: dubbo 20 port: 12346 21 status: server 22 serialization: kryo #高速序列化 23 # optimizer: 24 25 registry: 26 id: zookeeper 27 address: zookeeper://192.168.1.12:2181?back=192.168.1.12:2182,192.168.1.12:2183 28 provider: 29 loadbalance: roundrobin #负载均衡 30 31 32 33 management: 34 endpoint: 35 dubbo: 36 enable: true 37 dubbo-shutdown: 38 enabled: true 39 dubbo-configs: 40 enabled: true 41 dubbo-services: 42 enabled: true 43 dubbo-references: 44 enabled: true 45 dubbo-peoperties: 46 enabled: true 47 health: 48 dubbo: 49 status: 50 defaults: memory 51 extras: load,threadpool
pom文件如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 3 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelversion>4.0.0</modelversion> 5 <parent> 6 <groupid>org.springframework.boot</groupid> 7 <artifactid>spring-boot-starter-parent</artifactid> 8 <version>2.1.7.release</version> 9 <relativepath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupid>com.edu</groupid> 12 <artifactid>hello-dubbo-service-user-provider</artifactid> 13 <version>1.0.0-snapshot</version> 14 <name>hello-dubbo-service-user-provider</name> 15 <description>demo project for spring boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupid>org.springframework.boot</groupid> 24 <artifactid>spring-boot-starter</artifactid> 25 </dependency> 26 <dependency> 27 <groupid>org.springframework.boot</groupid> 28 <artifactid>spring-boot-starter-actuator</artifactid> 29 </dependency> 30 <dependency> 31 <groupid>org.springframework.boot</groupid> 32 <artifactid>spring-boot-starter-test</artifactid> 33 <scope>test</scope> 34 </dependency> 35 <dependency> 36 <groupid>com.alibaba.boot</groupid> 37 <artifactid>dubbo-spring-boot-starter</artifactid> 38 <version>0.2.0</version> 39 </dependency> 40 <dependency> 41 <groupid>com.edu</groupid> 42 <artifactid>hello-dubbo-service-user-api</artifactid> 43 <version>${project.version}</version> 44 </dependency> 45 <dependency> 46 <groupid>de.javakaffee</groupid> 47 <artifactid>kryo-serializers</artifactid> 48 <version>0.42</version> 49 </dependency> 50 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix --> 51 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --> 52 <dependency> 53 <groupid>org.springframework.cloud</groupid> 54 <artifactid>spring-cloud-starter-netflix-hystrix</artifactid> 55 <version>2.0.1.release</version> 56 </dependency> 57 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard --> 58 <dependency> 59 <groupid>org.springframework.cloud</groupid> 60 <artifactid>spring-cloud-starter-netflix-hystrix-dashboard</artifactid> 61 <version>2.0.1.release</version> 62 </dependency> 63 64 65 66 </dependencies> 67 68 <build> 69 <plugins> 70 <plugin> 71 <groupid>org.springframework.boot</groupid> 72 <artifactid>spring-boot-maven-plugin</artifactid> 73 </plugin> 74 </plugins> 75 </build> 76 77 </project>
本文pom文件和yml文件主要是根据dubbo官网中服务提供者的pom文件中的依赖而来,具体参考:。
注意basepackages注解
4.服务提供者
按照相同的方式创建服务提供者,配置文件和服务提供者也类似,直接贴代码了
1 package com.edu.hello.dubbo.service.user.consumer.controller; 2 3 4 import com.alibaba.dubbo.config.annotation.reference; 5 import com.edu.hello.dubbo.service.user.api.userservice; 6 import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand; 7 import org.springframework.web.bind.annotation.requestmapping; 8 import org.springframework.web.bind.annotation.requestmethod; 9 import org.springframework.web.bind.annotation.restcontroller; 10 11 /** 12 * @classname usercontroller 13 * @deccription todo 14 * @author dz 15 * @date 2019/8/31 18:37 16 **/ 17 @restcontroller 18 public class usercontroller { 19 20 @reference(version = "${user.service.version}") 21 private userservice userservice; 22 23 @hystrixcommand(fallbackmethod = "sayhierror") 24 @requestmapping(value = "hi", method = requestmethod.get) 25 public string sayhi() { 26 return userservice.sayhi(); 27 } 28 29 public string sayhierror() { 30 return "hystrix fallback"; 31 } 32 }
yml配置文件和pom配置文件和提供者基本类似;
1 spring: 2 application: 3 name: hello-dubbo-service-user-consumer 4 5 user: 6 service: 7 version: 1.0.0 8 9 dubbo: 10 scan: 11 basepackages: com.edu.hello.dubbo.service.user.consumer.controller 12 application: 13 id: hello-dubbo-service-user-consumer 14 name: hello-dubbo-service-user-consumer 15 qos-port: 22223 16 qos-enable: true 17 protocol: 18 id: dubbo 19 name: dubbo 20 port: 12345 21 #status: server 22 serialization: kryo 23 registry: 24 id: zookeeper 25 address: zookeeper://192.168.1.12:2181?back=192.168.1.12:2182,192.168.1.12:2183 26 27 28 management: 29 endpoint: 30 dubbo: 31 enable: true 32 dubbo-shutdown: 33 enabled: true 34 dubbo-configs: 35 enabled: true 36 dubbo-services: 37 enabled: true 38 dubbo-references: 39 enabled: true 40 dubbo-peoperties: 41 enabled: true 42 health: 43 dubbo: 44 status: 45 defaults: memory 46 extras: load,threadpool 47 server: 48 port: 9090
1 <?xml version="1.0" encoding="utf-8"?> 2 <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 3 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelversion>4.0.0</modelversion> 5 <parent> 6 <groupid>org.springframework.boot</groupid> 7 <artifactid>spring-boot-starter-parent</artifactid> 8 <version>2.1.7.release</version> 9 <relativepath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupid>com.edu</groupid> 12 <artifactid>hello-dubbo-service-user-consumer</artifactid> 13 <version>1.0.0-snapshot</version> 14 <name>hello-dubbo-service-user-consumer</name> 15 <description>demo project for spring boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupid>org.springframework.boot</groupid> 24 <artifactid>spring-boot-starter-web</artifactid> 25 </dependency> 26 <dependency> 27 <groupid>org.springframework.boot</groupid> 28 <artifactid>spring-boot-starter-actuator</artifactid> 29 </dependency> 30 <dependency> 31 <groupid>org.springframework.boot</groupid> 32 <artifactid>spring-boot-starter-test</artifactid> 33 <scope>test</scope> 34 </dependency> 35 <dependency> 36 <groupid>com.alibaba.boot</groupid> 37 <artifactid>dubbo-spring-boot-starter</artifactid> 38 <version>0.2.0</version> 39 </dependency> 40 <dependency> 41 <groupid>com.edu</groupid> 42 <artifactid>hello-dubbo-service-user-api</artifactid> 43 <version>${project.version}</version> 44 </dependency> 45 <dependency> 46 <groupid>de.javakaffee</groupid> 47 <artifactid>kryo-serializers</artifactid> 48 <version>0.42</version> 49 </dependency> 50 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix --> 51 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --> 52 <dependency> 53 <groupid>org.springframework.cloud</groupid> 54 <artifactid>spring-cloud-starter-netflix-hystrix</artifactid> 55 <version>2.0.1.release</version> 56 </dependency> 57 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard --> 58 <dependency> 59 <groupid>org.springframework.cloud</groupid> 60 <artifactid>spring-cloud-starter-netflix-hystrix-dashboard</artifactid> 61 <version>2.0.1.release</version> 62 </dependency> 63 </dependencies> 64 65 <build> 66 <plugins> 67 <plugin> 68 <groupid>org.springframework.boot</groupid> 69 <artifactid>spring-boot-maven-plugin</artifactid> 70 <configuration> 71 <mainclass>com.edu.hello.dubbo.service.user.consumer.hellodubboserviceuserconsumerapplication</mainclass> 72 </configuration> 73 </plugin> 74 </plugins> 75 </build> 76 77 </project>
这里面关于服务熔断和负载均衡的的代码可以暂时不关注,后面会专门对熔断进行讨论。
5.结果
分别启动服务消费者和服务提供者,启动成功后,如下:
访问
同时我们可以启动dubbo-admin控制台查看服务,注意端口的冲突:
推荐阅读
-
Spring boot 入门(一):快速搭建Spring boot项目
-
只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常
-
spring boot + jpa + kotlin入门实例详解
-
Spring Boot中使用LDAP来统一管理用户信息的示例
-
Spring Boot快速入门教程
-
Spring Boot JPA如何把ORM统一起来
-
spring boot thymeleaf 图片上传web项目根目录操作步骤
-
Spring Boot项目实战之拦截器与过滤器
-
Spring Boot入门HelloWorld程序
-
开发一个Spring Boot Starter!