Dubbo-搭建微服务(注解版本)
Posted 香蜜湖的蜜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo-搭建微服务(注解版本)相关的知识,希望对你有一定的参考价值。
一.目录
前言
内容
结语
二. 前言
Dubbo搭建微服务框架非常简单好用,尤其是对传统单体项目,只需要搭建三个工程即可轻松入手:接口API,接口实现service,服务调用Web,当然还要一个安装一个注册中心中间件,项目中直接使用即可。
三.内容
环境
IDE为STS
Dubbo 2.6.2
ZK
spring-boot-starter 2.0.3
JDK1.8
工程结构
shing-consumer
shing-dubbo-api
shing-dubbo-provider
代码
shing-dubbo-sample
父工程
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.shing</groupId>
<artifactId>shing-dubbo-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>shing-dubbo-consumer</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.shing</groupId>
<artifactId>shing-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
shing-dubbo-api
接口工程
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.shing</groupId>
<artifactId>shing-dubbo-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>shing-dubbo-consumer</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.shing</groupId>
<artifactId>shing-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
java
package shing.dubbo.api;
/**
* @ClassName: IHelloService
* @Description: IHelloService
* @author: Shing 公众号 :香蜜湖的蜜。慢慢来,成长最快。相信奋斗,看见未来!天堂向左,深圳向右,记录与分享深圳故事。
* @date: 2020-7-29 17:46:28
*/
public interface IHelloService {
String sayHello(String name);
}
新创建工程要有main方法,不然编译不通过
package shing.dubbo.api;
public class Test {
public static void main(String[] args) {
}
}
shing-dubbo-provider
提供服务的工程
java
基本配置
package shing.dubbo.provider.config;
import javax.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import shing.dubbo.provider.properties.DubboProperties;
/**
* @ClassName: DubboConfig
* @Description: DubboConfig
* @author: Shing 公众号 :香蜜湖的蜜。慢慢来,成长最快。相信奋斗,看见未来!天堂向左,深圳向右,记录与分享深圳故事。
* @date: 2020-7-29 18:12:00
*/
@Configuration
public class DubboConfig {
@Resource
private DubboProperties dubboProperties;
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(dubboProperties.getName());
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(dubboProperties.getAddress());
registryConfig.setClient(dubboProperties.getClient());
return registryConfig;
}
}
package shing.dubbo.provider.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "application.shing.dubbo.provider")
@PropertySource("dubbo-provider.properties")
//@Data
public class DubboProperties {
//
private String name;
private String address;
private String client;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getClient() {
return client;
}
public void setClient(String client) {
this.client = client;
}
}
application.properties
server.port=8080
server.servlet.context-path=/shing-dubbo-provider
dubbo-provider.properties
application.shing.dubbo.provider.name=shing-dubbo-provider
application.shing.dubbo.provider.address=zookeeper://192.168.47.104:2181
application.shing.dubbo.provider.client=zkclient
接口
@Service(timeout = 5000, version = "1.0", group = "shing-dubbo-provider")
Service引用的包com.alibaba.dubbo.config.annotation.Service;timeout = 5000, version = "1.0", group = "shing-dubbo-provider"非必要选项,timeout可以在项目级别上加
package shing.dubbo.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import shing.dubbo.api.IHelloService;
/**
* @ClassName: HelloService
* @Description: HelloService
* @author: Shing 公众号 :香蜜湖的蜜。慢慢来,成长最快。相信奋斗,看见未来!天堂向左,深圳向右,记录与分享深圳故事。
* @date: 2020-7-29 17:55:46
*/
@Service(timeout = 5000, version = "1.0", group = "shing-dubbo-provider")
public class HelloService implements IHelloService {
@Override
public String sayHello(String name) {
return "Shing ," + name;
}
}
启动
package shing.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
@SpringBootApplication
@DubboComponentScan(value = "shing.dubbo.provider.service")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
shing-consumer
调用shing-dubbo-provider的工程,可以服务调用服务,也可以web调用 。配置与shing-dubbo-provider工程一样,在这里就不贴出来了。
web调用
package shing.dubbo.consumer.control;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import shing.dubbo.api.IHelloService;
/**
* @ClassName: HelloControl
* @Description: HelloControl
* @author: Shing 公众号 :香蜜湖的蜜。慢慢来,成长最快。相信奋斗,看见未来!天堂向左,深圳向右,记录与分享深圳故事。
* @date: 2020-7-29 18:51:21
*/
@RestController
public class HelloControl {
@Reference(version = "1.0", group = "shing-dubbo-provider")
private IHelloService helloService;
/**
* @Title: hello
* @Description: hello
* @author: Shing
* @date: 2020-7-29 19:13:36
* @return: String
*/
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
String str = null;
try {
str = helloService.sayHello("Hello," + name);
} catch (Exception e) {
System.err.println(e);
}
return str;
}
}
服务调用
package shing.dubbo.consumer.service;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import shing.dubbo.api.IHelloService;
@Service
public class HelloService {
@Reference(version = "1.0", group = "shing-dubbo-provider")
private IHelloService helloService;
public void hello(String name) {
String str = helloService.sayHello(name);
System.out.println("str:" + str);
}
}
启动
确保注册中心服务启动,这是基础公共中间件
启动shing-dubbo-provider工程
启动shing-consumer工程
最后访问http://localhost:8081/shing-dubbo-consumer/hello
四.结语
Dubbo还是很强大的,现在有spring cloud alibaba微服务生态。Dubbo先写到这,后期我会继续写spring cloud alibaba生态相关技术,请后续关注!
以上是关于Dubbo-搭建微服务(注解版本)的主要内容,如果未能解决你的问题,请参考以下文章