Discovery Consumers in the Spring Cloud + Nacos Trilogy

Keywords: Spring network SpringBoot

1,Config of Spring Cloud+Nacos Trilogy

2,Discovery Service Registration Discovery in the Spring Cloud + Nacos Trilogy

3,Discovery Consumers in the Spring Cloud + Nacos Trilogy

Edition

  1. springboot version: 2.1.6.RELEASE
  2. nacos version Nacos 1.1.0

Create a springboot project

Quick Start

  1. pom reference
       <!--Configuration shielding,Convenient testing-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
            <!--<version>0.9.0.RELEASE</version>-->
        <!--</dependency>-->
        <!--Service registration-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>
  1. yml configuration

Modify the configuration name: bootstrap.yml
Add configuration information

spring:
  application:
    name: nacos-client
  cloud:
    nacos:
#      config:
#        server-addr: 127.0.0.1:8848
#        file-extension: yaml
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 8086


  1. Code

a.NacosController.class

@RestController
public class NacosController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @Value("${spring.application.name}")
    private String appName;

    @GetMapping("/echo/app-name")
    public String echoAppName() {
        //Access using a combination of LoadBalanceClient and RestTemolate
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-producer");
        String url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
        System.out.println("request url:" + url);
        return restTemplate.getForObject(url, String.class);
    }

}

b.NacosDiscoveryClientApplication.class

/**
 * @author huangdeyao
 */
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryClientApplication {

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

    //Instantiate RestTemplate instances
    @Bean
    public RestTemplate restTemplate() {

        return new RestTemplate();
    }
}

Start NacosDiscovery Client Application and NacosDiscovery Producer Application projects, respectively Discovery Service Registration Discovery in the Spring Cloud + Nacos Trilogy

Request interface: http://localhost:8086/echo/app-name
Return: Hello Nacos Discovery nacos-client

Here you can start a few more NacosDiscoveryProducerApplication Instances, always brush requests, you can find the background print information. nacos Default load balancing Ribbon Mechanisms for polling

request url:http://192.168.177.1:8085/echo/nacos-client
request url:http://192.168.177.1:8085/echo/nacos-client
request url:http://192.168.177.1:9999/echo/nacos-client
request url:http://192.168.177.1:8085/echo/nacos-client
request url:http://192.168.177.1:9999/echo/nacos-client
request url:http://192.168.177.1:8085/echo/nacos-client

More information about Nacos Starter configuration items

More starter configurations for spring-cloud-starter-alibaba-nacos-discovery are as follows:

Configuration Items Key Default values Explain
Server address spring.cloud.nacos.discovery.server-addr nothing Nacos Server Start Up the Listening ip Address and Port
service name spring.cloud.nacos.discovery.service ${spring.application.name} Name the current service
weight spring.cloud.nacos.discovery.weight 1 The larger the value is, the larger the weight is.
adapter name spring.cloud.nacos.discovery.network-interface nothing When IP is not configured, the registered IP corresponds to the IP address of the network card. If this is not configured, the address of the first network card is chosen by default.
Registered IP Address spring.cloud.nacos.discovery.ip nothing The highest priority
Registered Port spring.cloud.nacos.discovery.port -1 By default, no configuration is required, and automatic detection is performed.
Namespace spring.cloud.nacos.discovery.namespace nothing One of the common scenarios is the separation of registrations between different environments, such as the isolation of resources (such as configuration, services) in the development test environment and in the production environment.
AccessKey spring.cloud.nacos.discovery.access-key nothing When you want to go to Aliyun, a cloud account name on Aliyun
SecretKey spring.cloud.nacos.discovery.secret-key nothing When you want to go to Aliyun, there is a cloud account password on Aliyun.
Metadata spring.cloud.nacos.discovery.metadata nothing Using Map format configuration, users can customize some metadata information related to services according to their needs.
Log File Name spring.cloud.nacos.discovery.log-name nothing
colony spring.cloud.nacos.discovery.cluster-name DEFAULT Configure to Nacos cluster name
Access Point spring.cloud.nacos.discovery.enpoint UTF-8 The entry domain name of a service in a region, through which the server's address can be dynamically obtained.
Whether to integrate Ribbon ribbon.nacos.enabled true It's usually set to true.

Code
Personal website

reference

Nacos Config Example

Spring Cloud Alibaba Nacos Config

Spring Cloud Alibaba Nacos Discovery

Posted by woobarb on Fri, 19 Jul 2019 23:38:04 -0700