Alibaba Nacos学习记录

Alibaba Nacos

Nacos注册中心

1.下载Nacos并启动Nacos Server

Alibaba Nacos学习记录

2.Idea创建项目

使用Spring Initiaizar工具,选择Spring Web,生成项目

添加依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

启动Nacos Discovery Provider进行服务注册

package com.laowsoft.nacosprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NacosProviderApplication {

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

}

编写RestController

package com.laowsoft.nacosprovider.controller;

import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EchoController {

    @GetMapping("/echo")
    public String echo(HttpServletRequest request){
        return "echo:" + request.getParameter("name");
    }
}

application.yml配置文件

# 要注册的服务名
spring:
  application:
    name: my-provider
#Nacos Server地址信息
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

#要启动的端口号,也就是要注册的端口号
server:
  port: 8066

启动应用,成功注册

Alibaba Nacos学习记录

3.启动Nacos Discovery Customer服务发现

使用Spring Initiaizar工具,选择Spring Web,生成项目

添加依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

新建NacosConsumer启动类

package com.laosoft.nacosconsumer;

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.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient(autoRegister = false)
public class NacosConsumerApplication {

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

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

    @RestController
    class HelloController{

        @Autowired
        private DiscoveryClient discoveryClient;

        @Autowired
        private RestTemplate restTemplate;

        private String serviceName = "my-provider";

        @GetMapping("/info")
        public String info(){
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);

            StringBuilder sb = new StringBuilder();
            sb.append("All Service:"+discoveryClient.getServices()+"</br>");
            sb.append("my-provider instance list: </br>");
            serviceInstances.forEach(instance -> {
                sb.append("[ServiceId:" + instance.getServiceId()+
                        ",host" + instance.getHost()+
                        ",port"+ instance.getPort() +"]");
            });
            sb.append("</br>");
            return sb.toString();
        }
        
        @GetMapping("/hello")
        public String hello(){
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);
            ServiceInstance serviceInstance = serviceInstances.stream().findAny().orElseThrow(() ->
                new IllegalStateException("no" + serviceName + " instance available"));
            return restTemplate.getForObject("http://" + serviceInstance.getHost() +":"+serviceInstance.getPort()
            +"/echo?name=nacos",String.class);
        }
    }
}

application.properties配置

#应用名
spring.application.name= nacos-consumer
#应用启动端口
server.port= 8067

#Nacos Server地址信息
spring.cloud.nacos.discovery.server-addr= localhost:8848

启动NacosConsumer,访问localhost:8067/info

All Service:[my-provider]
my-provider instance list:
[ServiceId:my-provider,host129.125.100.225,port8066]

访问localhost:8067/hello

echo:nacos

服务发现成功

上一篇:SpringCloud Alibaba-1-系统架构演变与介绍


下一篇:SpringCloud-01