【微服务~Nacos】Nacos服务提供者和服务消费者

简介: 【微服务~Nacos】Nacos服务提供者和服务消费者

搭建父项目

  • 项目名:nacos-parent-2.1
  • 添加坐标
 <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-build</artifactId>
        <version>2.3.5.RELEASE</version>
    </parent>
    <properties>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.7-SNAPSHOT</spring.cloud.alibaba.version>
        <mybatis.plus.starter.version>3.4.0</mybatis.plus.starter.version>
        <durid.starter.version>1.1.10</durid.starter.version>
        <swagger.version>2.7.0</swagger.version>
        <jwt.jjwt.version>0.9.0</jwt.jjwt.version>
        <jwt.joda.version>2.9.7</jwt.joda.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- Spring Dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>2.2.7.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- mybatis-plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis.plus.starter.version}</version>
            </dependency>
            <!-- Druid连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${durid.starter.version}</version>
            </dependency>
            <!--swagger2-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 项目目录结构

image.png

服务提供者Provider

搭建服务

  • 创建项目:nacos-provider-2.1
  • 添加依赖:
<dependencies>
        <!-- web 启动类 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- nacos 服务发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
  • 创建yml文件
#server.port=8070
#spring.application.name=service-provider
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#端口号
server:
  port: 8070
spring:
  application:
    name: service-provider          #服务名
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848   #nacos服务地址

创建服务

  • 启动类

image.png

package com.czxy.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient      //服务发现
public class TestNacosProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestNacosProviderApplication.class, args );
    }
}
  • 处理类controller

image.png

package com.czxy.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
public class EchoController {
    @Resource
    private HttpServletRequest request;
    @GetMapping("/echo/{string}")
    public String echo(@PathVariable String string) {
        int serverPort = request.getServerPort();
        return "Hello Nacos Discovery " + string + ":" + serverPort;
    }
}

查看服务

  • 通过浏览器访问

http://localhost:8070/echo/abc

image.png

  • 通过Nacos控制台查看  

image.png

注册异常

image.png

服务消费者Consumer

搭建服务

  • 项目名:nacos-consumer-2.1
  • 添加依赖
<dependencies>
        <!-- web 启动类 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- nacos 服务发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- openfeign 远程调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
  • 创建配置文件

image.png

#server.port=8071
#spring.application.name=service-consumer
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#端口号
server:
  port: 8071
spring:
  application:
    name: service-consumer          #服务名
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848   #nacos服务地址

创建服务

  • 创建启动类

image.png

package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient  //服务发现
public class TestNacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestNacosConsumerApplication.class, args );
    }
}
  • 远程调用配置类

image.png

package com.czxy.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class RestTemplateConfig {
    @LoadBalanced   //负载均衡
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  • 处理类
package com.czxy.nacos.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class TestRestController {
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) {
        return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
    }
}

查询服务

  • 通过浏览器访问

http://localhost:8071/echo/abc

image.png

通过Nacos控制台查看

image.png

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
5月前
|
人工智能 安全 Nacos
Nacos 3.0:微服务与AI融合的技术新纪元
Nacos 3.0:微服务与AI融合的技术新纪元
330 83
|
5月前
|
人工智能 安全 Java
Nacos 3.0:从微服务治理到AI服务治理的跃迁
Nacos 3.0:从微服务治理到AI服务治理的跃迁
354 5
|
5月前
|
人工智能 自然语言处理 安全
Nacos 3.0:微服务与AI融合的新一代动态治理平台
Nacos 3.0:微服务与AI融合的新一代动态治理平台
343 2
|
5月前
|
人工智能 自然语言处理 Nacos
Nacos 3.0:微服务与AI融合的全新治理平台
Nacos 3.0:微服务与AI融合的全新治理平台
262 46
|
弹性计算 API 持续交付
后端服务架构的微服务化转型
本文旨在探讨后端服务从单体架构向微服务架构转型的过程,分析微服务架构的优势和面临的挑战。文章首先介绍单体架构的局限性,然后详细阐述微服务架构的核心概念及其在现代软件开发中的应用。通过对比两种架构,指出微服务化转型的必要性和实施策略。最后,讨论了微服务架构实施过程中可能遇到的问题及解决方案。
|
12月前
|
存储 网络协议 Nacos
高效搭建Nacos:实现微服务的服务注册与配置中心
Nacos(Dynamic Naming and Configuration Service)是阿里巴巴开源的一款动态服务发现、配置管理和服务管理平台。它旨在帮助开发者更轻松地构建、部署和管理分布式系统,特别是在微服务架构中。
1937 82
高效搭建Nacos:实现微服务的服务注册与配置中心
|
弹性计算 Kubernetes API
构建高效后端服务:微服务架构的深度剖析与实践####
本文深入探讨了微服务架构的核心理念、设计原则及实现策略,旨在为开发者提供一套系统化的方法论,助力其构建灵活、可扩展且易于维护的后端服务体系。通过案例分析与实战经验分享,揭示了微服务在提升开发效率、优化资源利用及增强系统稳定性方面的关键作用。文章首先概述了微服务架构的基本概念,随后详细阐述了其在后端开发中的应用优势与面临的挑战,最后结合具体实例,展示了如何从零开始规划并实施一个基于微服务的后端项目。 ####
|
NoSQL 前端开发 测试技术
👀探秘微服务:从零开启网关 SSO 服务搭建之旅
单点登录(Single Sign-On,简称SSO)是一种认证机制,它允许用户只需一次登录就可以访问多个应用程序或系统。本文结合网关和SaToken快速搭建可用的Session管理服务。
927 8

热门文章

最新文章