cloud Alibaba电商后台组件实战:Nacos2.0.3配置持久化与集群(跟着步骤走,绝对不会出错)

x33g5p2x  于2021-12-05 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(229)

Nacos2.0.3配置持久化与集群(跟着步骤走,绝对不会出错)

  • java 1.8 +
  • maven 3.2+

发行版github地址:https://github.com/alibaba/nacos/releases

选择最新的稳定版本 nacos2.0.3

下载解压即可

选择我们需要下载的对应安装包

注意 所有路径上的文件夹一定得是非中文无空格的不然会出现各种玄学的报错

打开mysql创建数据库 nacos-config(也可以自定义起名)库 执行这conf中的两个sql脚本文件

打开我们的conf 修改持久化文件

之后配置cluster.conf 我们要集群的地址 ip+端口

依次启动即可

服务注册进nacos client编写

创建新模块 e-commerce-naocs-client
所需依赖

<dependencies>
       <!-- spring cloud alibaba nacos discovery 依赖 -->
       <dependency>
           <groupId>com.alibaba.cloud</groupId>
           <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
           <version>2.2.3.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>com.imooc.ecommerce</groupId>
           <artifactId>e-commerce-mvc-config</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>
       <!-- 数据绑定 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <optional>true</optional>
       </dependency>
       <!-- nacos config -->
       <dependency>
           <groupId>com.alibaba.cloud</groupId>
           <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
       </dependency>
       <!-- zipkin = spring-cloud-starter-sleuth + spring-cloud-sleuth-zipkin-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-zipkin</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.kafka</groupId>
           <artifactId>spring-kafka</artifactId>
           <version>2.5.0.RELEASE</version>
       </dependency>
       <!-- Ribbon -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
       </dependency>
       <!-- open feign -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-openfeign</artifactId>
       </dependency>
       <!-- feign 替换 JDK 默认的 URLConnection 为 okhttp -->
       <dependency>
           <groupId>io.github.openfeign</groupId>
           <artifactId>feign-okhttp</artifactId>
       </dependency>
       <!-- 使用原生的 Feign Api 做的自定义配置, encoder 和 decoder -->
       <dependency>
           <groupId>io.github.openfeign</groupId>
           <artifactId>feign-gson</artifactId>
           <version>11.0</version>
       </dependency>
       <!-- 集成 hystrix -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
       </dependency>
   </dependencies>

这里我们使用 bootstrap.yml应为nacos也可以上云配置我们要保证他优先被夹在,bootstrap优先级大于application 于是我们使用bootstrap

server:
  port: 8000
  servlet:
    context-path: /ecommerce-nacos-client

spring:
  application:
    name: e-commerce-nacos-client #注册到nacos的服务名称 之后的服务调用 等等 这里在naocs配置上云的时候 会师config的前缀
  cloud:
    nacos:
      discovery:
        server-addr:http: 192.168.3.8:8848,192.168.3.8:8858,192.168.3.8:8868 #要注册的nacos服务中心的地址
        enabled: true # 使用naocs作为服务注册中心
        namespace: e25fa4c3-8b0b-4eb5-ae4f-6d1b8c7fd7ea #e25fa4c3-8b0b-4eb5-ae4f-6d1b8c7fd7ea 对应nacos上的配置命名空间的id
#暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
@Slf4j
@Service
public class NacosClientService {
    public final DiscoveryClient discoveryClient;

    public NacosClientService(DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
    }

    /* * 打印 nacos client 信息到日志中 * */
    public List<ServiceInstance> getNacosClientinfo(String serviceid) {
        log.info("request naocs client to get service instance info:[{}]", serviceid);
        return discoveryClient.getInstances(serviceid);
    }
@RestController
@Slf4j
@RequestMapping("/nacos-client")
public class NacosClinetController {
    private final NacosClientService nacosClientService;

    public NacosClinetController(NacosClientService nacosClientService) {
        this.nacosClientService = nacosClientService;
    }

    @GetMapping(value = "/service-instance")
    public List<ServiceInstance> logNacosClientinfo(@RequestParam(defaultValue = "e-commerce-nacos-client") String serviceid) {
        log.info("coming in log nacos client info :[{}]", serviceid);
        return nacosClientService.getNacosClientinfo(serviceid);
    }
}

这个时候我们用idea的访问脚本来看返回结果

编写脚本nacos-client.http

### 查询服务示例信息
http://localhost:8000/ecommerce-nacos-client/nacos-client/service-instance
Accept: application/json

查看返回结果

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 03 Dec 2021 11:49:31 GMT
Keep-Alive: timeout=60
Connection: keep-alive

{
  "code": 0,
  "message": "",
  "data": [
    {
      "serviceId": "e-commerce-nacos-client",
      "host": "192.xxx.x.x",
      "port": 8000,
      "secure": false,
      "metadata": {
        "nacos.instanceId": "192.xxx.x.x#8000#DEFAULT#DEFAULT_GROUP@@e-commerce-nacos-client",
        "nacos.weight": "1.0",
        "nacos.cluster": "DEFAULT",
        "nacos.ephemeral": "true",
        "nacos.healthy": "true",
        "preserved.register.source": "SPRING_CLOUD"
      },
      "uri": "http://192.xxx.x.x:8000",
      "scheme": null,
      "instanceId": null
    }
  ]
}

相关文章