关于java:SpringCloud整合Feign调用重构

40次阅读

共计 14169 个字符,预计需要花费 36 分钟才能阅读完成。

目录构造

springcloud-parents 父工程 (pom)
—-springcloud-eureka-server Eureka Service
—-springcloud-parent 父模块 (pom)
——–springcloud-api-service 接口 (pom)
————springcloud-api-member-service 接口 (jar)
————springcloud-api-order-service 接口 (jar)
——–springcloud-api-member-service-impl 会员接口实现 (pom)
——–springcloud-api-order-service-impl 订单接口实现 (pom)

模块代码

springcloud-parents 父工程

构建 spring initializr 工程习惯把开发工具和 web 加进来了哈:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <packaging>pom</packaging>
 <modules> <module>springcloud-eureka-server</module>
 <module>springcloud-parent</module>
 </modules> <parent> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.0.1.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.baba.wlb</groupId>
 <artifactId>springcloud-parents</artifactId>
 <version>1.0-SNAPSHOT</version>
 <name>springcloud-parents</name>
 <description>Demo project for Spring Boot</description>
 <properties> <java.version>1.8</java.version>
 </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
 </dependency> <dependency> <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <optional>true</optional>
 </dependency> <dependency> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency> </dependencies>
 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 </exclude> </excludes> </configuration> </plugin> </plugins> </build>
</project>

springcloud-parent

pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-parents</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-parent</artifactId>
 <packaging>pom</packaging>
 <modules> <module>springcloud-api-service</module>
 <module>springcloud-api-order-service-impl</module>
 <module>springcloud-api-member-service-impl</module>
 </modules>
 <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>Finchley.M7</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency> </dependencies> </dependencyManagement>
 <dependencies> <!--SpringCloud Eureka 客户端 -->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
 <!--springcloud 整合 openfeign-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency> </dependencies>
 <!-- 留神:这里必须增加,否则各种依赖有问题 -->
 <repositories>
 <repository> <id>spring-milestones</id>
 <name>Spring Milestones</name>
 <url>https://repo.spring.io/libs-milestone</url>
 <snapshots> <enabled>false</enabled>
 </snapshots> </repository> </repositories>
</project>

springcloud-api-service

pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-parent</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-api-service</artifactId>
 <packaging>pom</packaging>
 <modules> <module>springcloud-api-member-service</module>
 <module>springcloud-api-order-service</module>
 </modules>
</project>

springcloud-api-member-service

  • pom 依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-api-service</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-api-member-service</artifactId>
 <packaging>jar</packaging>
 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <configuration> <skip>true</skip>
 </configuration> </plugin> </plugins> </build>
</project>
  • IMemberService 接口
package com.baba.api.service;
import com.baba.api.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @Author wulongbo
 * @Date 2021/1/22 11:34
 * @Version 1.0
 */public interface IMemberService {
    // 实体类和定义的接口信息寄存在接口我的项目
 // 代码实现寄存在接口实现类外面
 @RequestMapping("/getMember")
    UserEntity getMember(String name);
}
  • 实体类 UserEntity:
package com.baba.api.entity;
import lombok.Data;
/**
 * @Author wulongbo
 * @Date 2021/1/22 11:41
 * @Version 1.0
 */@Data
public class UserEntity {
    private String name;
 private String password;
}

springcloud-api-order-service

  • pom 依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-api-service</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-api-order-service</artifactId>
 <packaging>jar</packaging>
 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <configuration> <skip>true</skip>
 </configuration> </plugin> </plugins> </build></project>
  • IOrderService 接口
package com.baba.api.service;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @Author wulongbo
 * @Date 2021/1/22 13:46
 * @Version 1.0
 */public interface IOrderService {// 订单服务调用会员服务接口 feign @RequestMapping("/getOrderByMember")
    String getOrderByMember(String name);
}

springcloud-api-member-service-impl

  • pom 依赖:依赖 springcloud-api-member-service 模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-parent</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-api-member-service-impl</artifactId>
 <dependencies> <dependency> <groupId>com.baba.wlb</groupId>
 <artifactId>springcloud-api-member-service</artifactId>
 <version>1.0-SNAPSHOT</version>
 <scope>compile</scope>
 </dependency> </dependencies>
</project>
  • MemberServiceImpl 现类:实现 IMemberService
package com.baba.api.service.impl;
import com.baba.api.entity.UserEntity;
import com.baba.api.service.IMemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Author wulongbo
 * @Date 2021/1/22 11:43
 * @Version 1.0
 */
@RestController
public class MemberServiceImpl implements IMemberService {
    @Override
 @RequestMapping("/getMember")
    public UserEntity getMember(String name) {UserEntity userEntity=new UserEntity();
 userEntity.setName(name);
 userEntity.setPassword("123456");
 return userEntity;
 }
}
  • application.yml 配置文件:做了 Eureka 集群
## 服务端口号
server:
  port: 8000
spring:
  application:
    ## 服务别名 -- 服务注册到 Eureka 名称
 name: app-member
eureka:
  client:
    service-url:
      ## 以后服务注册到 Eureka 服务地址
 defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka
    register-with-eureka: true
 ## 须要检索服务信息
 fetch-registry: true
  • AppMember 启动类:启用 EurekaClient 和 FeignClients
package com.baba.api.service.impl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
 * @Author wulongbo
 * @Date 2021/1/9 15:22
 * @Version 1.0
 */@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class AppMember {public static void main(String[] args) {SpringApplication.run(AppMember.class,args);
 }
}

