Spring Cloud Alibaba: Use Nacos to realize service registration and discovery

Attachment: Nacos documentation

Attached: [github] nacos download address

ps: downloading the nacos installation package directly from github will be particularly slow. The online host modification method has been used, and it has no effect. It is recommended to use a VPN or use someone else has downloaded it;

Attached: [csdn] nacos-server-1.1.0.tar.gz

tar -zxvf nacos-server-1.1.0.tar.gz  解压
cd nacos
cd bin
sh startup.sh -m standalone 启动

The following figure appears, indicating successful startup. 

Log http://192.168.101.16:8848/nacos /index.html , and enter the account password; both nacos . The following interface appears; 

Note: The version corresponds to:

Project structure:

1. Module: alibaba-nacos-discovery-server:

Control layer:

package com.lucifer.demo.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author: lucifer
 * @date: 2019/8/6
 * @description:
 */
@Slf4j
@RestController
public class TestController {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable(name = "name") String name) {
        log.info("invoked name = " + name);
        return "hello " + name;
    }


}

springboot startup class: (ps: @EnableDiscoveryClient open service registration discovery function)

package com.lucifer.demo;

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

/**
 * @author Lucifer
 */
@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaNacosDiscoveryServerApplication {

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

}

springboot configuration file: 

spring:
  application:
    name: service-provider
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.16:8848

server:
  port: 8001

pom.xml: (main jar package) 


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

start up:

2. Module: alibaba-nacos-discovery-client

1》 springboot startup class: same as alibaba-nacos-discovery-server startup class, plus @EnableDiscoveryClient;

2》 Configuration file:

spring:
  application:
    name: service-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.16:8848

server:
  port: 8080

3》 pom.xml Same as above

 Control layer:

package com.lucifer.alibabanacosdiscoveryclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author: lucifer
 * @date: 2019/8/6
 * @description:
 */
@EnableDiscoveryClient
@RestController
public class TestController {

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

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping(value = "/hello/{str}")
    public String echo(@PathVariable(name = "str") String str) {
        return restTemplate.getForObject("http://service-provider/hello/" + str, String.class);
    }


}

start up:

Visit http://192.168.101.16:8848/nacos/ again

postman test:

1.localhost:8080/hello/zhangsan

 2.localhost:8001/hello/zhangsan

ps:

Enter the service details, click on the offline, as shown in the figure:

Go to the interface again: the service provider has no instances available

Click online again, wait a few seconds-a dozen seconds, and then visit the interface again: normal;

 
Published 187 original articles · Like 146 · Visit 490,000+

Guess you like

Origin blog.csdn.net/qq_37495786/article/details/98626432