Springcloud 整理笔记记录 -eureka

    技术2022-07-14  68

    Eureka : 定义 : Netflix Eureka 是一款由Netflix开源的基于REST服务的注册中心,用于提供服务发现功能,Spring cloud Eureka是Spring Cloud Netflix微服务套案件的一部分,基于 Netflix Eureka 进行二次封装,主要负责完成 微服务架构的 服务治理功能。 组成 : Eureka Server , Eureka Client ( Application Service ,Application Client )

    功能点: 1. eureka server 注册中心,注册表数据结构为一个map,完全基于内存,采用多级缓存机制,

    运行流程:

    客户端启动后,会向注册中心发送请求,进行注册(保存自己的服务器信息)客户端 默认30秒,发送心跳给 注册中心,如果长时间不续约才默认90s 内会被 剔除。eureka 集群采用相互注册的方式(通过复制,保证数据同步),实现高可用集群。(保证AP,即保证高可用) private final ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> registry = new ConcurrentHashMap();

    服务端

    Eureka Server , (IDEA创建 spring boot 项目,选择下方依赖 )

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- 监控管理 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

    bootstrap.yml

    spring: application: name: kite-eureka-center cloud: inetutils: ## 网卡设置 ignoredInterfaces: ## 忽略的网卡 - docker0 - veth.* - VM.* preferredNetworks: ## 优先的网段 - 192.168

    application.yaml

    server: port: 3000 eureka: instance: hostname: eureka-center #主机名,不配置的时候将根据操作系统的主机名来获取 appname: register-center #服务名,默认取spring.application.name的配置值 lease-expiration-duration-in-seconds: 30 # 心跳超时时间,默认90s client: registerWithEureka: false # 表示是否将自己注册到Eureka Server,默认为true fetchRegistry: false # Eureak Server获取注册信息,默认为true, (需要同步其他的Eureka Server节点的数据时 true) serviceUrl: defaultZone: http://localhost:3000/eureka #设置与Eureka Server交互的地址 server: enableSelfPreservation: true #是否开启自我保护。 #evictionIntervalTimerInMs: 4000 #驱逐下线的服务,间隔,5秒,默认是60 启动类,标识 EurekaServer import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @author hexiaoshu 开启服务发现 */ @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }

    启动项目, 访问 http://localhost:3000/,进入Eureka 管理UI界面。

    客户端

    创建spring boot 项目,选择依赖。 pom ↓

    <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 监控管理 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>

    yaml

    server: port: 3001 eureka: instance: preferIpAddress: true #是否优先使用IP地址作为主机名的标识 instance-id: ${spring.cloud.client.ip-address}:${server.port} #实例名称 client: serviceUrl: defaultZone: http://localhost:3000/eureka #服务中心访问地址 healthcheck: #改变eureka server对客户端健康检测的方式,改用actuator的/health端点来检测。 enabled: true info: # eureka UI管理界面,点击实例详情设置 app.name: eureka-client company.name: hexiaoshu spring: # 应用名称 application: name: single-provider

    启动类:

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @author 54347 */ @EnableEurekaClient @SpringBootApplication public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }

    Feign (声明式 http客户端) 客户端服务之间调用

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>

    消费端 Application Client :

    /** * @program: client-customer * @description: @FeignClient(" 应用名称 ") @RequestMapping(value = " 该应用服务的访问地址 ") * @author: hexiaoshu **/ @FeignClient("single-provider") public interface IHelloService { @RequestMapping(value = "/hello") String hello(); @RequestMapping(value = "nice") String nice(); } @RestController public class ConsumerController { @Resource private IHelloService helloService; @RequestMapping(value = "feignRequest") public Object feignRequest(){ String s = helloService.nice(); return s; } }

    服务端 Application Service

    @Slf4j @RestController public class TestController { @Resource private DiscoveryClient discoveryClient; @RequestMapping(value = "/hello") public String hello(){ List<String> services = discoveryClient.getServices(); for(String s : services){ log.info(s); } return "hello spring cloud!"; } @RequestMapping(value = "/nice") public String nice(){ List<String> services = discoveryClient.getServices(); for(String s : services){ log.info("gogogo" + s); } return "nice to meet you!"; } }
    Processed: 0.011, SQL: 9