Nacos + Springboot 配置 【生产者】 与 【消费者】

本文介绍了如何使用Nacos作为微服务注册中心,通过Spring Cloud实现服务提供者(生产者)与服务消费者(消费者)的交互,包括配置、启动类、依赖管理和实际调用过程。展示了如何在Nacos控制台查看注册的服务实例。

1. 首先启动 nacos 注册中心(windows版本 + 单机模式):

        ****\nacos\distribution\target\nacos-server-2.0.3-SNAPSHOT\nacos\bin

        startup.cmd -m standalone

备注:如果不想通过外部启动,想要通过像是Eureka那样构建Springboot-server应该也可以,通过Git下载Nacos的源码来启动试试吧,这里我就没有尝试了。

 2. 生产者:

(最近看了看,发现生产者应该是将业务构建在【Service】之中才能称之为一个服务)

=>application.properties

server.port=8070
# spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=test-provider
  • spring.cloud.nacos.discovery.server-addr:nacos注册中心访问地址
  • spring.cloud.nacos.discovery.service:服务名称(生产者)

=>pom.xml

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos.version}</version>
        </dependency>

 备注:这里spring-cloud-starter-alibaba-nacos-config 似乎是没有用上,等之后在测试的。。。

=>启动类

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

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

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

}

注意这里要添加注解 @EnableDiscoveryClient

=>Controller

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController01 {
    @RequestMapping("/echo/{string}")
    public String echo(@PathVariable String string){
        return "Hello" + string;
    }
}

没什么好说的,普通的Controller

3. 消费者:

=>application.properties

server.port=8071
# spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=test-consumer

说明同生产者。

备注:这不过有关于这里的服务名称,由于生产者会被消费者所使用,所以生产者的服务名称是必须要有的,而消费者的服务名称又没有被别的微服务使用,会不会不设置也可以呢,但我感觉不设置的话,在启动的时候会报错,回来试试的

=>启动类

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

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

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

}

说明同生产者。

备注:这里生产者和消费者的启动类名称没注意给弄成一样的了,因为麻烦就没改过来。

=>pom.xml

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos.version}</version>
        </dependency>

说明同生产者。

=>配置类(为了使用Ribben的RestTemplate客户端负载均衡)

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

不是重点说Ribben,因此没有说明~~

=>Controller

import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
public class ConsumerController01 {

    private final RestTemplate restTemplate;

    @Autowired
    public ConsumerController01(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }

    @RequestMapping("/echo/{str}")
    public String echo(@PathVariable String str){
        return restTemplate.getForObject("http://test-provider/echo/" + str, String.class);
    }
}

通过restTemplate的getForObject()方法调用生产者提供的服务【http://生产者服务名称(就是在这里使用的)/**】

4. 测试结果:

5. Nacos控制台登录,查看注册的服务:

>>http://localhost:8848/nacos/

默认的用户名与密码为:nacos/nacos

已注册的服务如上图所示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值