Spring Cloud Hoxton.SR9版本(十四)Cloud Alibaba Nacos注册中心

本文档详细介绍了如何搭建Nacos服务器,配置单机模式运行,并在Spring Cloud应用中使用Nacos进行服务注册与发现。通过创建两个服务提供者和一个服务消费者,展示了Nacos的负载均衡功能。此外,还讨论了Nacos在AP和CP模式下的应用场景。
摘要由CSDN通过智能技术生成

1.Nacos官网https://nacos.io/zh-cn/docs/what-is-nacos.html

2.官网下载1.4版本https://github.com/alibaba/nacos/releases/tag/1.4.0

3.解压zip,并运行nacos。执行命令startup.cmd -m standalone(standalone代表着单机模式运行,非集群模式) 

4.启动成功后,访问http://localhost:8848/nacos,用户名密码默认是nacos

5.父pom增加依赖

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.alibaba.cloud</groupId>
				<artifactId>spring-cloud-alibaba-dependencies</artifactId>
				<version>2.1.0.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

 6.创建model cloud-alibaba-payment-6001

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>com.yinuo</groupId>
		<artifactId>springcloud-hoxton</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.yinuo</groupId>
	<artifactId>cloud-alibaba-payment-6001</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>cloud-alibaba-payment-6001</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</artifactId>
		</dependency>


		<dependency>
			<groupId>com.alibaba.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>

yml

management:
  endpoints:
    web:
      exposure:
        include: '*'
server:
  port: 6001
spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

主启动类

package com.yinuo.payment6001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudAlibabaPayment6001Application {

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

}

7.创建TestController

package com.yinuo.payment6001.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "payment/nacos/{id}")
    public String get(@PathVariable("id") int id){
        return "I am form:"+serverPort+"   and id is:"+id;
    }
}

8.访问http://localhost:6001/payment/nacos/1

9.查看Nacos可视化界面

10.copy6001,新建model6002

 

11.新建消费者model cloud-alibaba-consumer-6003 

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>com.yinuo</groupId>
		<artifactId>springcloud-hoxton</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.yinuo</groupId>
	<artifactId>cloud-alibaba-consumer-6003</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>cloud-alibaba-consumer-6003</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</artifactId>
		</dependency>


		<dependency>
			<groupId>com.alibaba.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>

yml

management:
  endpoints:
    web:
      exposure:
        include: '*'
server:
  port: 6003
spring:
  application:
    name: nacos-payment-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

provider:
  service-id: http://nacos-payment-provider/

主启动类

package com.yinuo.consumer6003;

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.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudAlibabaConsumer6003Application {

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

	/**
	 * 注入RestTemplate
	 *
	 * **/
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
}

12.新建TestController

package com.yinuo.consumer6003.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {
    @Autowired
    private RestTemplate template;

    @Value("${provider.service-id}")
    private String paymentUrl;


    @GetMapping("nacos/get/{id}")
    public String get(@PathVariable("id") int id){
        return template.getForObject(paymentUrl+"payment/nacos/"+id,String.class);
    }
}

13.启动项目,查看Nacos界面

14.访问 http://localhost:6003/nacos/get/1,多次访问可以发现,Nacos自带负载均衡功能,默认是轮询的方式,其原因是Nacos整合了Ribbon

15.Nacos是支持AP和CP切换的。C是所有节点在同一时间看到的数据是一致的,A是所有的请求都会得到响应。一般来说,如果不需要存储服务级别的信息且服务实例是通过nacos-client注册并能够保持心跳上报,那么就可以选择AP模式。当前主流的服务如SpringCloud和Dubbo服务,都是用与AP模式,AP模式是为了服务的可能性而减弱了一致性,因此AP模式下只支持注册临时实例。如果需要在服务级别编辑或存储配置信息,那么CP是必须的,K8s和DNS服务则适用于CP模式。CP模式下则支持注册持久化实例,此时则以Raft协议为集群运行模式,该模式下注册实例之前必须先注册服务,如果服务不存在,则会返回错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值