标签:star interface 测试 ica server cts conf prope oid
五、服务消费方搭建 1.在父项目上面新建模块myclient 2.选择Spring Cloud Discovery—>Eureka Discovery Client 选择Spring Cloud Routing—>OpenFeign 3.Module Name一般不做修改,和项目名称Artifact一样 4.将src\main\resources下面的application.properties改名为bootstrap.yml,修改文件编码方式为UTF-8,内容如下 spring: application: name: myclient cloud: config: discovery: enabled: true service-id: myconfig # 配置中心服务名 profile: dev # 环境名 zipkin: enabled: true base-url: http://localhost:9411/ locator: discovery: enabled: true sender: type: WEB sleuth: web: client: enabled: true sampler: # 默认的采样比率为0.1,不能看到所有请求数据 # 更改采样比率为1,就能看到所有的请求数据了,但是这样会增加接口调用延迟 probability: 1.0 eureka: instance: lease-renewal-interval-in-seconds: 5 # 心跳时间,即服务续约间隔时间 (缺省为30s) lease-expiration-duration-in-seconds: 10 # 没有心跳的淘汰时间,10秒,即服务续约到期时间(缺省为90s) prefer-ip-address: true # 将IP注册到服务注册中心 client: service-url: defaultZone: http://localhost:1024/eureka/ fetch-registry: true # 向注册中心注册 registry-fetch-interval-seconds: 5 # 服务清单的缓存更新时间,默认30秒一次 feign: hystrix: enabled: true # feign熔断器开关 默认关闭 打开才能调用fallback方法 client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: full # 在开发的时候数据所有feign日志 5.打开src\main\java\com\li\myclient下面的MyclientApplication.java 在启动类上加入@EnableDiscoveryClient注解,声明该微服务注册到服务注册中心。 加入@EnableFeignClients,声明使用Feign调用接口。 @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class MyclientApplication { public static void main(String[] args) { SpringApplication.run(MyclientApplication.class, args); } } 6.pom文件中增加依赖,并在Maven Projects里面刷新 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 7.src\main\java\com\li\myclient下面新建包controller,新建MyController.java @RestController public class MyController { @Autowired private MyFeignClient myFeignClient; @Value("${server.port}") private String port; @GetMapping("/getTime") public String getTime(){ return myFeignClient.getTime(); } @GetMapping("/getConfig") public String getConfig(){ return port; } } 8.src\main\java\com\li\myclient下面新建包service,新建接口MyFeignClient.java @FeignClient(value="myservice",fallback = MyFeignClientFallback.class) public interface MyFeignClient { @GetMapping("/getTime") public String getTime(); } 9.src\main\java\com\li\myclient\service下面新建包impl,新建接口MyFeignClientFallback.java @Service public class MyFeignClientFallback implements MyFeignClient { @Override public String getTime(){ return ""; } } 10.打开浏览器访问http://localhost:1002/getTime 11.测试熔断,停止myservice,访问http://localhost:1002/getTime 12.测试配置中心,访问http://localhost:2001/getConfig
标签:star interface 测试 ica server cts conf prope oid
原文地址:https://www.cnblogs.com/liw66/p/12294610.html