Spring cloud Alibaba II. Spring boot configuration uses nacos as the registry, and calls and tests with resttemplate and RPC tools

Premise of use

Building nacos environment: https://blog.csdn.net/qq_41463655/article/details/104002968
RPC tool RestTemplate: https://blog.csdn.net/qq_41463655/article/details/103431953

The simulation environment of this paper is as follows:

1, Create the spring boot project Alibaba server (version 2.0.1)

Producer project simulation

1. pom.xom dependency

        <!--  springboot integration web assembly-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- springboot integration alibaba-nacos-discovery Registry Center -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>

2. yml configuration

server:
  port: 8080
spring:
  application:
    ## Service name
    name: alibaba-server
  cloud:
    nacos:
      discovery:
        ## Service registration address
        server-addr: 192.168.177.128:8848

3. Add getUserId test interface

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserService {

    @GetMapping("/getUserId")
    public String getUserId(String userId){
         return  "userId:"+userId;
    }
}

4. Startup class (no configuration required)

Alibaba nacos discovery will automatically inject services into nacos

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

@SpringBootApplication
class AlibabaServerApplication {

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

2, Create the spring boot project Alibaba client (version 2.0.1)

Consumer project simulation

1. pom.xom dependency

        <!--  springboot integration web assembly-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- springboot integration alibaba-nacos-discovery Registry Center -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>

2. yml configuration

server:
  port: 8081
spring:
  application:
    ## Service name
    name: alibaba-client
  cloud:
    nacos:
      discovery:
        ## Service registration address
        server-addr: 192.168.177.128:8848

3. Add consumer interface - > call producer interface

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class LoginService {

    /**
     * rpc Calling tool
     */
    @Autowired
    private RestTemplate restTemplate;
    /**
     * Get registry data object
     */
    @Autowired
    private DiscoveryClient discoveryClient;


    @GetMapping("/login")
    public String login(){
       // Obtain the address information of the ip+ port registered to nacos through the service name (the cluster will obtain more than one)
        List<ServiceInstance> instances = discoveryClient.getInstances("alibaba-server");
        String ipAddr = instances.get(0).getUri().toString();
        String url  = ipAddr+"/getUserId?userId=123456789";
        // rpc call
        String result = restTemplate.getForObject(url, String.class);
        System.out.println("call alibaba-server service,result:" + result);
        return result;
    }
}

4. Start class injection RestTemplate module (rpc calling tool)

@SpringBootApplication
class AlibabaClientApplication {

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


    /**
     * TODO   RestTemplate =  rest,http Style, api calling tool
     *
     * @return org.springframework.web.client.RestTemplate
     * @date 2019/12/6 0006 12:02
     */
    @Bean              //Inject spring boot container
    //@LoadBalanced / / let RestTemplate have the ability of client load balancing when requesting
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

3, Test results

1. Nacos view registered services after project launch

2. URL access to the / login interface of the Alibaba client service

http://127.0.0.1:8081/login
Output results:

182 original articles published, praised 23, visited 60000+
Private letter follow

Keywords: Spring SpringBoot Java REST

Added by FortMyersDrew on Sat, 18 Jan 2020 18:03:30 +0200