• JavaEE:Spring Cloud Alibaba-Nacos与Feign使用


    Nacos服务器安装(单):

    JavaEE:CentOS 7中安装Nacos_無_爲的博客-CSDN博客

    一、添加Spring Cloud与Spring Cloud Alibaba依赖:

    1.在工程根目录pom.xml中:

    1. <project ...>
    2.     ...
    3.     <properties>
    4.         <spring-cloud-alibaba.version>2021.0.1.0spring-cloud-alibaba.version>
    5.         <spring-cloud.version>2021.0.3spring-cloud.version>  
    6.     properties>
    7.     <dependencies>
    8.         <dependency>
    9.             <groupId>org.springframework.bootgroupId>
    10.             <artifactId>spring-boot-starter-webartifactId>
    11.         dependency>
    12.         <dependency>
    13.             <groupId>org.springframework.cloudgroupId>
    14.             <artifactId>spring-cloud-starter-loadbalancerartifactId>
    15.         dependency>
    16.     dependencies>
    17.     <dependencyManagement>
    18.         <dependencies>
    19.            
    20.             <dependency>
    21.                 <groupId>org.springframework.cloudgroupId>
    22.                 <artifactId>spring-cloud-dependenciesartifactId>
    23.                 <version>${spring-cloud.version}version>
    24.                 <type>pomtype>
    25.                 <scope>importscope>
    26.             dependency>
    27.            
    28.             <dependency>
    29.                 <groupId>com.alibaba.cloudgroupId>
    30.                 <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    31.                 <version>${spring-cloud-alibaba.version}version>
    32.                 <type>pomtype>
    33.                 <scope>importscope>
    34.             dependency>
    35.         dependencies>
    36.     dependencyManagement>
    37. project>

    二、微服务Module发布服务:

    1.添加Nacos依赖,在模块工程pom.xml中:

    1. <dependencies>
    2.     ...
    3.    
    4.     <dependency>
    5.         <groupId>com.alibaba.cloudgroupId>
    6.         <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    7.     dependency>
    8. dependencies>

    2.注册服务,在application.yml中:

    1. ...
    2. spring:
    3.   ...
    4.   application:
    5.     name: nacos  #配置微服务的名称
    6.   cloud:
    7.     nacos:
    8.       discovery:
    9.         server-addr: http://192.168.83.129:8848  #Nacos服务器地址
    10. management:
    11.   endpoints:
    12.     web:
    13.       exposure:
    14.         include: '*'

    3.启动类中加@EnableDiscoveryClient:

    1. @EnableDiscoveryClient
    2. @SpringBootApplication
    3. public class NacosApplication {
    4.     public static void main(String[] args) {
    5.         SpringApplication.run(NacosApplication.class, args);
    6.     }
    7. }

    4.提供服务接口:

    1. @Controller
    2. @RequestMapping
    3. public class UserController {
    4.     @RequestMapping(value = "/userList")
    5.     @ResponseBody
    6.     public String userList() {
    7.         ...
    8.     }
    9. }

    三、Web应用Module调用服务:

    1.添加Feign与Nacos依赖,在模块工程pom.xml中:

    1. <dependencies>
    2.     ...
    3.    
    4.     <dependency>
    5.         <groupId>org.springframework.cloudgroupId>
    6.         <artifactId>spring-cloud-starter-openfeignartifactId>
    7.     dependency>
    8.    
    9.     <dependency>
    10.         <groupId>com.alibaba.cloudgroupId>
    11.         <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    12.     dependency>
    13. dependencies>

    2.发现服务,在application.yml中:

    1. ...
    2. spring:
    3.   ...
    4.   cloud:
    5.     nacos:
    6.       discovery:
    7.         server-addr: http://192.168.83.129:8848  #Nacos服务器地址

    3.启动类中加@EnableFeignClients与@EnableDiscoveryClient:

    1. @EnableFeignClients
    2. @EnableDiscoveryClient
    3. @SpringBootApplication
    4. public class FeignApplication {
    5.     public static void main(String[] args) {
    6.         SpringApplication.run(FeignApplication.class, args);
    7.     }
    8. }

    4.调用服务接口:

    (1)定义Feign调用接口:

    1. @FeignClient(name = "nacos"/*, fallback = */) //被调用的微服务名,fallback可配置降级处理类
    2. public interface UserServiceClient {
    3.     @RequestMapping(value = "/userList")
    4.     String userList();
    5. }

    (2)测试例子:

    1. @Controller
    2. @RequestMapping
    3. public class UserApi {
    4.     @Autowired
    5.     private UserServiceClient userServiceClient;
    6.     @RequestMapping(value = "/getUserList")
    7.     @ResponseBody
    8.     public String getUserList() {
    9.         System.out.println("Web应用被调用");
    10.         return userServiceClient.userList();
    11.     }
    12. }

    5.查看服务列表:

     

  • 相关阅读:
    “阿里爸爸”最新 Java 面试指南,基础 + 框架 + 数据库 + 系统设计 + 算法
    听GPT 讲Istio源代码--cni
    当用get请求,接到前端传的字符串数组列表的时候,用split 分割的方式
    Leetcode105. 从前序与中序遍历序列构造二叉树
    19. Remove Nth Node From End of List
    408考研科目《数据结构》第三章第三节:特殊矩阵的压缩存储
    013. N 皇后
    ElasticSearch Client体验
    SpringBoot整合Junit
    蓝桥杯物联网竞赛_STM32L071_19_输出方波信号(PWM)
  • 原文地址:https://blog.csdn.net/a526001650a/article/details/126018660