添加依赖
org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR3
‘org.springframework.cloud:spring-cloud-starter-consul-discovery’,
‘com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2.1.2.RELEASE’
‘io.github.openfeign:feign-okhttp’,
‘org.springframework.cloud:spring-cloud-starter-openfeign’
nacos 配置
spring: profiles: active: local application: # TODO: 这里配置成实际工程的名字, 作为发布服务的ID. name: test-server main: allow-bean-definition-overriding: true cloud: consul: config: enabled: false nacos: config: server-addr: nacos2-offline.com:80 enabled: true namespace: dev group: TEST_GROUP username: dev password: skLN2LsI/JC2yBtg file-extension: yml
consul配置
cloud: config: override-none: true // 本地配置覆盖远程 consul: host: consul-dev-service port: 8500 discovery: #prefer-ip-address: false healthCheckPath: /actuator/health healthCheckInterval: 15s hostname: 10.0.3.105 port: 30039
1.在启动类上添加 EnableFeignClients 制定扫描路径
添加依赖
@Slf4j @EnableFeignClients({"com.test.api"}) @EnableDiscoveryClient @SpringBootApplication public class Application implements WebMvcConfigurer{ public static void main(String[] args) { SpringApplication.run(DcJingdongProviderApplication.class, args); } }
2.提供 controller
@RequestMapping("/api") @RestController public class ApiController { @ApiOperation(value = "测试") @PostMapping(value = "/test") public String test (@RequestBody TestDTO request) throws Exception { return "测试成功!!"; } }
- 提供 远程调用接口
@FeignClient(name = "服务名称", path = "/eclp",fallback = TestServiceHystrix.class) public interface TestServiceRemote { @RequestMapping(value = "/api/test", method = RequestMethod.POST,produces="application/json;charset=UTF-8") String test (@RequestBody TestDTO request); }
- 熔断器
@Slf4j @Component public class EclpServiceHystrix implements EclpServiceRemote { @Override public String test (TestDTO request) { log.info("服务降低!!"); return "服务降低!!"; } }
6.服务使用者,只需在启动类添加@EnableFeignClients({“com.test.api”})
@Slf4j
@EnableFeignClients({“com.test.api”})
@EnableDiscoveryClient
@SpringBootApplication
public class Application implements WebMvcConfigurer{
public static void main(String[] args) { SpringApplication.run(DcJingdongProviderApplication.class, args); }
}
客户端测试controller
在服务中注入 TestServiceRemote 直接使用即可
@Slf4j @Api(value = "test测试 ", tags = "Test测试") @RequestMapping("/test") @RestController public class TestController { @Autowired TestServiceRemote testServiceRemote ; @ApiOperation(value = "删除缓存的shopkey") @RequestMapping(value = "/test", method = RequestMethod.POST) public result queryPoOrder() { TestDTO testDto= new TestDTO (); String result = testServiceRemote .queryPoOrder(requestDTO); return result ; }
引入依赖
‘org.springframework.cloud:spring-cloud-starter-openfeign’
‘io.github.openfeign:feign-okhttp’,
yml 配置文件
feign: hystrix: enabled: true compression: request: enabled: true min-request-size: 2048 response: enabled: true httpclient: enabled: true # 支持HttpClient的开关 max-connections: 500 # 最大连接数 max-connections-per-route: 100 # 单个路径的最大连接数 client: config: default: loggerLevel: basic
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)