项目搭建基本步骤
- (一)搭建Nacos服务器
- (二)项目总pom文件
-
- (三)服务提供者(alibaba-provider)
-
-
- 1. pom.xml 配置
- 2. application.yml 配置
- 3. Controller 编写
- 4. 主启动类
- (四)服务消费者(alibaba-consumer)
-
-
- 1. pom.xml 配置
- 2. application.yml 配置
- 3. FeignClient 定义
- 3. Controller 编写
- 4. 主启动类
- (五)网关(gateway)
-
-
- 1. pom.xml 配置
- 2. application.yml 配置
- 3. 主启动类
- (五)验证
(一)搭建Nacos服务器
- 从官网下载对应版本下载地址,解压之后执行 startup.cmd -m standalone (windows系统)
- 通过docker安装
# 拉取镜像
docker pull nacos/nacos-server:v2.1.0
# 启动容器
docker run --name nacos -e MODE=standalone -p 8848:8848 -d nacos/nacos-server:v2.1.0
- 访问 http://127.0.0.1:8848/nacos 并使用默认账号与密码登录(均为nacos)控制面板
(二)项目总pom文件
1. 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.microservice</groupId>
<artifactId>alibaba-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>alibaba-consumer</module>
<module>alibaba-provider</module>
<module>gateway</module>
</modules>
<properties>
<spring-cloud.version>2021.0.1</spring-cloud.version>
<spring-boot.version>2.6.6</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
(三)服务提供者(alibaba-provider)
1. 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>
<groupId>com.microservice</groupId>
<artifactId>alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. application.yml 配置
server:
port: 6010
spring:
application:
name: alibaba-provider
cloud:
nacos:
discovery:
server-addr: http://${ip.address:127.0.0.1}:8848
config:
server-addr: http://${ip.address:127.0.0.1}:8848
3. Controller 编写
@RestController
public class ProviderServiceImpl implements ProviderService {
@Override
@GetMapping("sayHello")
public String sayHello(@RequestParam("name") String name) {
return "hello " + name + ", This is Spring Cloud Alibaba + Nacos !";
}
}
# 接口定义
public interface ProviderService {
String sayHello(String name);
}
4. 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaProviderApplication.class, args);
}
}
(四)服务消费者(alibaba-consumer)
1. 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>
<groupId>com.microservice</groupId>
<artifactId>alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. application.yml 配置
server:
port: 6020
spring:
application:
name: alibaba-consumer
cloud:
nacos:
discovery:
server-addr: http://${ip.address:127.0.0.1}:8848
config:
server-addr: http://${ip.address:127.0.0.1}:8848
feign:
client:
config:
default:
connectTimeout: 10000
readTimeout: 10000
3. FeignClient 定义
@FeignClient(value = "alibaba-provider",contextId = "providerService")
public interface ProviderService {
@GetMapping(value = "/sayHello")
String sayHello(@RequestParam("name") String name);
}
3. Controller 编写
@RestController
public class ConsumerController {
@Autowired
private ProviderService providerService;
@GetMapping("sayHello")
public String sayHello(@RequestParam("name") String name) {
return providerService.sayHello(name);
}
}
4. 主启动类
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class AlibabaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaConsumerApplication.class, args);
}
}
(五)网关(gateway)
1. 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>
<groupId>com.microservice</groupId>
<artifactId>alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</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-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. application.yml 配置
server:
port: 6600
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: http://${ip.address:127.0.0.1}:8848
config:
server-addr: http://${ip.address:127.0.0.1}:8848
gateway:
routes:
- id: alibaba-consumer
uri: lb://alibaba-consumer
predicates:
- Path=
3. 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
(五)验证
- 分别启动 alibaba-provider、alibaba-consumer 和 gateway
- 访问 http://localhost:6600/sayHello?name=nacos,返回 “hello nacos, This is Spring Cloud Alibaba + Nacos !” 则项目搭建成功。