微服务注册中心现在常用的主流有三个
Nacos,Eureka与ZooKeeper
Nacos是阿里开源的,Nacos 支持基于 DNS 和基于 RPC 的服务发现。在Spring Cloud中使用Nacos,只需要先下载 Nacos 并启动 Nacos server,Nacos只需要简单的配置就可以完成服务的注册发现。
Nacos除了服务的注册发现之外,还支持动态配置服务。动态配置服务可以让您以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置。动态配置消除了配置变更时重新部署应用和服务的需要,让配置管理变得更加高效和敏捷。配置中心化管理让实现无状态服务变得更简单,让服务按需弹性扩展变得更容易。
Nacos = Spring Cloud注册中心 + Spring Cloud配置中心
进入正题,nacos下载 https://github.com/alibaba/nacos/releases?page=1
本地测试下载win版本的
解压后显示当前目录文件
通过bin目录下的startup.cmd直接启动,下载后直接启动会报错,因为默认模式是集群方式。在启动文件中做修改
startup.cmd中找到 set MODE="cluster" 修改为 set MODE="standalone"
然后启动
打开浏览器 http://本机ip:8848/nacos/index.html
帐号密码都是 nacos
这里单机版的nacos注册中心就完成了。
现在编写spring微服务
新建maven项目
项目目录
pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.13.RELEASE cn.joral nacos-server0.0.1-SNAPSHOT nacos-server 用户服务 1.8 Greenwich.SR5 2.1.1.RELEASE org.springframework.boot spring-boot-starter-webcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery${spring-nacos.version} org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
启动类 DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RestController
static class TestController {
@GetMapping("/hello")
public String hello(String name) {
//log.info("name:{}", name);
return "hello " + name;
}
}
}
application.yml
server:
port: 8081
spring:
application:
name: nacos-server
cloud:
nacos:
discovery:
server-addr: 192.168.0.107:8848
启动以后再nacos里面就可以看到注册的服务了
在浏览器输入
http://192.168.0.107:8081/hello?name=word 192.168.0.107是本机ip
证明服务正常可以调用。
现在编写消费者
另外新建一个maven
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.13.RELEASE cn.joral jmall-userc0.0.1-SNAPSHOT jmall-user 客户端 1.8 Greenwich.SR5 2.1.1.RELEASE org.springframework.boot spring-boot-starter-webcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery${spring-nacos.version} org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
DemoApplication 启动类
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import lombok.extern.slf4j.Slf4j;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Slf4j
@RestController
static class TestController{
@Autowired
LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test(){
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-server");
String url = serviceInstance.getUri() + "/hello?name=" + "zhangsan";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return "url : " + url + ", return : " + result;
}
}
}
application.yml
server:
port: 8080
spring:
application:
name: nacos-client
cloud:
nacos:
discovery:
server-addr: 192.168.0.107:8848
启动服务
http://192.168.0.107:8080/test
调用成功。到此nacos就完成了。



