Spring Cloud Nacos 使用快速开始

Spring Cloud 是什么

我们先来打开Spring Cloud的官网,学习一个东西最好的办法是通过官网来认识它,如果这个东西官网做的一塌糊涂我们就没必要花费心思去学了。
打开地址:https://spring.io/projects/spring-cloud
在这里插入图片描述
这一段就是对Spring Cloud的解释。翻译过来大致意思:
Spring Cloud 为开发者提供了快速构建分布式系统中一些常用模式的工具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态)。分布式系统的协调导致了样板模式,使用 Spring Cloud 开发人员可以快速建立实现这些模式的服务和应用程序。它们将适用于任何分布式环境,包括开发人员自己的笔记本电脑、裸机数据中心和托管平台(如 Cloud Foundry)。
简而言之呐,Spring Cloud 继承了分布式服务中所需的各个组件。将各厂家优秀的分布式开源组件继承在一起构建成Spring Cloud。就像拼图一样,Spring Cloud 就是拼图的框框,各个组件适配到这个框框中。组成一套分布式解决方案。

方案对比

Spring Cloud 比较优秀常用的方案,大致有三种。
1、Spring Cloud Alibaba
2、Spring Cloud Netflix
3、各自组件自由组装(Dubbo zookeeper …)

第三种方案使用比较复杂。前两种都是提供了全套方案。所以我们对比下前两种:

在这里插入图片描述
两者社区活跃度和github star阿里巴巴都要更好点。
在这里插入图片描述
在这里插入图片描述
那我们就来看下Spring Cloud Alibaba吧。

服务注册组件Nacos

学习第一步弄清楚什么是nacos
来到nacos官网
https://nacos.io/zh-cn/docs/what-is-nacos.html
在这里插入图片描述
Nacos 在微服务种可以当做配置中心和服务的注册中心。

Nacos快速开始

**

准备环境

**

Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用:

64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac64 bit JDK 1.8+;下载 & 配置。
Maven 3.2.x+;下载 & 配置。

下载源码或者安装包

git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  
ls -al distribution/target/

// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin

启动服务器

Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows
启动命令(standalone代表着单机模式运行,非集群模式):

startup.cmd -m standalone

在这里插入图片描述
在这里插入图片描述

服务注册&发现和配置管理

服务注册

curl -X POST "http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080"

这里使用了服务注册代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaojing
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(NacosProviderApplication.class, args);
	}

	@RestController
	class EchoController {
		@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
		public String echo(@PathVariable String string) {
			return "Hello Nacos Discovery " + string;
		}
	}
}

application.properties 配置如下

server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

在这里插入图片描述

服务发现

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author xiaojing
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        }
    }
}

application.properties 配置如下

server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

在这里插入图片描述

这里消费者使用restTemplate传入服务提供者名称service-provider和地址、参数既可以成功调用服务提供者的接口。

发布配置

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"

在这里插入图片描述
获取配置

curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值