feign重构源码
1、-----sccase: 父项目,里面是共同的依赖,maven project 的打包类型选择pom
##########################################################
##########################################################
##########################################################
##############1、eurake:com.sc.ek2.Ek2Application#########
##############2、providercom.sc.api.AppMember#############
##############3、consume:com.sc.api.AppOrder##############
##########################################################
##########################################################
################<groupId>com.sc</groupId>#################
################<artifactId>sccase</artifactId>###########
##########################################################
1.1、pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sc</groupId>
<artifactId>sccase</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>ek2</module>
<module>springcloud-api-service</module>
<module>springcloud-api-member-service</module>
<module>springcloud-api-order-service</module>
<module>springcloud-api-member-impl-service</module>
<module>springcloud-api-order-impl-service</module>
</modules>
<properties>
<java.version>1.8</java.version>
<!-- <spring-cloud.version>Hoxton.SR3</spring-cloud.version>-->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--临控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、-----sccase: 父项目
springcloud-api-member-service: 子项目
2.1、pom.xml
<parent>
<groupId>com.sc</groupId>
<artifactId>sccase</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-member-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-api-member-service</name>
<description>Demo project for Spring Boot</description>
2.2、IMemberService
package com.sc.api.service;
import com.sc.api.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
public interface IMemberService {
//实体类与定义接口放在接口项目
@RequestMapping("/getMember")
public UserEntity getMember(@RequestParam("name") String name);
}
2.3、entity
package com.sc.api.entity;
import lombok.Data;
@Data
public class UserEntity {
private String name;
private String age;
public UserEntity(String name, String age) {
this.name = name;
this.age = age;
}
}
3、-----sccase: 父项目
springcloud-api-member-impl-service: 子项目
##########################################################
9002######################################################
http://localhost:9002/getMember?name=sdfsdfsafa###########
#########zuul 9006#################################################
http://localhost:9006/member/getMember?name=sdfsdfsafa&token=12311
##################################################################
3.1、pom.xml
<parent>
<groupId>com.sc</groupId>
<artifactId>sccase</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-member-impl-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-api-member-impl-service</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-member-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3.2、MemberServiceImpl
package com.sc.api.service.impl;
import IMemberService;
import com.sc.api.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("会员服务接口")
@RestController
public class MemberServiceImpl implements IMemberService {
@ApiOperation("获取会员相关信息")
@ApiImplicitParam(name="name",value = "用户信息参数",required = true, dataType = "String")
@RequestMapping("/getMember")
public UserEntity getMember(@RequestParam("name") String name) {
UserEntity userEntity=new UserEntity();
userEntity.setName(name);
userEntity.setAge("12");
return userEntity;
}
}
3.3、SpringBootApplication
package com.sc.api;
AppMember.java
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableSwagger2Doc
注:SpringBootApplication 放在最外包层
3.4、application.properties
server.port=9002
#server.address=localhost
spring.application.name=member
#服务注册地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
4、-----sccase: 父项目
springcloud-api-order-service: 子项目
4.1、pom.xml
<parent>
<groupId>com.sc</groupId>
<artifactId>sccase</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-api-order-service</name>
<description>Demo project for Spring Boot</description>
4.2、IOrderService
package com.sc.api.service;
import org.springframework.web.bind.annotation.RequestMapping;
public interface IOrderService {
//订单服务调用会员服务接口信息feign
@RequestMapping("/orderToMember")
public String orderToMember(String name);
}
5、-----sccase: 父项目
springcloud-api-order-impl-service: 子项目
##########################################################
#####9003#############################################################
1)、HystrixCommand
###########http://localhost:9003/orderToMember?name=hhhh##
zuul#######http://localhost:9006/order/orderToMember?name=hhhh&token=12311####
2)、FeignClient(value = "member",fallbac
###########http://localhost:9003/orderToMember2?name=hhhh##
zuul#######http://localhost:9006/order/orderToMember2?name=hhhh&token=12311####
##########################################################
5.1、pom.xml
<parent>
<groupId>com.sc</groupId>
<artifactId>sccase</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-order-impl-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-api-order-impl-service</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sc</groupId>
<artifactId>springcloud-api-member-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
5、2、feign
package com.sc.api.feign;
import com.sc.api.service.IMemberService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "member",fallback = MemberServiceFallback.class)
public interface MemberServiceFeign extends IMemberService {
}
5、3、OrderServiceImpl
@Api("订单服务")
@RestController
public class OrderServiceImpl implements IOrderService {
@Autowired
private MemberServiceFeign memberServiceFeign;
@HystrixCommand(fallbackMethod = "hiError")
@RequestMapping("/orderToMember")
public String orderToMember(String name) {
UserEntity userEntity=memberServiceFeign.getMember(name);
return userEntity==null?"没找到用户信息":userEntity.toString();
}
public String hiError(String name) {
return "hi, "+name+",sorry,error!";
}
/*
Hystrix第二种写法
*/
@ApiOperation("订单服务接口")
@RequestMapping("/orderToMember2")
public String orderToMember2(String name) {
UserEntity userEntity=memberServiceFeign.getMember(name);
return userEntity==null?"没找到用户信息":userEntity.toString();
}
}
5、4、SpringBootApplication
package com.sc.api;
AppMember.java
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableSwagger2Doc
5.5、application.properties
server.port=9003
#server.address=localhost
spring.application.name=order
#服务注册地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
feign.hystrix.enabled=true
5.6、MemberServiceFallback
@Component
public class MemberServiceFallback implements MemberServiceFeign {
@Override
public UserEntity getMember(String name) {
return new UserEntity("error","11");
}
}
6、ConfigServer
#############9004################################
#############config server#######################
http://localhost:9004/config-client-dev.properties
http://localhost:9004/config-client/dev
http://localhost:9004/config-client/dev
http://localhost:9004zuul/dev
#################################################
git:https://github.com/sf2014/sc-config.git
###############config-client-dev.properties#####
https://github.com/sf2014/sc-config/client1/
config-client-dev.properties ###################
#################################################
6-1、pom.xml
<parent>
<groupId>com.sc</groupId>
<artifactId>sccase</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.sc</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configserver</name>
<description>Demo project for Spring Boot</description>
6-2、application.properties
server.port=9004
spring.application.name=config-server
#eureka服务注册地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
#eureka.client.serviceUrl.defaultZone=http://localhost:9001/" target="_blank">http://localhost:9001/eureka/,http://localhost:9008/eureka/
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/,http://localhost:9008/eureka/
#连接GitHub
#https://github.com/sf2014/sc-config.git
spring.cloud.config.server.git.uri=https://github.com/sf2014/sc-config.git
spring.cloud.config.server.git.search-paths=client1
spring.cloud.config.label=master
spring.cloud.config.server.git.username=sf2014@qq.com
spring.cloud.config.server.git.password=sf2014git
6-3、CsApplication.java
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
7、ConfigClient
#############9005##################################
#############config client#########################
http://localhost:9005/foo
###################################################
###################################################
ConfigServer:http://localhost:9004配置变化,需要
actuator/refresh手动刷新同步
i)、 Controller=>@RefreshScope //actuator
ii)、 Post=>http://localhost:9005/actuator/refresh
postman
###################################################
###################################################
7-1、pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sc</groupId>
<artifactId>configclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--临控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
7-2、bootstrap.properties
server.port=9005
spring.application.name=config-client
####config###########################
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:9004
#eureka服务注册地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
#eureka.client.serviceUrl.defaultZone=http://localhost:9001/" target="_blank">http://localhost:9001/eureka/,http://localhost:9008/eureka/
eureka.client.serviceUrl.defaultZone=http://localhost:9001/" target="_blank">http://localhost:9001/eureka/
#访问端点根路径,默认为/actuator
management.endpoints.web.base-path=/actuator
#需要开启的端点,这里主要用到是refresh这个端点
management.endpoints.web.exposure.include=*
#不需要开启的端点
#management.endpoints.web.exposure.exclude=
7-3、ConfigconsumerApplication
@SpringBootApplication
@RefreshScope //actuator
@RestController
@EnableEurekaClient
public class ConfigconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigconsumerApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/foo")
public String hi(){
return foo;
}
}
8、ZL Zuul
###9006############################################
#####http://localhost:9006/member/getMember?name=sdfsdfsafa
#######http://localhost:9006/order/orderToMember2?name=hhhh##
#######http://127.0.0.1:9006/swagger-ui.html##########swagger
###############################################
8-1、pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sc</groupId>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--临控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
8-2、application.properties
server.port=9006
server.address=localhost
spring.application.name=zuul
#服务注册地址
eureka.instance.ip-address=localhost
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/
#路由配置 #- 临时注解
#url形式
#- zuul.routes.baidu.path=/baidu/**
#- zuul.routes.baidu.url=http://www.baidu.com
#注册服务provider####
#- zuul.routes.member.path=/member/**
#- zuul.routes.member.service-id=member
#注册服务consumer####
#- zuul.routes.order.service-id=order
#- zuul.routes.order.path=/order/*
####config-server###########################
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:9004
#访问端点根路径,默认为/actuator
management.endpoints.web.base-path=/actuator
#需要开启的端点,这里主要用到是refresh这个端点
management.endpoints.web.exposure.include=*
#不需要开启的端点
#management.endpoints.web.exposure.exclude=
8-3、AccessFilter
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class AccessFilter extends ZuulFilter {
//获取Zuul服务端口号
@Value("${server.port}")
private String serverPort;
@Override
public String filterType() {
return "pre";
}
/**
* 判断过滤器是否执行,直接返回true,代表对所有请求过滤
* 此方法指定过滤范围
* @return
*/
@Override
public int filterOrder() {
return 0;
}
/**
* 过滤的具体逻辑
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
String token = request.getParameter("token");
request.setAttribute("serverPort", serverPort);
if (StringUtils.isEmpty(token)) {
currentContext.setSendZuulResponse(false);
currentContext.setResponseBody("token is null");
currentContext.setResponseStatusCode(401);
return null;
}
System.out.println("网关执行端口号:" + serverPort);
return null;
}
}
8-4、ZuulApplication.java
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
@EnableSwagger2Doc
#######spring.cloud.config.uri=http://localhost:9004
#######zuul-dev.properties
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
8-5、 DocumentationConfig
// 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
public List<SwaggerResource> get() {
List resources = new ArrayList<Object>();
//app-itmayiedu-order
resources.add(swaggerResource("member", "/member/v2/api-docs", "2.0")); // 第一个参数可以随便写 第二个参考yml对应
resources.add(swaggerResource("order", "/order/v2/api-docs", "2.0"));
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
9、nginx-zuul
###zuul:9006 zuul:9007##############################################
###http://localhost/member/getMember?name=sdfsdfsafa&token=12311######
###http://localhost/order/orderToMember2?name=hhhh&token=12311########
######################################################################
9-1、nginx.conf
##########http://nginx.org/#######################################
nginx-1.19.1conf
ginx.conf######################################
upstream backServer{
server 127.0.0.1:9006 weight=1;
server 127.0.0.1:9007 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
proxy_pass http://backServer/;
index index.html index.htm;
}
10、其他问题
10.1、提示如下问题
1.1、pom.xml以下节点没有添加会提示如下问题
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
问题.......................................
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
No qualifying bean of type 'org.springframework.core.env.Environment
##