spring-cloud-alibaba Nacos configuration start

1. Why is it called Nacos:

The first four letters are the first two letters of Naming and Configuration, and the last s is Service.

Nacos is a combination of registration center + configuration center


  • Nacos supports DNS-based and RPC-based service discovery. After the service provider uses the native SDK, OpenAPI, or an independent Agent TODO to register the Service, the service consumer can use DNS TODO or HTTP&API to find and discover the service.

  • Nacos provides real-time health checks on services to prevent sending requests to unhealthy hosts or service instances. Nacos supports health checks at the transport layer (PING or TCP) and application layer (such as HTTP, MySQL, user-defined). For the health check of services in complex cloud environments and network topology environments (such as VPC, edge networks, etc.), Nacos provides two health check modes: agent reporting mode and server-side active detection. Nacos also provides a unified health check dashboard to help you manage service availability and traffic based on your health status. java project www.fhadmin.org


    6937285ceb2b4f72304a59472b1d7990.webp

    Insert picture description here

Install and run nacos

Install nacos on docker

Pull the nacos mirror:

docker pull nacos/nacos-server

Run nacos:

docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server

Enter in the browser: http://ip address:8848/nacos/ The
account and password are both nacos.

3f4703e959622be0a1eadb692e4b9ba5.webp

Insert picture description here


38f59f4da5312d368440fa6dcdffbdb2.webp

Insert picture description here

Demonstration of Nacos as a service registry

  1. New module cloudalibaba-provider-payment9001
  2. pom
<dependencies>
    <!--SpringCloud Alibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. marl
server:
  port: 9001


spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: 120.92.164.250:8848 #Configured Nacos address (write localhost:8848 for this machine, write IP address for the server)


management:
  endpoints:
    web:
      exposure:
        include: '*'
  1. Main start class
//java project www.fhadmin.org
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001 {

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

}
  1. Create a new controller package, and create a new PaymentController under the package
//java project www.fhadmin.org
@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;


    @GetMapping("/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Integer id){
        return "nacos registry, serverPort: " + serverPort + "\t id: " + id;
    }

}
  1. Start 9001

2376a75508e41203cbfcffb21943f5f9.webp

Insert picture description here

Consumer (integrated with Feign)

  1. 新建 cloudalibaba-consumer-nacos-order83
  2. pom
<dependencies>
        <!--SpringCloud Alibaba nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- Refer to the api universal package defined by yourself, you can use Payment to pay for Entity -->
        <dependency>
            <groupId>com.angenin.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
  1. L yml
server:
  port: 83


spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 120.92.164.250:8848 #Configured Nacos address (write localhost:8848 for this machine, write IP address for the server)


#The name of the microservice that the consumer wants to access (service provider successfully registered into nacos)
service-url:
  nacos-user-service: http://nacos-payment-provider
  1. Main start class
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderNacosMain83 {

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

}
  1. Create a new feign package and add an interface PaymentFeignService under the package
@Component
@FeignClient(value = "nacos-payment-provider")
public interface PaymentFeignService {

    @GetMapping("/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Integer id);

}
  1. New controller
@RestController
public class OrderNacosController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/feign/nacos/{id}")
    public String paymentInfo2(@PathVariable("id") Integer id){
        return paymentFeignService.getPayment(id);
    }

}
  1. start up


    5c030ee36f8380fc59d20fca35907d37.webp


Guess you like

Origin blog.51cto.com/14622073/2675264