• spring cloud nacos服务搭建


          基于nacos搭建的父项目、spring cloud nacos provider(服务提供者)、spring cloud nacos consumer(服务消费者)结构:

    1. 创建一个maven父项目。

            maven父项目包含springcloudnacos-provider(服务提供者)、springcloudnacos-consumer(服务消费者)两个子模块。父模块下的src可以删除。

            maven父项目的pom.xml:

    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. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.2.4.RELEASEversion>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.examplegroupId>
    12. <artifactId>springcloudnacosartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>springcloudnacosname>
    15. <description>Demo project for Spring Bootdescription>
    16. <packaging>pompackaging>
    17. <properties>
    18. <java.version>1.8java.version>
    19. <spring-boot-dependencies.version>2.2.4.RELEASEspring-boot-dependencies.version>
    20. <spring-cloud-dependencies.version>Hoxton.RELEASEspring-cloud-dependencies.version>
    21. <spring-cloud-alibaba.version>2.2.0.RELEASEspring-cloud-alibaba.version>
    22. properties>
    23. <modules>
    24. <module>springcloudnacos-providermodule>
    25. <module>springcloudnacos-consumermodule>
    26. modules>
    27. <dependencies>
    28. <dependency>
    29. <groupId>org.springframework.bootgroupId>
    30. <artifactId>spring-boot-starterartifactId>
    31. dependency>
    32. <dependency>
    33. <groupId>org.springframework.bootgroupId>
    34. <artifactId>spring-boot-starter-testartifactId>
    35. <scope>testscope>
    36. dependency>
    37. dependencies>
    38. <dependencyManagement>
    39. <dependencies>
    40. <dependency>
    41. <groupId>org.springframework.cloudgroupId>
    42. <artifactId>spring-cloud-dependenciesartifactId>
    43. <version>${spring-cloud-dependencies.version}version>
    44. <type>pomtype>
    45. <scope>importscope>
    46. dependency>
    47. <dependency>
    48. <groupId>org.springframework.bootgroupId>
    49. <artifactId>spring-boot-dependenciesartifactId>
    50. <version>${spring-boot-dependencies.version}version>
    51. <type>pomtype>
    52. <scope>importscope>
    53. dependency>
    54. <dependency>
    55. <groupId>com.alibaba.cloudgroupId>
    56. <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    57. <version>${spring-cloud-alibaba.version}version>
    58. <type>pomtype>
    59. <scope>importscope>
    60. dependency>
    61. dependencies>
    62. dependencyManagement>
    63. <build>
    64. <plugins>
    65. <plugin>
    66. <groupId>org.springframework.bootgroupId>
    67. <artifactId>spring-boot-maven-pluginartifactId>
    68. plugin>
    69. <plugin>
    70. <groupId>org.apache.maven.pluginsgroupId>
    71. <artifactId>maven-surefire-pluginartifactId>
    72. <configuration>
    73. <testFailureIgnore>truetestFailureIgnore>
    74. configuration>
    75. plugin>
    76. plugins>
    77. build>
    78. project>

    2. 服务提供者:springcloudnacos-provider

    (1)spring cloud nacos provider项目的pom.xml

    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. <parent>
    6. <groupId>com.examplegroupId>
    7. <artifactId>springcloudnacosartifactId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. parent>
    10. <groupId>com.examplegroupId>
    11. <artifactId>springcloudnacos-providerartifactId>
    12. <version>0.0.1-SNAPSHOTversion>
    13. <name>springcloudnacos-providername>
    14. <description>Demo project for Spring Bootdescription>
    15. <properties>
    16. <java.version>1.8java.version>
    17. properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>org.springframework.bootgroupId>
    21. <artifactId>spring-boot-starterartifactId>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframework.bootgroupId>
    25. <artifactId>spring-boot-starter-webartifactId>
    26. dependency>
    27. <dependency>
    28. <groupId>org.springframework.bootgroupId>
    29. <artifactId>spring-boot-starter-testartifactId>
    30. <scope>testscope>
    31. dependency>
    32. <dependency>
    33. <groupId>com.alibaba.cloudgroupId>
    34. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    35. dependency>
    36. <dependency>
    37. <groupId>com.alibaba.cloudgroupId>
    38. <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    39. dependency>
    40. dependencies>
    41. <build>
    42. <plugins>
    43. <plugin>
    44. <groupId>org.apache.maven.pluginsgroupId>
    45. <artifactId>maven-surefire-pluginartifactId>
    46. <configuration>
    47. <testFailureIgnore>truetestFailureIgnore>
    48. configuration>
    49. plugin>
    50. plugins>
    51. build>
    52. project>

    (2)spring cloud nacos provider服务的启动类

    1. package com.example.springcloudnacosprovider;
    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 SpringcloudnacosProviderApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(SpringcloudnacosProviderApplication.class, args);
    10. }
    11. }

    (3)spring cloud nacos provider服务的yml配置

    1. spring:
    2. cloud:
    3. nacos:
    4. discovery:
    5. server-addr: 127.0.0.1:8848
    6. application:
    7. name: nacos-provider
    8. server:
    9. port: 8090

    (4)spring cloud nacos provider服务的测试类

    1. package com.example.springcloudnacosprovider.controller;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class NacosProviderController {
    6. @RequestMapping("/testNacosProvider")
    7. public String testNacosProvider(){
    8. return "this is my nacos provider!";
    9. }
    10. }

    (5)spring cloud nacos provider服务测试

    (6)spring cloud nacos provider服务在nacos上的注册(nacos服务注册中心

    3. 服务消费者:springcloudnacos-consumer

    (1)spring cloud nacos consumer服务的pom.xml

    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. <parent>
    6. <groupId>com.examplegroupId>
    7. <artifactId>springcloudnacosartifactId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. parent>
    10. <groupId>com.examplegroupId>
    11. <artifactId>springcloudnacos-consumerartifactId>
    12. <version>0.0.1-SNAPSHOTversion>
    13. <name>springcloudnacos-consumername>
    14. <description>Demo project for Spring Bootdescription>
    15. <properties>
    16. <java.version>1.8java.version>
    17. properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>org.springframework.bootgroupId>
    21. <artifactId>spring-boot-starterartifactId>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframework.bootgroupId>
    25. <artifactId>spring-boot-starter-webartifactId>
    26. dependency>
    27. <dependency>
    28. <groupId>org.springframework.bootgroupId>
    29. <artifactId>spring-boot-starter-testartifactId>
    30. <scope>testscope>
    31. dependency>
    32. <dependency>
    33. <groupId>com.alibaba.cloudgroupId>
    34. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    35. dependency>
    36. <dependency>
    37. <groupId>com.alibaba.cloudgroupId>
    38. <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    39. dependency>
    40. <dependency>
    41. <groupId>org.springframework.cloudgroupId>
    42. <artifactId>spring-cloud-loadbalancerartifactId>
    43. dependency>
    44. dependencies>
    45. <build>
    46. <plugins>
    47. <plugin>
    48. <groupId>org.apache.maven.pluginsgroupId>
    49. <artifactId>maven-surefire-pluginartifactId>
    50. <configuration>
    51. <testFailureIgnore>truetestFailureIgnore>
    52. configuration>
    53. plugin>
    54. plugins>
    55. build>
    56. project>

    (2)spring cloud nacos consumer服务的启动类

    1. package com.example.springcloudnacosconsumer;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.web.client.RestTemplate;
    8. @SpringBootApplication
    9. @EnableDiscoveryClient
    10. public class SpringcloudnacosConsumerApplication {
    11. public static void main(String[] args) {
    12. SpringApplication.run(SpringcloudnacosConsumerApplication.class, args);
    13. }
    14. @Bean
    15. @LoadBalanced
    16. public RestTemplate restTemplate(){
    17. return new RestTemplate();
    18. }
    19. }

    (3)spring cloud nacos consumer服务的yml配置

    1. spring:
    2. cloud:
    3. nacos:
    4. discovery:
    5. server-addr: 127.0.0.1:8848
    6. application:
    7. name: nacos-consumer
    8. server:
    9. port: 8091

    (4)spring cloud nacos consumer服务的测试类

    1. package com.example.springcloudnacosconsumer.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import org.springframework.web.client.RestTemplate;
    6. @RestController
    7. public class NacosConsumerController {
    8. private RestTemplate restTemplate;
    9. @Autowired
    10. public NacosConsumerController(RestTemplate restTemplate){
    11. this.restTemplate = restTemplate;
    12. }
    13. @RequestMapping("/testNacosConsumer")
    14. public String testNacosConsumer(){
    15. return restTemplate.getForObject("http://nacos-provider/testNacosProvider",String.class);
    16. }
    17. }

    (5)spring cloud nacos consumer服务测试

    (6)spring cloud nacos consumer服务在nacos服务器上的注册(nacos服务注册中心)

  • 相关阅读:
    51单片机入门_江协科技_27~28_OB记录的自学笔记_AT24C02数据存储&秒表
    【Spring】Spring 事务和事务传播机制
    自动驾驶行业数据防泄漏解决方案分享
    4核16G服务器价格腾讯云PK阿里云
    golang设计模式——解析器模式
    torch.hub.load报错urllib.error.HTTPError: HTTP Error 403: rate limit exceeded
    10月11日
    python实战故障诊断之CWRU数据集(一):数据集初识
    链表-真正的动态数据结构
    艾美捷双链RNA定量试剂盒试验方案
  • 原文地址:https://blog.csdn.net/mudisheng0202/article/details/126457303