微服务 – Spring Cloud – Nacos服务注册、发现

服务注册

1、引入依赖

父pom依赖

<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  <version>2.1.0.RELEASE</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>


子pom依赖


<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2、配置文件

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:7848

management:
  endpoints:
    web:
      exposure:
        include: '*'

3、主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaPaymentMain9001 {

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

}

第三部完成 打开nacos 在服务列表即可看到注册进来的服务.

4、业务类 写一个接口供服务发现者使用

@RestController
public class PaymentController {

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

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

}

服务发现

1、引入依赖

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2、配置文件

server:
  port: 9002

spring:
  application:
    name: nacos-payment-consumner
  cloud:
    nacos:
      discovery:
        server-addr: localhost:7848

management:
  endpoints:
    web:
      exposure:
        include: '*'

3、主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaConsumnerMain9002 {

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

}

打开nacos 在服务列表即可看到注册进来的服务.

4、发现第一个服务 并调用第一个服务提供的接口

nacos默认支持负载,因为底层nacos包集成了Ribbon

服务调用用的还是RestTemplate

在第二个服务中,配置RestTemplate

@Configuration
public class ApplicationContextConfig {

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

}

业务类中调用第一个服务的接口

@RestController
public class ConsumnerController {

    public static final String SERVER_URL = "http://nacos-payment-provider";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/{id}")
    public String paymentInfo(@PathVariable("id") Long id) {
        return restTemplate.getForObject(SERVER_URL + "/payment/nacos/" + id, String.class);
    }

}

访问http://localhost:9002/consumer/1

nacos registery , server port: 9001 id: 1