Nacos profile

SpringCloud registry and configuration center

Nacos website

Nacos starts fast

Build Windows NacOS

Decompress the installation package from the official website and run bin/startup. CMD

Open visual console

http://localhost:8848/nacos account password: nacos

Create a parent projectcloud-alibaba-projectIt containsOne service provider, Cloud-Alibaba-PaymentCloud-alibaba-consumer, a service consumer

Service Provider Module

Introducing environment dependencies

<! Maven Spring-cloud-starter-alibaba-nacos-discovery maven Spring-cloud-starter-alibaba-nacos-discovery maven Spring-cloud-starter-alibaba-nacos-discovery maven Spring-cloud-starter-alibaba-nacos-discovery
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<! -- SpringBoot project dependencies -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.2.2. RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.2.2. RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

application.yml


# project port number
server:
  port: 8008
  
# Project name
spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 Configure the Nacos address

management:
  endpoints:
    web:
      exposure:
        include: The '*'  Only /health and /info endpoints are exposed by default
Copy the code

The app to start class

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

@SpringBootApplication

// Register the current service provider with the registry
@EnableDiscoveryClient
public class PaymentMain9001 {
    public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class,args); }}Copy the code

Start the service provider to view the successful registration to the registry

Consumer Module

Introduction of depend on

<! Maven Spring-cloud-starter-alibaba-nacos-discovery maven Spring-cloud-starter-alibaba-nacos-discovery maven Spring-cloud-starter-alibaba-nacos-discovery maven Spring-cloud-starter-alibaba-nacos-discovery
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<! -- SpringBoot project dependencies -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.2.2. RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.2.2. RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

application.yml

Spring: Application: name: nacos-order- Consumer Cloud: nacos: discovery: server-addr: Nacos-payment-provider (optional) service-url: nacos-user-service: nacos-payment-provider (optional) service-url: nacos-user-service: http://nacos-payment-providerCopy the code

The app to start class

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

// Register the consumer in nacOS
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain83{
    public static void main(String[] args){ SpringApplication.run(OrderNacosMain83.class,args); }}Copy the code

View whether the current service consumer is registered with the registry

The service consumer invokes the service provider

Call that

The current service consumer invokes the service provider through the RestTemplate or you can configure the Openfeign microservice interface for remote invocation yourself

Service provider creation: This class is created under the Controller package for the service consumer to invoke

import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Long id) { return "nacos registry, serverPort: "+ serverPort+"\t id"+id; }}Copy the code

Service consumer creation: This class is created in service consumer creation package config

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig{
    @Bean
    @LoadBalanced// Configure the default polling policy for the load balancing ribbon
    public RestTemplate getRestTemplate(a){
        return newRestTemplate(); }}Copy the code

Service consumer creation: Create this class under the Controller package

import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; 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; import javax.annotation.Resource; @RestController @Slf4j public class OrderNacosController{ @Resource private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}") private String serverURL; @getMapping (value = "/consumer/payment/nacos/{id}") public String @getMapping (value = "/consumer/payment/nacos/{id}" paymentInfo(@PathVariable("id") Long id){ return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);  }}Copy the code

Call successful: Access the service consumer's controller