docker 安装nacos_轻松入门Nacos

1. 安装nacos,这里采用docker安装,不清楚docker,请移步centos8.0 docker演示

1) 拉取最新版本nacos

docker pull nacos/nacos-server

2) 查看镜像

docker images

3) 启动nacos,这里做了端口映射,映射为8080

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

4) 访问nacos,默认用户名和密码均为nacos,http://127.0.0.1:8080/nacos/#/login

c8b0a3e703a143d24edc11007b8ea432.png

2. 测试服务注册于发现

1) 创建maven父项目nacos-test,删除src目录

2) 引入POM依赖

           org.springframework.boot        spring-boot-starter-parent        2.2.4.RELEASE                                    org.springframework.boot            spring-boot-starter-web                                    com.alibaba.cloud            spring-cloud-starter-alibaba-nacos-discovery            2.2.0.RELEASE                                    groupId>com.alibaba.cloud            spring-cloud-starter-alibaba-nacos-config            2.2.0.RELEASE                                    org.springframework.cloud            spring-cloud-starter-openfeign            2.2.1.RELEASE            

3) 创建生产者

A. 在父项目nacos-test下创建module,此处命名为nacos-service

B. 创建启动类NacosServiceApplication.java

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class NacosServiceApplication {    public static void main(String[] args) {        SpringApplication.run(NacosServiceApplication.class,args);    }}

C. 创建配置文件bootstrap.yml,因为加载顺序的问题,此处必须是bootstrap.yml,而不是application.yml,否则引入了nacos config会启动失败

server:  port: 8091 spring:  application:    #定义服务名    name: nacos-service  cloud    nacos:      discovery:        #注册中心地址        server-addr: http://127.0.0.1:8080      config:        #配置中心地址        server-addr: http://127.0.0.1:8080        #组别        group: DEFAULT_GROUP        #文件类别        file-extension: yaml  #使用环境  profiles:    active: dev

D. 创建类NacosService.java提供服务

import com.alex.nacosconfigfile.ProducerConfigInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class NacosService {    @Value("${server.port}")    private String serverPort;     @Autowired    private ProducerConfigInfo producerConfigInfo;      @GetMapping("/sayHello")    @RefreshScope    public String sayHello(){        return "我是生产者,我的端口号为:"+serverPort+",nacos-config配置内容为:"+producerConfigInfo.getProducerContent();    }}

E. 创建配置文件ProducerConfigInfo.java类,用于测试nacos config

import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.context.annotation.Configuration; @Configuration@RefreshScopepublic class ProducerConfigInfo {    @Value("${producer.content}")    private String producerContent;    public String getProducerContent() {        return producerContent;    }    public void setProducerContent(String producerContent) {        this.producerContent = producerContent;    }}

F. 在控制台创建配置文件nacos-service-dev.yaml

78553c10ae4f0aaed64ebfac6c8dc881.png

G. 分别用8091和8092两个端口启动生产者项目,创建集群,以便验证负载均衡,查看nacos服务列表是否注册成功

35d61bcea53827e8b17bcfd4b2ade9ec.png

4) 创建消费者

A. 在父项目nacos-test下创建module,此处命名为nacos-customer

B. 创建启动类NacosCustomerApplication.java

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication/***开启openfeign客户端*/@EnableFeignClientspublic class NacosCustomerApplication {    public static void main(String[] args) {        SpringApplication.run(NacosCustomerApplication.class,args);    }}

C. 创建配置文件bootstrap.yml,因为是在父pom中引入的依赖,只要引入了nacos config的依赖,配置文件名称都必须是bootstrap.yml,且必须配置nacos config相关信息,否则启动失败

server:  port: 9001spring:  application:    #定义服务名    name: nacos-customer  cloud:    nacos:      discovery:        #注册中心地址        server-addr: http://127.0.0.1:8080      config:        #配置中心地址        server-addr: http://127.0.0.1:8080        #组别        group: DEFAULT_GROUP        #文件类别        file-extension: yaml

D. 创建openfeign客户端,NacosServiceFeign.java

import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;@FeignClient("nacos-service")public interface NacosServiceFeign {    @GetMapping("sayHello")    String sayHello();}

E. 创建消费者NacosCustomerService.java调用生产者接口

import com.alex.feign.NacosServiceFeign;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class NacosCustomerService {    @Autowired    private NacosServiceFeign nacosServiceFeign;     @GetMapping("/getTestResult")    public String getTestResult() {        String result = nacosServiceFeign.sayHello();        return "我是消费者,通过ribbon负载均衡调用sevice返回结果:" + result;    }}

F. 启动消费者,并调用http://127.0.0.1:9001/getTestResult

动态均衡演示:

7b3d53b5c5d2deadcaa40ec8229926ec.png
bf143e972732b5b1d7f2742d853b03a4.png

动态配置修改演示,修改配置文件值为2222,旧值为1111,如图:

c1dd303cd60a3f5457781ccfd587444b.png
0586e0824e5e07f60affbd81371fdc96.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值