SpringCloud Alibaba | Nacos(二):Nacos作为注册中心

1. 前言

在这里插入图片描述
在SpringCloud 项目中我们需要 一个服务发布者 一个服务消费者

创建两个springboot项目

  • provider 服务发布者
  • consumer 服务消费者

demo项目git地址: https://gitee.com/yijiecaomin/nacos-demo

2. 准备demo环境

  • Windos 10
  • JDK8
  • 已经安装的Naocs
  • idea

3. 开始

3.1 创建聚合父工程

在idea 中创建聚合工程 nacos-demo
pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>nacos-demo</artifactId>
    <version>1.0.0</version>
    <name>nacos-demo</name>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <!--spring boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2 创建provider 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>nacos-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties
不要想为什么不是yml配置 , 我不喜欢yml的格式。

server.port=8091
spring.application.name=provider
###nacos 配置
## 名称空间
spring.cloud.nacos.discovery.namespace=evone
## nacos 链接账号   没有设置不可以不选
spring.cloud.nacos.username=evone
# nacos 链接 密码   没有设置不可以不选
spring.cloud.nacos.password=evone123456
# spring.cloud.nacos.server-addr=192.168.104.110:8848  使用的nacos改了端口号,   默认端口 8848
spring.cloud.nacos.server-addr=192.168.104.110:16848

启动类

package com.example.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient// 开启nacos 服务发现
public class ProviderApplication {

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

}

创建DemoServiceImpl

package com.example.provider.service;

import org.springframework.stereotype.Service;

@Service("demoService")
public class DemoServiceImpl {
    public String test(String id) {
        return "111" + id;
    }
}

创建DemoController

package com.example.provider.controller;

import com.example.provider.service.DemoServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource(name = "demoService")
    private DemoServiceImpl demoService;

    @GetMapping("/test/{id}")
    public String test(@PathVariable String id) {
        return demoService.test(id);
    }
}

3.3 创建consumer 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>nacos-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example.consumer</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

** 配置文件 application.properties **

server.port=8010
spring.application.name=consumer

###nacos 配置
## 名称空间
spring.cloud.nacos.discovery.namespace=evone
## nacos 链接账号   没有设置不可以不选
spring.cloud.nacos.username=evone
# nacos 链接 密码   没有设置不可以不选
spring.cloud.nacos.password=evone123456
# spring.cloud.nacos.server-addr=192.168.104.110:8848  使用的nacos改了端口号,   默认端口 8848
spring.cloud.nacos.server-addr=192.168.104.110:16848

###ribbon 配置
#请求处理的超时时间
ribbon.ReadTimeout=6000
#请求连接的超时时间
ribbon.ConnectTimeout=4000
#对当前实例的重试次数
ribbon.MaxAutoRetries=2
#切换实例的重试的次数  1 就切换一次实例   2 切换两次(如果只有两个服务就会切换会刚刚请求的那个服务) 一次类推
ribbon.MaxAutoRetriesNextServer=1

###hystrix 配置
# 开启 hystrix
feign.hystrix.enabled=true
# hystrix 超时时间 一般 hystrix超时时间 = ((1 + MaxAutoRetries) +(MaxAutoRetriesNextServer+1)) * ReadTimeout
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=9000
#设置所有实例的默认值
hystrix.command.default.execution.timeout.enabled=true

启动类 ConsumerApplication

package com.example.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient// 开启nacos 服务发现
public class ConsumerApplication {

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

}

**接口类 ConsumerDemoController **

package com.example.consumer.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("/consumer")
public class ConsumerDemoController {

    @Resource(name="restTemplate")
    private RestTemplate restTemplate;

    @GetMapping("/test/{id}")
    public Object test(@PathVariable String id) {

        String forObject = restTemplate.getForObject("http://procider/demo/test/111", String.class);

        return forObject;
    }
}

**Ribbon 配置 AppConfig **

package com.example.consumer.config.ribbon;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * springcloud ribbon 负载均衡 配置
 *
 */
@SpringBootConfiguration
public class AppConfig {

    @Bean
    @LoadBalanced//负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

  /*  @Bean //负载 自定义机制
    public IRule getRule() {
        return new CunsumerRule();
    }*/

    @Bean //随机机制   默认轮询
    public IRule getRule() {
        return new RandomRule();
    }
}

4. 测试

启动 provider 与 consumer 项目
启动成功后 就可以看到Nacos管理平台中服务列表 就能看到
在这里插入图片描述
调用测试地址 http://127.0.0.1:8010/consumer/test/1
在这里插入图片描述
测试成功 接下来 Nacos集成OpenFegin

总结&注意事项

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

nacos-discovery中 集成了Ribbon

spring-cloud-alibaba-dependencies为什么使用 com.alibaba.cloud 而不是 org.springframework.cloud?
org.springframework.cloud: spring-cloud-alibaba-dependencies管理的nacos最新版本是0.9.0.RELEASE,貌似不再维护了,用起来有版本问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一介草民丶

谢谢老板的一分钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值