• 31.nacos集成Feign和Gateway实例(springcloud)


    一、项目nacos-client-a

     

    1.pom.xml文件

    新增了springcloud的依赖

    新增了springcloud的依赖管理

     新增了feign依赖

     

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.itgroupId>
    6. <artifactId>nacos-client-aartifactId>
    7. <version>0.0.1-SNAPSHOTversion>
    8. <name>nacos-client-aname>
    9. <description>Demo project for Spring Bootdescription>
    10. <properties>
    11. <java.version>1.8java.version>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    14. <spring-boot.version>2.3.12.RELEASEspring-boot.version>
    15. <spring-cloud.version>Hoxton.SR12spring-cloud.version>
    16. <spring-cloud-alibaba.version>2.2.7.RELEASEspring-cloud-alibaba.version>
    17. properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>org.springframework.bootgroupId>
    21. <artifactId>spring-boot-starter-webartifactId>
    22. dependency>
    23. <dependency>
    24. <groupId>com.alibaba.cloudgroupId>
    25. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    26. dependency>
    27. <dependency>
    28. <groupId>org.springframework.cloudgroupId>
    29. <artifactId>spring-cloud-starter-openfeignartifactId>
    30. dependency>
    31. <dependency>
    32. <groupId>org.springframework.bootgroupId>
    33. <artifactId>spring-boot-starter-testartifactId>
    34. <scope>testscope>
    35. <exclusions>
    36. <exclusion>
    37. <groupId>org.junit.vintagegroupId>
    38. <artifactId>junit-vintage-engineartifactId>
    39. exclusion>
    40. exclusions>
    41. dependency>
    42. dependencies>
    43. <dependencyManagement>
    44. <dependencies>
    45. <dependency>
    46. <groupId>org.springframework.bootgroupId>
    47. <artifactId>spring-boot-dependenciesartifactId>
    48. <version>${spring-boot.version}version>
    49. <type>pomtype>
    50. <scope>importscope>
    51. dependency>
    52. <dependency>
    53. <groupId>com.alibaba.cloudgroupId>
    54. <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    55. <version>${spring-cloud-alibaba.version}version>
    56. <type>pomtype>
    57. <scope>importscope>
    58. dependency>
    59. <dependency>
    60. <groupId>org.springframework.cloudgroupId>
    61. <artifactId>spring-cloud-dependenciesartifactId>
    62. <version>${spring-cloud.version}version>
    63. <type>pomtype>
    64. <scope>importscope>
    65. dependency>
    66. dependencies>
    67. dependencyManagement>
    68. <build>
    69. <plugins>
    70. <plugin>
    71. <groupId>org.apache.maven.pluginsgroupId>
    72. <artifactId>maven-compiler-pluginartifactId>
    73. <version>3.8.1version>
    74. <configuration>
    75. <source>1.8source>
    76. <target>1.8target>
    77. <encoding>UTF-8encoding>
    78. configuration>
    79. plugin>
    80. <plugin>
    81. <groupId>org.springframework.bootgroupId>
    82. <artifactId>spring-boot-maven-pluginartifactId>
    83. <version>2.3.7.RELEASEversion>
    84. <configuration>
    85. <mainClass>com.it.NacosClientAApplicationmainClass>
    86. configuration>
    87. <executions>
    88. <execution>
    89. <id>repackageid>
    90. <goals>
    91. <goal>repackagegoal>
    92. goals>
    93. execution>
    94. executions>
    95. plugin>
    96. plugins>
    97. build>
    98. project>

    2.application.yml

    3. feign目录下的TestFeign接口

     4.controller目录下的TestController

    1. package com.it.controller;
    2. import com.it.feigin.TestFeign;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.cloud.client.ServiceInstance;
    5. import org.springframework.cloud.client.discovery.DiscoveryClient;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import java.util.List;
    9. @RestController
    10. public class TestController {
    11. @Autowired
    12. public DiscoveryClient discoveryClient;
    13. @Autowired
    14. public TestFeign testFeign;
    15. @GetMapping("test")
    16. public String test(){
    17. List instances = discoveryClient.getInstances("nacos-client-b");
    18. System.out.println(instances);
    19. return testFeign.info();
    20. }
    21. }

    5.主函数启动类

    二、项目nacos-client-b

     

    1.pom.xml文件

    不需要额外添加springcloud的依赖,使用springcloudalibaba即可

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.itgroupId>
    6. <artifactId>nacos-client-bartifactId>
    7. <version>0.0.1-SNAPSHOTversion>
    8. <name>nacos-client-bname>
    9. <description>Demo project for Spring Bootdescription>
    10. <properties>
    11. <java.version>1.8java.version>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    14. <spring-boot.version>2.3.12.RELEASEspring-boot.version>
    15. <spring-cloud-alibaba.version>2.2.7.RELEASEspring-cloud-alibaba.version>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.springframework.bootgroupId>
    20. <artifactId>spring-boot-starter-webartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>com.alibaba.cloudgroupId>
    24. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-testartifactId>
    29. <scope>testscope>
    30. <exclusions>
    31. <exclusion>
    32. <groupId>org.junit.vintagegroupId>
    33. <artifactId>junit-vintage-engineartifactId>
    34. exclusion>
    35. exclusions>
    36. dependency>
    37. dependencies>
    38. <dependencyManagement>
    39. <dependencies>
    40. <dependency>
    41. <groupId>org.springframework.bootgroupId>
    42. <artifactId>spring-boot-dependenciesartifactId>
    43. <version>${spring-boot.version}version>
    44. <type>pomtype>
    45. <scope>importscope>
    46. dependency>
    47. <dependency>
    48. <groupId>com.alibaba.cloudgroupId>
    49. <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    50. <version>${spring-cloud-alibaba.version}version>
    51. <type>pomtype>
    52. <scope>importscope>
    53. dependency>
    54. dependencies>
    55. dependencyManagement>
    56. <build>
    57. <plugins>
    58. <plugin>
    59. <groupId>org.apache.maven.pluginsgroupId>
    60. <artifactId>maven-compiler-pluginartifactId>
    61. <version>3.8.1version>
    62. <configuration>
    63. <source>1.8source>
    64. <target>1.8target>
    65. <encoding>UTF-8encoding>
    66. configuration>
    67. plugin>
    68. <plugin>
    69. <groupId>org.springframework.bootgroupId>
    70. <artifactId>spring-boot-maven-pluginartifactId>
    71. <version>2.3.7.RELEASEversion>
    72. <configuration>
    73. <mainClass>com.it.NacosClientBApplicationmainClass>
    74. configuration>
    75. <executions>
    76. <execution>
    77. <id>repackageid>
    78. <goals>
    79. <goal>repackagegoal>
    80. goals>
    81. execution>
    82. executions>
    83. plugin>
    84. plugins>
    85. build>
    86. project>

    2.application.yml

    3.BController

     4.主函数启动类

     

    分别启动项目一和项目二进行测试

    只有在同一个命名空间,同一个分组下的项目模块才能互相调用,测试其他情况均不能调用

     

    三、新建gateway模块

     1.pom.xml文件

    新增springcloud的依赖

    2.application.yml

    3.主函数启动类

    同时运行三个项目

    如果不运行动态路由模块,使用localhost/nacos-client-a会导致页面报错,无法正常访问

     同时启动三个项目

    再次访问该路径

     至此,nacos 做注册中心,服务发现,以及远程调用都完成了

  • 相关阅读:
    php接口api数据签名及验签
    java毕业设计企业员工管理系统源码+lw文档+mybatis+系统+mysql数据库+调试
    创建2个线程并执行(STL/Windows/Linux)
    chatgpt的插件功能下架了吗?
    不重启docker进程,重新reload加载配置
    记一次京东前端面试被问到的题目
    IDEA插件开发(11)---插件配置文件
    数据导入与预处理-拓展-pandas时间数据处理01
    JUC并发编程系列详解篇十一(synchronized底层的锁)
    vue项目的学习周报03
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/127949382