• 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.查看服务列表:

     

  • 相关阅读:
    BGP基础
    基于Python的51job的招聘信息可视化分析系统的设计与实现
    js手撕代码
    Go连接Redis集群并实现单例,组件go-redis
    js判断对象是否为空
    【版本控制】Github和Gitlab同时使用ssh
    SSM+广西壮族文化宣传网站 毕业设计-附源码230932
    【华为OD题库-008】座位调整-Java
    java源码系列:HashMap源码验证,自己写的HashMap性能高吗?为什么在JDK8中新增红黑树?
    web网页设计期末课程大作业——香格里拉旅游网页设计 5页 HTML+CSS+JavaScript
  • 原文地址:https://blog.csdn.net/a526001650a/article/details/126018660