Spring Cloud Alibaba——简介
Spring Cloud Alibaba——nacos搭建
SpringCloudAlibaba+dubbo+nacos——服务生产和消费
SpringCloudAlibaba+zuul+nacos
SpringCloudAlibaba+dubbo+nacos——服务生产和消费
本示例建在不同的java工程
基本结构:
公共接口
生产者
消费者
版本依赖
公共接口服务
创建maven工程,编写接口
package com.dubbo.service;
public interface TestService {
public String getDubbo();
}
打成jar包:api-1.0-SNAPSHOT.jar
放在resources的lib文件夹下,引入生产者和消费者
pom依赖
<dependency>
<groupId>dubbo-demo</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/api-1.0-SNAPSHOT.jar</systemPath>
</dependency>
生产者服务
pom依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>dubbo-demo</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/api-1.0-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--整合spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.properties
配置config的时候会首先加载这里的配置
对应的nacos添加的配置文件名
如下:
dubbo-demo-provider-test.properties
spring.application.name=dubbo-demo-provider
spring.profiles.active=test
spring.cloud.nacos.config.server-addr=localhost:8848
spring.cloud.nacos.config.file-extension=properties
application.properties
server.port=7080
spring.application.name=dubbo-demo-provider
spring.cloud.nacos.discovery.server-addr=localhost:8848
dubbo.registry.address=spring-cloud://localhost
dubbo.scan.base-packages=com.dubbo.service
dubbo.protocols.dubbo.name=dubbo
dubbo.protocols.dubbo.port=-1
dubbo.protocols.rest.name=rest
dubbo.protocols.rest.port=8888
接口实现
注意@Service来自dubbo的包
import org.apache.dubbo.config.annotation.Service;
package com.dubbo.service.impl;
import com.dubbo.service.TestService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class TestServiceImpl implements TestService {
@Override
public String getDubbo(){
return "hello dubbo";
}
}
启动类加注解
@EnableDiscoveryClient
消费者服务
pom依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo.consumer</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>dubbo-demo</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/api-1.0-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--整合spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=7081
spring.application.name=dubbo-demo-consumer
spring.cloud.nacos.discovery.server-addr=localhost:8848
dubbo.registry.address=spring-cloud://localhost
dubbo.registry.subscribe=true
dubbo.cloud.subscribed-services=dubbo-demo-provider
控制层
注意接口引入用注解@Reference
@RestController
public class TestDubboConsume {
@Reference
private TestService testService;
@RequestMapping(value = "getDubbo", method = RequestMethod.GET)
private String getTest(){
return testService.getDubbo();
}
}
启动类
@EnableDiscoveryClient
启动服务
先启动生产者,再启动消费者