前言
随着SpringCloud Netflix组件的停止更新,大多数国内的互联网公司把目光转向Spring Cloud Alibaba,so,果断入坑,下面把学习的过程记录一下。SpringCloud Alibaba的主要组件
这些是Spring Cloud Alibaba的几个主要组件
- Nacos 服务注册和发现组件,类似Eureka,除此还包含配置中心的功能,类似Config
- Sentinel 限流、熔断和降级组件,类似Hystrix
- Seata 分布式事务
- RocketMQ Alibaba的消息中间件,以高性能和高吞吐量为特点
- Dubbo Alibaba的RPC组件
使用步骤
这里我们需要服务提供者来提供商品数据,服务提供者主要注册到nacos中,服务消费者通过nacos来调用服务提供者。1.安装Nacos
-
下载Nacos
下载地址https://github.com/alibaba/nacos/releases -
复制文件到Linux的/usr/local目录下
cd /usr/local
tar -vxf nacos-server-1.4.0.tar.gz
cd nacos/bin
sh startup.sh -m standalone
PS:启动文件位于nacos的bin目录下,这里以单机模式启动,除此还有集群模式 -
打开浏览器,输入http://Linux主机IP:8848/nacos/index.html,就可以看到Nacos的登录界面
账号密码默认都是nacos,输入后进入主界面
2.创建父项目
先创建一个SpringBoot父项目,便于统一管理项目依赖,下面是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.xray</groupId>
<artifactId>springcloud_alibaba_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud_alibaba_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.milanosoft.com:444/repository/maven-aliyun-mirror/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 服务提供者
在父项目中,新建Module作为服务提供者,继承父项目,同时引入nacos依赖:<?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>com.xray</groupId>
<artifactId>springcloud_alibaba_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xray</groupId>
<artifactId>provider_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider_service</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.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类上需要添加注解:@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderServiceApplication.class, args);
}
}
配置文件application.yml
server:
port: 8002
spring:
application:
name: provider-service
cloud:
nacos:
discovery:
server-addr: 192.168.7.188:8848 # nacos的地址,端口默认是8848
商品实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
private Long id;
private String name;
private Double price;
}
Service类
@Service
public class GoodsService {
public Goods findGoods(){
return new Goods(1L,"MI笔记本5400",5555.0);
}
}
Controller类
@RestController
@RequestMapping("provider")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("goods")
public Goods findGoods(){
return goodsService.findGoods();
}
}
4. 服务消费者
在父项目中创建新的Module作为服务消费者,消费者也需要继承父项目,引入nacos依赖pom文件可以参考服务提供者修改就可以了
这里也需要添加Goods实体类,启动类上添加@EnableDiscoveryClient,这里就不写了
配置文件,修改下端口
server:
port: 8001
spring:
application:
name: consumer-service
cloud:
nacos:
discovery:
server-addr: 192.168.7.188:8848
下面我们需要使用RestTemplate来调用服务提供者的接口,需要添加配置类:
@Configuration
public class RestTemplateConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
编写控制器调用服务提供者
@RestController
@RequestMapping("consumer")
public class GoodsController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("goods")
public Goods findGoods(){
//通过服务id调用服务提供者
Goods goods = restTemplate.getForObject("http://provider-service/provider/goods", Goods.class);
return goods;
}
}
5.测试
打开浏览器,输入:http://localhost:8001/consumer/goods出现结果:
这里我们访问的是服务消费者的接口,服务消费者通过nacos查找服务提供者,然后成功调用服务提供者的接口获得商品数据。在nacos后台可以看到上面两个服务:
总结
本文对SpringCloud Alibaba的nacos组件做了基本的介绍,它可以完成服务的注册和发现,还能作为配置中心管理服务的配置,入门也比较简单,如果本文对你有用,左下角,你懂的!SpringCloud Alibaba 入坑(二)Nacos 配置中心
大家如果需要学习其他Java知识点,戳这里 超详细的Java知识点汇总