SpringCloud学习笔记(一)服务注册

接触SpringCloud很久了总结一下

 项目结构

主项目 pom文件

<?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>spring-cloud</groupId>
    <artifactId>eureka</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>
    <!-- 统一项目字符集 -->
    <!--jdk版本-->
    <!-- spring-cloud版本 -->
    <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.RELEASE</spring-cloud.version>
    </properties>
<!--在父项目的POM文件中,我们会使用到dependencyManagement元素。通过它来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在dependencyManagement元素中指定的版本号。-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

然后构建eureka-server

pom文件

<?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>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>spring-cloud</groupId>
        <artifactId>eureka</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <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</artifactId>
        </dependency>

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

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

</project>

然后写eurake-server的配置文件application.yml

server:
    port: 8761
    enableSelfPreservation: false
eureka:
    instance:
       hostname: localhost
    client:
      register-with-eureka: false
      fetch-registry: false
      service-url:
           default-zone:
              http://${eurake.instance.hostname}:${server.port}/eurake/

#服务注册中心端口号

server.port=8761

#服务注册中心实例的主机名

eureka.instance.hostname=localhost

#是否向服务注册中心注册自己

eureka.client.register-with-eureka=false

#是否检索服务

eureka.client.fetch-registry=false

#服务注册中心的配置内容,指定服务注册中心的位置

eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

#是否关闭自我保护模式
server.enableSelfPreservation= false

当有一个新的 Eureka Se凹er 出现时,它尝试从相邻 Peer 节点获取所有服务实例注册表信 息。如果从相邻的 Peer 节点获取信息时出现了故障, Eureka Server 会尝试其他的 Peer 节点。 如果 Eureka Serve 能够成功获取所有的服务实例信息,则根据配置信息设置服务续约的阀值。 在任何时间,如果 Eureka Serve 接收到的服务续约低于为该值配置的百分比(默认为 15 分钟 内低于 85%),则服务器开启自我保护模式,即不再剔除注册列表的信息。 这样做的好处在于,如果是 Eureka Server 自身的网络问题而导致 Eureka Cli阳it 无法续约, Eureka Client 的注册列表信息不再被删除,也就是 Eureka Client 还可以被其他服务消费。 

因为我们目前只有一个节点所以先关闭,不然会有警告。

然后是工程启动类


@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

启动 EurekaClientApplication

Eureka Server 的所有搭建工作已经完成。

接着是配置 Eureka-client

pom文件和Eureka-server一样,注意名字是eureka-client

关键是配置文件 bootstrap.yml

eureka:
   client:
     service-url:
       default-zone: http://localhost:8761/eureka/
server:
   port: 8762
spring:
   application:
      name: eureka-client

bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等

application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

bootstrap.yml 先于 application.yml 加载

#http://localhost:8761/eureka/ 是client向server注册服务的地址

client启动类

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

启动client

控制台打印出

看server控制台

说明已经注册成功

再次访问注册中心

参考资料:

           《深入理解Spring+Cloud与微服务构建》

猜你喜欢

转载自blog.csdn.net/qq_33543634/article/details/82225115
今日推荐