IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

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

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

什么是 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

启动完成后,访问 http://192.168.127.132:8848/nacos

登录

账号:nacos

密码:nacos

127_1.png

Nacos 控制台

127_2.png

服务提供者

引入依赖

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

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

相关配置

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 + ";
    }
}

验证是否成功

启动项目,浏览器访问 http://192.168.127.132:8848/nacos

127_3.png提供者服务已上线

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

此时访问网址 http://localhost:8848/echo/hi,浏览器会响应以下内容:

Hello Nacos Provider hi

服务消费者

引入依赖

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

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

相关配置

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);
    }
}

验证是否成功

启动项目,访问网址 http://localhost:8080/feign/echo/hi

127_4.png

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

此时再访问 http://localhost:8080/echo/hi,浏览器会同样响应以下内容:

Hello Nacos Provider hi

文章永久链接:https://tech.souyunku.com/?p=23943


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(77) 打赏



未经允许不得转载:搜云库技术团队 » 使用 Spring Cloud Alibaba Nacos 实现服务注册与发现

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367