SpringCloud Netflix Eureka Server入门及集群

做一个sample,微服务间应用调用,首先创建父工程将几个微服务聚合maven版本管理。 推荐几个gitee上的精致demo,主要github太卡了。

参考sample

https://gitee.com/forezp/SpringCloudLearning -- 方志朋 随书教程

https://gitee.com/didispace/SpringCloud-Learning -- 程序员dd

https://gitee.com/exclusiver/springcloud2020/tree/master -- 跟敲教程代码

https://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi_spring-cloud.html Spring cloud 官方文档 强推!

SpringCloud Springboot版本关系

https://spring.io/projects/spring-cloud 附录 springcloud和springboot版本关联。这里选用2.0.6.Release的springboot,对应就是Finchley。

1. 创建父工程

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>SpringCloudDemo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka-server-01</module>
        <module>client-order</module>
        <module>client-stock</module>
        <module>eureka-server-02</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. 创建eureka server 01单机模式

new module eureka-server-01 主要参考 How to run a eureka server 创建 EurekaServerApplication.java 启动类, 这个文件不能直接放java文件夹下不然会报

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
警告:你的应用上下文可能没有启动,因为你将注解添加到了默认的package上面了

因为会扫当前main方法的包,启动类没有包就报错,新建package里放进去即可。先开始Standalone Mode 单机模式

package org.xl.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xl
 * @description: gzy的项目
 * @date 2022/2/13 6:35 PM
 */
@SpringBootApplication
@EnableEurekaServer
@RestController
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

配置文件 /resource/application.yml

server:
  port: 7001


eureka:
  instance:
    hostname: server7001   #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false   #false表示自己端就是注册中心
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
  application:
    name: cloud-eureka-service

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloudDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server-01</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--后续版本由spring-cloud-starter-eureka-server改成spring-cloud-starter-netflix-eureka-server-->
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

启动工程后访问 localhost:7001 若出现控制台则说明启动成功。

2. 创建eureka server 02 集群模式

High Availability, Zones and Regions The Eureka server does not have a backend store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory). Clients also have an in-memory cache of eureka registrations (so they don’t have to go to the registry for every single request to a service). By default every Eureka server is also a Eureka client and requires (at least one) service URL to locate a peer. If you don’t provide it the service will run and work, but it will shower your logs with a lot of noise about not being able to register with the peer.

就是说Eureka没有后台存储,eureka服务端同时也是客户端,可以互相注册,遵从AP原则。

创建module,eureka-server-02 pom.xml文件和EurekaServerApplication同上

新增/resource/application.yml

server:
  port: 7002

eureka:
  instance:
    hostname: server7002   #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false   #false表示自己端就是注册中心
    service-url:
      defaultZone: http://server7001/eureka/
spring:
  application:
    name: cloud-eureka-service

修改 server-01的application.yml文件

server:
  port: 7001


eureka:
  instance:
    hostname: server7001   #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false   #false表示自己端就是注册中心
    service-url:
      defaultZone: http://server7002/eureka/
spring:
  application:
    name: cloud-eureka-service

eureka.client.service-url.defaultZone 配置为对方,形成互补。如果大于两个呢?官网文档也提供了配置方法,就是将所有域都配置进去

运行,可看到 Got 1 instances from neighboring DS node

2022-02-13 23:54:41.901  INFO 2566 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
2022-02-13 23:54:41.901  INFO 2566 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2022-02-13 23:54:41.901  INFO 2566 --- [      Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2022-02-13 23:54:41.913  INFO 2566 --- [      Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-02-13 23:54:41.954  INFO 2566 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7001 (http) with context path ''
2022-02-13 23:54:41.955  INFO 2566 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7001
2022-02-13 23:54:41.960  INFO 2566 --- [           main] org.xl.demo.EurekaServerApplication      : Started EurekaServerApplication in 22.837 seconds (JVM running for 23.974)
2022-02-13 23:55:41.907  INFO 2566 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms

附录,eureka 配置

https://docs.spring.io/spring-cloud-netflix/docs/3.0.4/reference/html/appendix.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章