java服务注册实现_使用 Spring Cloud Alibaba Nacos 实现服务注册与发现

什么是 Nacos

Nacos 是 Spring Cloud Alibaba 生态中的重要组件之一,用于发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,可以快速实现 动态服务发现、服务配置、服务元数据及流量管理。

下载安装

# 下载源码

git clone https://github.com/alibaba/nacos.git

# 安装到本地仓库

cd nacos/

mvn -Prelease-nacos clean install -U

启动服务

cd distribution/target/nacos-server-0.7.0/nacos/bin

# Linux

./startup.sh -m standalone

# Windows

startup.cmd

登录

账号:nacos

密码:nacos

c7cad5d62bc113ee1f48a8ab9cedfa41.png

Nacos 控制台

bbf8ac8a72b5c408737f9d3deedbfa04.png

服务提供者

引入依赖

在 pom.xml 中主要添加依赖 spring-cloud-starter-alibaba-nacos-discovery

org.springframework.cloud

spring-cloud-starter-alibaba-nacos-discovery

相关配置

在 application.yml 中主要配置 Nacos 的地址

spring:

application:

# 服务名

name: service-provider

cloud:

nacos:

discovery:

# 服务注册中心

server-addr: 192.168.127.132:8848

在 Application 入口类中主要添加注解 @EnableDiscoveryClient 开启服务注册发现功能

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication

@EnableDiscoveryClient

public class ProviderApplication {

public static void main(String[] args) {

SpringApplication.run(ProviderApplication.class, args);

}

}

Controller

创建一个 RestController 提供 RestFul API 用于测试提供服务

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.GetMapping;

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

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

@RestController

public class EchoController {

@GetMapping(value = "/echo/{keyword}")

public String echo(@PathVariable(value = "keyword") String keyword) {

return "Hello Nacos Provider " + keyword + ";

}

}

验证是否成功

8b8b7bc9335f9d9b221f57441caf4385.png提供者服务已上线

会发现有一个服务已经注册在 Nacos 中,服务名为 service-provider

Hello Nacos Provider hi

服务消费者

引入依赖

在 pom.xml 中主要添加 spring-cloud-starter-alibaba-nacos-discovery 和 spring-cloud-starter-openfeign 依赖

org.springframework.cloud

spring-cloud-starter-alibaba-nacos-discovery

org.springframework.cloud

spring-cloud-starter-openfeign

相关配置

在 Application 入口类中同样添加注解 @EnableDiscoveryClient 开启服务注册与发现功能,添加注解 @EnableFeignClients 开启 Feign 客户端

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

public class ConsumerApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerApplication.class, args);

}

}

Service

创建业务层接口,通过 @FeignClient("服务名") 注解来指定具体调用哪个服务

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;

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

@FeignClient(value = "service-provider")

public interface EchoService {

@GetMapping(value = "/echo/{keyword}")

String echo(@PathVariable(value = "keyword") String keyword);

}

Controller

创建 Controller 调用服务

import com.funtl.spring.cloud.alibaba.consumer.service.EchoService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

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

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

@RestController

public class TestEchoController {

@Autowired

private EchoService echoService;

@GetMapping(value = "/feign/echo/{keyword}")

public String echoFeign(@PathVariable(value = "keyword") String keyword) {

return echoService.echo(keyword);

}

}

验证是否成功

dab29082d14101f30cc5f3ca18498481.png

会发现有一个名为 service-consumer 的服务已经注册到 Nacos 中

Hello Nacos Provider hi

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值