H5W3
当前位置:H5W3 > java > 正文

【Java】SpringCloud整合Feign调用重构

SpringCloud整合Feign调用重构

isWulongbo发布于 今天 01:54

目录结构

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)

【Java】SpringCloud整合Feign调用重构

模块代码

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>

【Java】SpringCloud整合Feign调用重构

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>

【Java】SpringCloud整合Feign调用重构

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>

【Java】SpringCloud整合Feign调用重构

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;
}

【Java】SpringCloud整合Feign调用重构

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);
}

【Java】SpringCloud整合Feign调用重构

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);
}
}

【Java】SpringCloud整合Feign调用重构

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);
}
}

【Java】SpringCloud整合Feign调用重构

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/
【Java】SpringCloud整合Feign调用重构
【Java】SpringCloud整合Feign调用重构
可以看到provide服务注册到了默认Eureka上。

调用会员模块:http://localhost:8000/getMember?name=99
【Java】SpringCloud整合Feign调用重构

调用订单模块,查看能否feign调用到会员模块:http://localhost:8200/getOrderByMember?name=iswulongbo
【Java】SpringCloud整合Feign调用重构

成功调用!但是 name 没有成功入参:

解决方案:入参强制要求,需要在 IMemberService接口 以及 MemberServiceImpl实现类中都加入 @RequestParam(“name”)注解:
【Java】SpringCloud整合Feign调用重构
【Java】SpringCloud整合Feign调用重构
再次访问:http://localhost:8200/getOrderByMember?name=iswulongbo
入参成功!
【Java】SpringCloud整合Feign调用重构

至此实现了和调用 http://localhost:8000/getMember?name=iswulongbo一样的效果:
【Java】SpringCloud整合Feign调用重构

javaspringboot
阅读 28发布于 今天 01:54
本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

wulongbo
我的专栏
avatar

isWulongbo

在人生的头三十年,你培养习惯,后三十年,习惯铸就你

178 声望
8 粉丝

0 条评论
得票时间

avatar

isWulongbo

在人生的头三十年,你培养习惯,后三十年,习惯铸就你

178 声望
8 粉丝

宣传栏

目录结构

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)

【Java】SpringCloud整合Feign调用重构

模块代码

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>

【Java】SpringCloud整合Feign调用重构

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>

【Java】SpringCloud整合Feign调用重构

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>

【Java】SpringCloud整合Feign调用重构

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;
}

【Java】SpringCloud整合Feign调用重构

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);
}

【Java】SpringCloud整合Feign调用重构

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);
}
}

【Java】SpringCloud整合Feign调用重构

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);
}
}

【Java】SpringCloud整合Feign调用重构

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/
【Java】SpringCloud整合Feign调用重构
【Java】SpringCloud整合Feign调用重构
可以看到provide服务注册到了默认Eureka上。

调用会员模块:http://localhost:8000/getMember?name=99
【Java】SpringCloud整合Feign调用重构

调用订单模块,查看能否feign调用到会员模块:http://localhost:8200/getOrderByMember?name=iswulongbo
【Java】SpringCloud整合Feign调用重构

成功调用!但是 name 没有成功入参:

解决方案:入参强制要求,需要在 IMemberService接口 以及 MemberServiceImpl实现类中都加入 @RequestParam(“name”)注解:
【Java】SpringCloud整合Feign调用重构
【Java】SpringCloud整合Feign调用重构
再次访问:http://localhost:8200/getOrderByMember?name=iswulongbo
入参成功!
【Java】SpringCloud整合Feign调用重构

至此实现了和调用 http://localhost:8000/getMember?name=iswulongbo一样的效果:
【Java】SpringCloud整合Feign调用重构

本文地址:H5W3 » 【Java】SpringCloud整合Feign调用重构

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址