1.环境搭建
1.创建模块 sunrays-common-cloud-openfeign-starter
2.目录结构
3.OpenFeignAutoConfiguration.java 自动配置类
package com.sunxiansheng.cloud.openfeign.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@Slf4j
public class OpenFeignAutoConfiguration {
@PostConstruct
public void logConfigSuccess() {
log.info("OpenFeignAutoConfiguration has been loaded successfully!");
}
}
4.spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.cloud.openfeign.config.OpenFeignAutoConfiguration
5.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>sunrays-common-cloud-openfeign-starter</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.sunrays-common-cloud 模块整体测试
1.创建demo模块
2.新建三个服务都注册到Nacos
1.项目结构
2.sunrays-common-cloud-base-starter 的pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-demo</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<packaging>pom</packaging>
<description>对spring-cloud的常用组件gateway、nacos、openfeign进行统一测试</description>
<modules>
<module>gateway</module>
<module>service-a</module>
<module>service-b</module>
</modules>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-nacos-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
3.gateway
1.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>gateway</artifactId>
</project>
2.application.yml
spring:
application:
name: gateway
cloud:
nacos:
discovery:
enabled: true
server-addr:
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/sunrays-common-cloud-base-starter/gateway/logs
module: sunrays-demo/sunrays-common-cloud-base-starter/gateway
server:
port: 8081
3.GateWayApplication.java
package com.sunxiansheng.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
}
4.service-a
1.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>service-a</artifactId>
</project>
2.application.yml
spring:
application:
name: service-a
cloud:
nacos:
discovery:
enabled: true
server-addr:
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/sunrays-common-cloud-base-starter/service-a/logs
module: sunrays-demo/sunrays-common-cloud-base-starter/service-a
server:
port: 8082
3.ServiceAApplication.java
package com.sunxiansheng.serviceA;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
}
}
5.service-b
1.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>service-b</artifactId>
</project>
2.application.yml
spring:
application:
name: service-b
cloud:
nacos:
discovery:
enabled: true
server-addr:
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/sunrays-common-cloud-base-starter/service-b/logs
module: sunrays-demo/sunrays-common-cloud-base-starter/service-b
server:
port: 8083
3.ServiceBApplication.java
package com.sunxiansheng.serviceB;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceBApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceBApplication.class, args);
}
}
6.全部启动,成功注册到Nacos
3.配置GateWay
1.目录结构
2.gateway
1.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>gateway</artifactId>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-gateway-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
2.application.yml 配置路由到两个模块
spring:
application:
name: gateway
cloud:
nacos:
discovery:
enabled: true
server-addr:
gateway:
routes:
- id: service-a
uri: lb://service-a
predicates:
- Path=/service-a/**
filters:
- StripPrefix=1
- id: service-b
uri: lb://service-b
predicates:
- Path=/service-b/**
filters:
- StripPrefix=1
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/sunrays-common-cloud-base-starter/gateway/logs
module: sunrays-demo/sunrays-common-cloud-base-starter/gateway
server:
port: 8081
3.service-a
1.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>service-a</artifactId>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>common-web-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
2.ServiceAController.java
package com.sunxiansheng.serviceA.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceAController {
@RequestMapping("/serviceA")
public String serviceA() {
return "serviceA";
}
}
4.service-b
1.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>service-b</artifactId>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>common-web-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
2.ServiceBController.java
package com.sunxiansheng.serviceB.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceBController {
@RequestMapping("/serviceB")
public String serviceB() {
return "serviceB";
}
}
5.测试,网关服务发现+前缀+上下文路径+资源路径
http://localhost:8081/service-a/serviceA
http://localhost:8081/service-b/serviceB
4.配置OpenFeign
1.目录结构
2.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-base-starter</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>service-a</artifactId>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>common-web-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-openfeign-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
3.ServiceBRpc.java 暴露rpc接口
package com.sunxiansheng.serviceA.rpc;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name = "service-b")
public interface ServiceBRpc {
@RequestMapping("/serviceB")
String serviceB();
}
4.ServiceAController.java 注入rpc接口,一旦调用就会被代理请求
package com.sunxiansheng.serviceA.controller;
import com.google.gson.Gson;
import com.sunxiansheng.serviceA.rpc.ServiceBRpc;
import com.sunxiansheng.tool.response.ResultWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class ServiceAController {
@RequestMapping("/serviceA")
public String serviceA() {
return "serviceA";
}
@Resource
private ServiceBRpc serviceBRpc;
@RequestMapping("/rpcToServiceB")
public String rpcToServiceB() {
String json = serviceBRpc.serviceB();
Gson gson = new Gson();
ResultWrapper resultWrapper = gson.fromJson(json, ResultWrapper.class);
return (String) resultWrapper.getData();
}
}
5.ServiceAApplication.java 启动类使用 @EnableFeignClients注解
package com.sunxiansheng.serviceA;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class ServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
}
}
6.测试