本文共 2545 字,大约阅读时间需要 8 分钟。
1.System Status信息修改
在eureka注册中心的项目上进行修改:
eureka: environment: weifuwu datacenter: weifuwu-cloud
重启eureka进行访问:
2.eureka剔除心跳失败的节点
我们希望Eureka Server直接踢出已关停的节点的配置如下:
服务器端配置:
eureka: server: enable-self-preservation: false (设为false,关闭自我保护) eviction-interval-timer-in-ms: 4000
客户端配置:
eureka: lease-renewal-interval-in-seconds: 10 lease-expiration-duration-in-seconds: 30
这里我们设置服务端关闭自我保护,然后清理服务列表的时间间隔为4秒;客户端端租期更新时间间改为10秒,租期到期时间改为30秒,这样Eureka会更快感知到其租期到期,并直接关闭该服务。
3、Eureka配置instanceId显示IP
配置参数:${spring.cloud.client.ipAddress}
注意:spring could 2.0版本 需要改成${spring.cloud.client.ip-address}
eureka: instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
1.自定义配置时,@Configuration和@ComponentScan包不应重叠
自定义的RibbonConfiguration类必须用@Configuration注解标注,但是它不应该在主Application Context的组件扫描之中,否则它将被所有的Ribbon客户端共享。
例如:自定义的配置类需要引入@Configuration注解,而启动类需要@SpringBootApplication,而次注解包含Application Context,所以二者不能放在同一个包下。
2.使用RestTemplate时,想要获得一个List时,应该用数组,而不应该直接用List
我们在user中编写代码来反映这个问题。我们在user的Controller类中添加这样一个服务:
@GetMapping("/list-all")public ListlistAll(){ List userList = new ArrayList(); User u1 = new User(); u1.setId(1L); u1.setName("jack1"); User u2 = new User(); u2.setId(2L); u2.setName("jack2"); User u3 = new User(); u3.setId(3L); u3.setName("jack3"); userList.add(u1); userList.add(u2); userList.add(u3); return userList;}
启动user,直接访问“list-all”得到的结果如下:
然后我们需要在movie的服务中通过ribbon去调用这个服务,所以在movie工程的Controller方法中添加这样一个服务:
@GetMapping("/list-all")public ListlistAll(){ List list = this.restRemplate.getForObject("http://microserver-provider-user/list-all/", List.class); for(User user:list) { System.out.println(user.getId()); } return list;}
启动movie服务,访问一下它的“list-all”服务:
解决办法,将接收的List类型数据,使用目标实体类的数组来接收:
1、自定义配置时,@Configuration和@ComponentScan包不应重叠
这一块和Ribbon一样,这里不再赘述。2、@FeignClient所在的接口中,不支持@GetMapping等组合注解
通常我们在FeignClient中使用的接口修饰都是@RequestMapping或者@RequestLine(主要看Contract 契约的设定,默认是SpringMVC的@RequestMapping,配置成Default就是@RequestLine),例如之前编写的UserFeignClient:注意使用到了@RequestLine注解之后,一定要在congfiguration后面配置配置类中,开启fegin特有注解的支持。
3、使用@PathVariable时,需要指定其value
我们在FeignClient中使用@PathVariable时,即使参数不使用别名,也需要为@PathVariable指定value。4、Feign暂不支持复杂对象作为一个参数
例如之前我们在浏览器上调用testPullUser方法。url路径是这么拼写的:
http://localhost:7901/testPullUser?id=888&username=杰克&name=jack&age=25&balance=2500 在后台服务是这么写的:转载地址:http://touzz.baihongyu.com/