springcloud-api-order-service-impl

  • pom 依赖:依赖 springcloud-api-order-service 模块 和 springcloud-api-member-service 模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-parent</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-api-order-service-impl</artifactId>
 <dependencies> <dependency> <groupId>com.baba.wlb</groupId>
 <artifactId>springcloud-api-order-service</artifactId>
 <version>1.0-SNAPSHOT</version>
 <scope>compile</scope>
 </dependency>
 <!-- 引入会员接口我的项目 -->
 <dependency>
 <groupId>com.baba.wlb</groupId>
 <artifactId>springcloud-api-member-service</artifactId>
 <version>1.0-SNAPSHOT</version>
 <scope>compile</scope>
 </dependency> </dependencies>
</project>
  • application.yml 配置文件:做了 Eureka 集群
## 服务端口号
server:
  port: 8200
spring:
  application:
    ## 服务别名 -- 服务注册到 Eureka 名称
 name: app-order
eureka:
  client:
    service-url:
      ## 以后服务注册到 Eureka 服务地址
 defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka
    register-with-eureka: true
 ## 须要检索服务信息
 fetch-registry: true
 ## 心跳检测与续约工夫
 ## 测试的时候将值设置小一些,保障服务敞开后注册核心能及时剔除服务
 instance:
    ##Eureka 客户端向服务端发送心跳的工夫距离,单位为秒 (客户端会通知服务端会依照该规定)
 lease-renewal-interval-in-seconds: 1
 ##Eureka 服务端在收到最初一次心跳之后期待的工夫下限,单位为秒,超过则剔除 (客户端会通知服务端会依照该规定)
 lease-expiration-duration-in-seconds: 2
  • Feign Service 调用:继承 IMemberService, 注入 provide 别名
package com.baba.api.feign;
import com.baba.api.service.IMemberService;
import org.springframework.cloud.openfeign.FeignClient;
/**
 * @Author wulongbo
 * @Date 2021/1/22 14:12
 * @Version 1.0
 */@FeignClient(name = "app-member")
public interface MemberServiceFeign extends IMemberService{}
  • OrderServiceImpl 实现类:IOrderService 的实现,注入 Feign
package com.baba.api.service.impl;
import com.baba.api.entity.UserEntity;
import com.baba.api.feign.MemberServiceFeign;
import com.baba.api.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Author wulongbo
 * @Date 2021/1/22 13:51
 * @Version 1.0
 */
@RestController
public class OrderServiceImpl implements IOrderService {
    // 订单服务集成会员服务接口,用来实现 feign 客户端 缩小接口反复代码
 @Autowired
 private MemberServiceFeign memberServiceFeign;
 @Override
 @RequestMapping("/getOrderByMember")
    public String getOrderByMember(String name) {UserEntity userEntity = memberServiceFeign.getMember(name);
 return userEntity == null ? "没有找到用户信息!" : userEntity.toString();}
}
  • AppOrder 启动类:启用 EurekaClient 和 FeignClients
package com.baba.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
 * @Author wulongbo
 * @Date 2021/1/9 15:39
 * @Version 1.0
 */@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class AppOrder {public static void main(String[] args) {SpringApplication.run(AppOrder.class,args);
 }
}

springcloud-eureka-server

Eureka Server 服务

  • pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent> <artifactId>springcloud-parents</artifactId>
 <groupId>com.baba.wlb</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent> <modelVersion>4.0.0</modelVersion>
 <artifactId>springcloud-eureka-server</artifactId>
 <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>Finchley.M7</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency> </dependencies> </dependencyManagement>
 <dependencies> <!--SpringCloud Eureka Server-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency> </dependencies>
 <!-- 留神:这里必须增加,否则各种依赖有问题 -->
 <repositories>
 <repository> <id>spring-milestones</id>
 <name>Spring Milestones</name>
 <url>https://repo.spring.io/libs-milestone</url>
 <snapshots> <enabled>false</enabled>
 </snapshots> </repository> </repositories></project>
  • application.yml 务配置文件:
## 服务端口号
server:
  port: 8100
spring:
  application:
    ##Eureka 集群应用,名称必须统一
 name: baba-eureka
eureka:
  instance:
    ## 服务注册核心 ip 地址
 hostname: 127.0.0.1
  client:
    serviceUrl:
      ## 注册地址
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
 defaultZone: http://${eureka.instance.hostname}:9100/eureka/
    ## 因为本人是注册核心,是否须要本人将本人注册到注册核心 (集群的时候为 true)
 register-with-eureka: true
 ## 因为本人是注册核心,不须要去检索服务信息
 fetch-registry: true
 server:
    ## 测试时敞开自我爱护机制,保障不可用服务及时剔除
 enable-self-preservation: false
 eviction-interval-timer-in-ms: 2000

注:另外一台集群配置省略

  • Eureka 服务启动类:
package wlb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    //@EnableEurekaServer 示意开启 Eureka 服务 开启注册核心
 public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);
 }
}

测试

启动 Eureka Server(集群) 服务, 启动 Member 和 order 服务。
别离拜访 Eureka 集群服务:http://localhost:8100/,http://localhost:9100/


能够看到 provide 服务注册到了默认 Eureka 上。

调用会员模块:http://localhost:8000/getMember?name=99

调用订单模块,查看是否 feign 调用到会员模块:http://localhost:8200/getOrderByMember?name=iswulongbo

胜利调用!然而 name 没有胜利入参:

解决方案:入参强制要求,须要在 IMemberService 接口 以及 MemberServiceImpl 实现类中都退出 @RequestParam(“name”) 注解:


再次拜访:http://localhost:8200/getOrderByMember?name=iswulongbo
入参胜利!

至此实现了和调用 http://localhost:8000/getMember?name=iswulongbo 一样的成果:

正文完
 0