spring cloud alibaba nacos部署启动

1. 父pom版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencyManagement>
   <dependencies>
      <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-alibaba-dependencies</artifactId>
	<version>0.9.0.RELEASE</version>
	<type>pom</type>
      </dependency>
   </dependencies>
</dependencyManagement>

阿里巴巴中间件推文:https://mp.weixin.qq.com/s/poFmP0qScdrxVaeZuFz0Mw

2. 安装nacos服务, docker方式

nacos官网:https://nacos.io/zh-cn/docs/what-is-nacos.html

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

启动后访问: ip:8848/nacos/index.html
账号密码:nacos/nacos
Nacos单机模式默认使用内嵌的数据库作为存储引擎, 生产环境应使用集群模式

2.1. 启动配置管理

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
配置文件:bootstrap.yml

spring:
  profiles:
    active: dev
  application:
    name: ecg-system
  cloud:
    nacos:
      config:
        server-addr: 172.17.0.120:8848
        fileExtension: yml

server:
  port: 8008
ccc:
  name: swell
@SpringBootApplication
@RestController
@RefreshScope //spring cloud 原生注解 @RefreshScope 实现配置自动更新
public class EcgSystemApplication {

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

    @Value("${ccc.name}")
    private String useLocalCache;

    @RequestMapping("/get")
    public String get() {
        return useLocalCache;
    }
}

图中Data ID组成格式:   ${prefix}-${spring.profile.active}.${file-extension},prefix: 应用名。详情看官网

即: ecg-system-dev.yml  访问 localhost:8018/get  可看到最新配置

注:可通过环境变量区分dev, test, prod 把相关属性写入nacos,很多配置不放在本地,本地yml干净整洁,

       nacos配置文件由运维管理员管理,安全+1

3.1 启动服务发现

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
配置文件: bootstrap.yml

spring:
  profiles:
    active: dev
  application:
    name: ecg-system
  cloud:
    nacos:
      config:
        server-addr: 172.16.0.251:8848
        fileExtension: yml
      discovery:
        server-addr: 172.16.0.251:8848
@SpringBootApplication
@EnableDiscoveryClient  //Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能
@RestController
@RefreshScope
public class EcgSystemApplication {

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

服务已注册上:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值