-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from KEHFAN/20240709-openFeign
新增openfeign示例代码
- Loading branch information
Showing
11 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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> | ||
|
||
<groupId>com.netease.lowcode.extension</groupId> | ||
<artifactId>OpenFeign</artifactId> | ||
<version>1.0.15</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<!-- 与应用保持一致 --> | ||
<spring.boot.version>2.2.9.RELEASE</spring.boot.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- 导出源码启动时将此依赖手动添加到pom --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-openfeign</artifactId> | ||
<version>${spring.boot.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<artifactId>nasl-metadata-collector</artifactId> | ||
<groupId>com.netease.lowcode</groupId> | ||
<version>0.8.0</version> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>com.netease.lowcode</groupId> | ||
<artifactId>nasl-metadata-maven-plugin</artifactId> | ||
<version>1.4.2</version> | ||
<configuration> | ||
<jarWithDependencies>false</jarWithDependencies> | ||
<rewriteVersion>false</rewriteVersion> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>archive</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
10 changes: 10 additions & 0 deletions
10
OpenFeign/src/main/java/com/netease/lowcode/extension/OpenFeignAutoScan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.netease.lowcode.extension; | ||
|
||
import com.netease.lowcode.extension.service.UserService; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ComponentScan(basePackageClasses = {UserService.class}) | ||
public class OpenFeignAutoScan { | ||
} |
14 changes: 14 additions & 0 deletions
14
OpenFeign/src/main/java/com/netease/lowcode/extension/OpenFeignConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.netease.lowcode.extension; | ||
|
||
import com.netease.lowcode.extension.feign.ExtensionUserClient; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableFeignClients(basePackageClasses = ExtensionUserClient.class) | ||
public class OpenFeignConfig { | ||
|
||
public OpenFeignConfig() { | ||
System.out.println("OpenFeignConfig 扫描成功"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
OpenFeign/src/main/java/com/netease/lowcode/extension/OpenFeignEnvironmentPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.netease.lowcode.extension; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.env.EnvironmentPostProcessor; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.MapPropertySource; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class OpenFeignEnvironmentPostProcessor implements EnvironmentPostProcessor { | ||
@Override | ||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { | ||
Map<String,Object> config = new HashMap<>(); | ||
config.put("feign.hystrix.enabled",true); | ||
config.put("hystrix.command.default.circuitBreaker.requestVolumeThreshold",2); | ||
config.put("hystrix.command.default.circuitBreaker.errorThresholdPercentage",0); | ||
|
||
environment.getPropertySources().addLast(new MapPropertySource("OPENFEIGNMAP",config)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
OpenFeign/src/main/java/com/netease/lowcode/extension/feign/ExtensionUserClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.netease.lowcode.extension.feign; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@FeignClient(name="extensionUserClient",url = "http://dev.nacos.cstest.lcap.codewave-test.163yun.com") | ||
public interface ExtensionUserClient { | ||
|
||
@GetMapping("/rest/hello") | ||
String hello(); | ||
} |
14 changes: 14 additions & 0 deletions
14
...gn/src/main/java/com/netease/lowcode/extension/feign/ExtensionUserClientWithFallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.netease.lowcode.extension.feign; | ||
|
||
import com.netease.lowcode.extension.service.ExtensionUserClientFallback; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@FeignClient(name = "extensionUserClientWithFallback", | ||
url = "http://dev.nacos.cstest.lcap.codewave-test.163yun.com", | ||
fallback = ExtensionUserClientFallback.class) | ||
public interface ExtensionUserClientWithFallback { | ||
|
||
@GetMapping("/rest/hello") | ||
String hello(); | ||
} |
13 changes: 13 additions & 0 deletions
13
...eign/src/main/java/com/netease/lowcode/extension/service/ExtensionUserClientFallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.netease.lowcode.extension.service; | ||
|
||
import com.netease.lowcode.extension.feign.ExtensionUserClientWithFallback; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExtensionUserClientFallback implements ExtensionUserClientWithFallback { | ||
|
||
@Override | ||
public String hello() { | ||
return "触发熔断"; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
OpenFeign/src/main/java/com/netease/lowcode/extension/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.netease.lowcode.extension.service; | ||
|
||
import com.netease.lowcode.core.annotation.NaslLogic; | ||
import com.netease.lowcode.extension.feign.ExtensionUserClient; | ||
import com.netease.lowcode.extension.feign.ExtensionUserClientWithFallback; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserService { | ||
|
||
private static ExtensionUserClient extensionUserClient; | ||
private static ExtensionUserClientWithFallback extensionUserClientWithFallback; | ||
|
||
/** | ||
* 普通接口调用 | ||
* 当服务不可用时直接异常 | ||
* | ||
* @return | ||
*/ | ||
@NaslLogic | ||
public static String hello() { | ||
return extensionUserClient.hello(); | ||
} | ||
|
||
/** | ||
* 开启熔断器的接口调用 | ||
* 当调用服务不可用时将触发熔断降级 | ||
* | ||
* @return | ||
*/ | ||
@NaslLogic | ||
public static String helloWithFallback() { | ||
return extensionUserClientWithFallback.hello(); | ||
} | ||
|
||
@Autowired | ||
public void setUserClient(ExtensionUserClient extensionUserClient) { | ||
UserService.extensionUserClient = extensionUserClient; | ||
} | ||
@Autowired | ||
public void setExtensionUserClientWithFallback(ExtensionUserClientWithFallback extensionUserClientWithFallback){ | ||
UserService.extensionUserClientWithFallback = extensionUserClientWithFallback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.netease.lowcode.extension.OpenFeignConfig,\ | ||
com.netease.lowcode.extension.OpenFeignAutoScan | ||
org.springframework.boot.env.EnvironmentPostProcessor=\ | ||
com.netease.lowcode.extension.OpenFeignEnvironmentPostProcessor |
Empty file.