1、创建springboot项目,略;
2、添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.7.2</version>
</dependency>
3、启动类
@SpringBootApplication
@EnableFeignClients
public class FeginClientDemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(FeginClientDemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder){
return applicationBuilder.sources(Application.class);
}
}
4、okhttp配置
/**
* @Author: Liu Yue
* @Descripition:
* @Date; Create in 2020/11/4 13:36
**/
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {
@Bean
public okhttp3.OkHttpClient okHttpClient(){
return new okhttp3.OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool())
.build();
}
}
5、代理类
/**
* @Author: Liu Yue
* @Descripition: 阈值设置的service
* @Date; Create in 2020/11/4 13:20
**/
@FeignClient(name = "feignClientProxy",url = "http://localhost:9005/rocketmq-producer/")
public interface ThresholdServiceProxy {
@GetMapping(value = "/fegin/server/send/{id}/{thresholdval}")
String invoke(@PathVariable("id") String id, @PathVariable("thresholdval") String thresholdval);
/**
* 容错处理类,当调用失败时 返回空字符串
*/
@Component
class DefaultFallback implements ThresholdServiceProxy {
@Override
public String invoke(@PathVariable("id") String id, @PathVariable("thresholdval")String thresholdval){
return "ERROR";
}
}
}
6、接口类
/**
* @Author: Liu Yue
* @Descripition: 阈值设置的controller
* @Date; Create in 2020/11/4 13:58
**/
@RestController
@RequestMapping("/threshold")
public class ThresholdController {
@Resource
ThresholdServiceProxy thresholdServiceProxy;
@GetMapping("/send/{id}/{thresholdval}")
public String sendThresholdVal(@PathVariable("id") String id,
@PathVariable("thresholdval")String thresholdval){
String rs = thresholdServiceProxy.invoke(id, thresholdval);
return "SUCCESS";
}
}
设置路径: http://localhost:9011/feginDemo/threshold/send/01/FA
每天提高一点点!