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


    🔎这里是【微服务~Nacos】,关注我学习云原生不迷路
    👍如果对你有帮助,给博主一个免费的点赞以示鼓励
    欢迎各位🔎点赞👍评论收藏⭐️

    👀专栏介绍

    【微服务~Nacos】 目前主要更新微服务,一起学习一起进步。

    👀本期介绍

    本期主要介绍微服务~Nacos

    文章目录

    搭建父项目

    服务提供者Provider

    搭建服务

    创建服务

    查看服务

    注册异常

    服务消费者Consumer

    搭建服务

    创建服务

    查询服务

    搭建父项目

    • 项目名:nacos-parent-2.1

    • 添加坐标

    1. <parent>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-build</artifactId>
    4. <version>2.3.5.RELEASE</version>
    5. </parent>
    6. <properties>
    7. <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
    8. <spring.cloud.alibaba.version>2.2.7-SNAPSHOT</spring.cloud.alibaba.version>
    9. <mybatis.plus.starter.version>3.4.0</mybatis.plus.starter.version>
    10. <durid.starter.version>1.1.10</durid.starter.version>
    11. <swagger.version>2.7.0</swagger.version>
    12. <jwt.jjwt.version>0.9.0</jwt.jjwt.version>
    13. <jwt.joda.version>2.9.7</jwt.joda.version>
    14. </properties>
    15. <dependencyManagement>
    16. <dependencies>
    17. <!-- Spring Dependencies -->
    18. <dependency>
    19. <groupId>org.springframework.boot</groupId>
    20. <artifactId>spring-boot-dependencies</artifactId>
    21. <version>${spring-boot.version}</version>
    22. <type>pom</type>
    23. <scope>import</scope>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework.cloud</groupId>
    27. <artifactId>spring-cloud-dependencies</artifactId>
    28. <version>${spring.cloud.version}</version>
    29. <type>pom</type>
    30. <scope>import</scope>
    31. </dependency>
    32. <dependency>
    33. <groupId>com.alibaba.cloud</groupId>
    34. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    35. <version>2.2.7.RELEASE</version>
    36. <type>pom</type>
    37. <scope>import</scope>
    38. </dependency>
    39. <!-- mybatis-plus -->
    40. <dependency>
    41. <groupId>com.baomidou</groupId>
    42. <artifactId>mybatis-plus-boot-starter</artifactId>
    43. <version>${mybatis.plus.starter.version}</version>
    44. </dependency>
    45. <!-- Druid连接池 -->
    46. <dependency>
    47. <groupId>com.alibaba</groupId>
    48. <artifactId>druid-spring-boot-starter</artifactId>
    49. <version>${durid.starter.version}</version>
    50. </dependency>
    51. <!--swagger2-->
    52. <dependency>
    53. <groupId>io.springfox</groupId>
    54. <artifactId>springfox-swagger2</artifactId>
    55. <version>${swagger.version}</version>
    56. </dependency>
    57. <dependency>
    58. <groupId>io.springfox</groupId>
    59. <artifactId>springfox-swagger-ui</artifactId>
    60. <version>${swagger.version}</version>
    61. </dependency>
    62. </dependencies>
    63. </dependencyManagement>
    64. <build>
    65. <plugins>
    66. <plugin>
    67. <groupId>org.apache.maven.plugins</groupId>
    68. <artifactId>maven-javadoc-plugin</artifactId>
    69. <configuration>
    70. <skip>true</skip>
    71. </configuration>
    72. </plugin>
    73. </plugins>
    74. </build>
    • 项目目录结构

    服务提供者Provider

    搭建服务

    • 创建项目:nacos-provider-2.1

    • 添加依赖:

    1. <dependencies>
    2. <!-- web 启动类 -->
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-starter-web</artifactId>
    6. </dependency>
    7. <!-- nacos 服务发现 -->
    8. <dependency>
    9. <groupId>com.alibaba.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    11. </dependency>
    12. </dependencies>
    • 创建yml文件
    1. #server.port=8070
    2. #spring.application.name=service-provider
    3. #spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    4. #端口号
    5. server:
    6. port: 8070
    7. spring:
    8. application:
    9. name: service-provider #服务名
    10. cloud:
    11. nacos:
    12. discovery:
    13. server-addr: 127.0.0.1:8848 #nacos服务地址

    创建服务

    • 启动类

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

    1. package com.czxy.controller;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import javax.annotation.Resource;
    6. import javax.servlet.http.HttpServletRequest;
    7. @RestController
    8. public class EchoController {
    9. @Resource
    10. private HttpServletRequest request;
    11. @GetMapping("/echo/{string}")
    12. public String echo(@PathVariable String string) {
    13. int serverPort = request.getServerPort();
    14. return "Hello Nacos Discovery " + string + ":" + serverPort;
    15. }
    16. }

    查看服务

    • 通过浏览器访问

    http://localhost:8070/echo/abc

    • 通过Nacos控制台查看  

    注册异常

    服务消费者Consumer

    搭建服务

    • 项目名:nacos-consumer-2.1

    • 添加依赖

    1. <dependencies>
    2. <!-- web 启动类 -->
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-starter-web</artifactId>
    6. </dependency>
    7. <!-- nacos 服务发现 -->
    8. <dependency>
    9. <groupId>com.alibaba.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    11. </dependency>
    12. <!-- openfeign 远程调用 -->
    13. <dependency>
    14. <groupId>org.springframework.cloud</groupId>
    15. <artifactId>spring-cloud-starter-openfeign</artifactId>
    16. </dependency>
    17. </dependencies>
    • 创建配置文件

    1. #server.port=8071
    2. #spring.application.name=service-consumer
    3. #spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    4. #端口号
    5. server:
    6. port: 8071
    7. spring:
    8. application:
    9. name: service-consumer #服务名
    10. cloud:
    11. nacos:
    12. discovery:
    13. server-addr: 127.0.0.1:8848 #nacos服务地址

    创建服务

    • 创建启动类

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

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

    查询服务

    • 通过浏览器访问

    http://localhost:8071/echo/abc

    通过Nacos控制台查看

     

  • 相关阅读:
    Maven与IDEA版本兼容问题以及配置
    某些之前的漏洞的遗忘的记录
    牛客网刷题(三)
    C++面向对象——类与对象
    目标检测指标AP50/准确率/召回率说明
    net-java-php-python-小学随班就读管理系统设计计算机毕业设计程序
    Mybatis获取自增主键
    C语言2:说心里话
    Gateway Timeout504: 网关超时的完美解决方法
    Java函数式编程(1):Lambda表达式(2)
  • 原文地址:https://blog.csdn.net/weixin_45481821/article/details/125320833