spring cloud alibaba 学习(一)环境搭建


前言

Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。

依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。

五大组件选择:

1.服务注册中心 Nacos
2.服务负载均衡 Openfeign
3.服务熔断降级 sentinel
4.服务网关组件 Gateway
5.统一配置中心组件 Nacos


一、安装nacos

下载 : nacos-server-2.0.4.tar.gz

解压: tar -zxvf nacos-server-2.0.4.tar.gz

启动: 进入解压出来的 nacos/bin/ 目录,执行 ./startup.sh -m standalone

访问: 访问8848默认端口,http://172.16.10.159:8848/nacos

输入用户名密码 nacos/nacos

在这里插入图片描述

二、服务提供者

(1)引入依赖

springboot版本使用较低一些的 2.4.13,
spring-cloud-dependencies 版本使用 2020.0.5,
nacos-discovery 版本使用 2021.1

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.13</version>
</parent>

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>2020.0.5</version>
			<type>pom</type>
<!--			<scope>runtime</scope>-->
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
			<version>2021.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

(2)配置文件

#指定端口
server.port=9000
#指定服务名称
spring.application.name=server
#指定nacas地址
spring.cloud.nacos.discovery.server-addr=172.16.10.159:8848

(3)创建服务接口

@RestController
@RequestMapping("/server")
public class ServerController {

    @GetMapping("/test/{id}")
    public Object test(@PathVariable String id) {
        Map map = new HashMap();
        map.put("server", id);
        return map;
    }
}

三、服务消费者

(1)引入依赖

在服务提供者依赖的基础上,加入 openfeign 和 loadbalancer 依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.13</version>
</parent>

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>2020.0.5</version>
			<type>pom</type>
<!--			<scope>runtime</scope>-->
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
			<version>2021.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
			<version>3.0.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-loadbalancer</artifactId>
			<version>3.0.0</version>
		</dependency>
	</dependencies>

(2)配置文件

#指定端口
server.port=9001
#指定服务名称
spring.application.name=client
#指定nacas地址
spring.cloud.nacos.discovery.server-addr=172.16.10.159:8848

(3)启动类

加上注解@EnableFeignClients

@SpringBootApplication
@EnableFeignClients
public class CloudApplication {

	public static void main(String[] args) {
		SpringApplication.run(CloudApplication.class, args);
		System.out.println("started...");
	}

}

(4)创建服务api接口

用@FeignClient 注解标识,value = “server” 表示要访问的服务提供者的名称是server

@FeignClient(value = "server")
@Component
public interface ServerApi {

    @GetMapping("/server/test/{id}")
    Map test(@PathVariable String id);
}

(5)客户端接口

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ServerApi serverApi;

    @GetMapping("/test/{id}")
    public Map test(@PathVariable String id){
        return serverApi.test(id);
    }
}

四、测试

(1)查看nacos
启动服务提供者和服务消费者,查看服务注册

在这里插入图片描述
(2)访问服务消费者接口

访问:

http://192.168.100.73:9001/client/test/100

结果:

{
    "server": "100"
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_lrs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值