springcloud-alibaba-Nacos简单使用和配置

一.Nacos注册中心

1.客户端添加依赖

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

2.文件配置

server:
  port: 83


spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #配置nacos地址

#消费者将要去访问微服务的名称(成功注册进nacos的微服务提供者)
server-url:
  nacos-user-servicer: http://nacos-payment-pro

3.config配置

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced  //这天千万别忘了
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

4.controller

/**
 * @author qxL
 * @create 2022-02-04 15:14
 */
@RestController
@Slf4j
public class OrderNacosController {

    @Resource
    private RestTemplate restTemplate;
    @Value("${server-url.nacos-user-servicer}")
    private String serverURl;

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

5.启动类

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

二.服务端

服务端添加两个端口9001和9002,配置一样,改端口即可

Nacos添加了spring-cloud-starter-alibaba-nacos-discovery后就包含ribbon依赖,实现了负载均衡

1.添加依赖

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

2.配置文件

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #配置nacos地址

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

3.controller

@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    
    @GetMapping(value = "/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Integer id){
    return "nacos registry, serverPort:"+serverPort+"\t id"+id;
    }
}

4.启动类

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

5.启动客户端和服务端的端口

springcloud-alibaba-Nacos简单使用和配置

在nacos的控制台可以看到服务启动成功

springcloud-alibaba-Nacos简单使用和配置

访问端口
springcloud-alibaba-Nacos简单使用和配置

springcloud-alibaba-Nacos简单使用和配置

可见两个端口交替访问。

三.Nacos配置中心

1.添加依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>

2.配置文件

spring:
  profiles:
    active: dev #表示开发环境

bootstrap.yml

server:
  port: 3377


spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #配置nacos地址
      config:
        server-addr: localhost:8848 #nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置


  #${prefix}-${spring.profiles.active}.${file-extension}
  #nacos-config-client-dev.yaml

3.controller

@RestController
@RefreshScope //支持nacos的动态刷新功能
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String GetConfigInfo(){
        return configInfo;
    }
}

4.启动类

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClientMain3377.class,args);
    }
}

5.nacos配置

springcloud-alibaba-Nacos简单使用和配置

访问端口

springcloud-alibaba-Nacos简单使用和配置

上一篇:spring cloud alibaba 学习(三)NacosNamingService初始化流程


下一篇:程序员在Medium上写作赚美金的基本流程