此架构同时提供RESTful和Dubbo接口服务,应用层对前端提供RESTful接口,RESTful是互联网通用的轻量级交互
协议,方便前端接入系统;微服务层向应用层提供Dubbo接口,Dubbo接口基于RPC通信协议速度更快。
项目结构
service对外暴露dubbo协议的接口,考虑远程接口可能 会被其它多个服务调用,这里将service的接口单独抽取
出api工程
service-api:存放接口,独立成一个工程方便被其它服务工程依赖。
service-server:存放接口实现,即dubbo服务的实现部分。(调用service-api)
nacos-restful-consumer:实现远程调用(调用service-service)
启动nacos
访问正常 http://localhost:8848/nacos
父工程
pom.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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima.nacos</groupId>
<artifactId>nacos-discovery</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
接口服务(service-api)
pom.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>nacos-dubbo-service</artifactId>
<groupId>li.chen.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>serivce-api</artifactId>
</project>
ServiceApi文件
package li.chen.com.microservice.service.api;
public interface ServiceApi {
public String dubboService();
}
服务注册
调用service-api
pom.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>nacos-dubbo-service</artifactId>
<groupId>li.chen.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>serivce-service</artifactId>
<dependencies>
<dependency>
<groupId>li.chen.com</groupId>
<artifactId>serivce-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
bootstrap.yml
server:
port: 56040 #启动端口 命令行注入
spring:
application:
name: dubbo-service
main:
allow-bean-definition-overriding: true # Spring Boot 2.1 需要设定
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
dubbo:
scan:
# dubbo服务扫描基准包
base-packages: li.chen.com.microservice.serivce.service
protocol:
# dubbo 协议
name: dubbo
# dubbo 协议端口
port: 20891
registry:
address: nacos://127.0.0.1:8848
application:
qos-enable: false #dubbo运维服务是否开启
consumer:
check: false #启动时就否检查依赖的服务
ServiceApiImpl 文件
package li.chen.com.microservice.serivce.service;
import li.chen.com.microservice.service.api.ServiceApi;
import org.apache.dubbo.config.annotation.Service;
/**
* 实现service-api模块的ServiceApi接口
*/
@Service //dubbo的service注解 org.apache.dubbo.config.annotation.Service
public class ServiceApiImpl implements ServiceApi {
@Override
public String dubboService() {
return "dubboService";
}
}
启动类
package li.chen.com.microservice.serivce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceBootstrap {
public static void main(String[] args) {
SpringApplication.run(ServiceBootstrap.class,args);
}
}
访问nacos页面
注:启动时如下错误:在pom.xml文件增加spring-boot-starter-web坐标。
服务发现(远程调用)
调用service-service
pom.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>nacos-dubbo-service</artifactId>
<groupId>li.chen.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>serivce-service</artifactId>
<dependencies>
<dependency>
<groupId>li.chen.com</groupId>
<artifactId>serivce-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 56020
# 服务生产方地址
#provider:
# address: 192.168.58.1:56010
spring:
application:
name: nacos-restful-consumer #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #服务发现中心地址
nacos-restful-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
ServiceApiImpl 文件
package li.chen.com.nacos.controller;
import li.chen.com.microservice.service.api.ServiceApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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.net.URI;
@RestController
@RequestMapping("/RestConsumerController")
public class RestConsumerController {
@Reference //import org.apache.dubbo.config.annotation.Reference;
ServiceApi serviceApi;
@GetMapping("/service-api/service")
public String serviceApi(){
//远程调用
String providerResult = serviceApi.dubboService();
return "consumer dubbo invode|"+providerResult;
}
}
启动类
package li.chen.com.microservice.serivce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceBootstrap {
public static void main(String[] args) {
SpringApplication.run(ServiceBootstrap.class,args);
}
}
远程调用步骤
- 引入调用方坐标
<dependency>
<groupId>com.itheima.nacos</groupId>
<artifactId>service‐api</artifactId>
<version>1.0‐SNAPSHOT</version>
</dependency>
- 注入调用方,直接调用方法
@Reference //import org.apache.dubbo.config.annotation.Reference;
ServiceApi serviceApi;