SpringCloud系列使用Eureka进行服务治理 (2)

在这里插入图片描述


写个例子,以github用户为例:

package com.example.springcloud.provider.bean; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import java.io.Serializable; /** * <pre> * Github User * </pre> * * <pre> * @author mazq * 修改记录 * 修改后版本: 修改人: 修改日期: 2020/07/27 17:38 修改内容: * </pre> */ @JsonIgnoreProperties(ignoreUnknown = true) @Data public class User implements Serializable { private String name; private String blog; @Override public String toString() { return "User{" + "name='" + name + '\'' + ", blog='" + blog + '\'' + '}'; } }

读取github用户信息:

package com.example.springcloud.provider.service; import com.example.springcloud.provider.bean.User; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * <pre> * UserService * </pre> * * <pre> * @author mazq * 修改记录 * 修改后版本: 修改人: 修改日期: 2020/07/27 17:42 修改内容: * </pre> */ @Service @Slf4j public class UserService { private final RestTemplate restTemplate; public UserService(RestTemplateBuilder restTemplateBuilder) { this.restTemplate = restTemplateBuilder.build(); } public User findUser(String user) throws InterruptedException { log.info("username[{}]" , user); String url = String.format("https://api.github.com/users/%s", user); User results = restTemplate.getForObject(url, User.class); return results; } }

@EnableEurekaClient指定eureka client:

package com.example.springcloud.provider; import com.example.springcloud.provider.bean.User; import com.example.springcloud.provider.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @RestController public class SpringcloudServiceProviderApplication { @Autowired UserService userService; public static void main(String[] args) { SpringApplication.run(SpringcloudServiceProviderApplication.class, args); } @GetMapping({"/api/users/{username}"}) @ResponseBody public User findUser(@PathVariable("username")String username) throws InterruptedException{ User user= userService.findUser(username); return user; } }

部署后,注册信息发布到eureka server:

在这里插入图片描述


服务注册信息打印到控制台

在这里插入图片描述


访问接口::8081/api/users/mojombo,能访问就是注册成功

提示:EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

在这里插入图片描述

SpringCloud警告(Eureka):EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
警告!Eureka可能存在维护了错误的实例列表(当它们没有启动的时候,Eureka却把它当成启动的了);Renews值小于Threshold值,因此剩下未过期的都是安全的。

方法:参考博客https://www.cnblogs.com/gudi/p/8645370.html,可以关了eureka的自我保护模式eureka.server.enableSelfPreservation=false

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdsjd.html