nacos 注册中心,快速上手

Nacos 注册中心

Nacos ,spring cloud alibaba nacos 中的部分呢

推荐使用doker 安装

这里使用 聚会工程实现,来快速上手

项目搭建

pom 文件

父工程

<groupId>org.example</groupId>
    <artifactId>springcloud-nacos</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <description>nacos作为注册中心</description>

    <modules>
        <module>my-goods</module>
        <module>my-order</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
<!--        <version>2.2.3.RELEASE</version>-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        <com-alibaba-cloud.version>2.2.0.RELEASE</com-alibaba-cloud.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-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${com-alibaba-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

消费者

 <modelVersion>4.0.0</modelVersion>

    <artifactId>my-goods</artifactId>
    <name>my-goods</name>
    <description>商品</description>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>springcloud-nacos</artifactId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

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

生产者

   <modelVersion>4.0.0</modelVersion>

    <artifactId>my-order</artifactId>
    <name>my-order</name>
    <description>订单服务</description>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>springcloud-nacos</artifactId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

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

goods 代码

提供调用服务,简单的字符串传输

server:
  port: 8081
spring:
  application:
    name: my-goods
  cloud:
    nacos:
      server-addr: .....:8848
@RestController
public class GoodsController {

    @GetMapping("goods/{message}")
    public String greet(@PathVariable String message){
        return "goods 被访问 :"+ message;
    }
}

order 代码

服务调用方

server:
  port: 8080
spring:
  application:
    name: my-order
  cloud:
    nacos:
      server-addr: ...:8848
@RestController
public class HelloController {

    /*
    spring-cloud-alibaba-nacos-discovery 内置了Ribbon,可以直接注入 LoadBalancerClient
     */
    @Autowired
    private LoadBalancerClient loadBalancerClient;

  	// 在配置类中注入,或者自己new,配置类比较简单,就不贴出来了
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("hello")
    public String hello(){
        return "测试";
    }

    @GetMapping("hello/{message}")
    public String hello(@PathVariable String message) {
        //获取注册中心的 服务端信息
        ServiceInstance serviceInstance = loadBalancerClient.choose("my-goods");
        String path = String.format("http://%s:%s/goods/%s", serviceInstance.getHost(), serviceInstance.getPort(), message);
        //发起请求
        String result = restTemplate.getForObject(path, String.class);
        return String.format("%s from %s %s", result, serviceInstance.getHost(), serviceInstance.getPort());
    }
}

测试效果

image-20220507133551300

Nacos默认的 负载均衡的策略是轮询,这里做个测试

查看是否注册成功

image-20220507133654578

访问路径
http://localhost:8080/hello/nacos

image-20220507133833151

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值