springboot-cloud-1-eureka

来源:互联网 发布:java 编码 编辑:程序博客网 时间:2024/05/29 18:03
   github地址:https://github.com/github-ygy/spring_cloud_test整体学习思路:    1.服务提供者在启动时,向注册中心注册自己提供的服务。    2.服务消费者在启动时,向注册中心订阅自己所需的服务。    3.注册中心返回服务提供者地址给消费者。    4.服务消费者从提供者的地址中调用服务。

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>test.ygy.eureka</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>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.6.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <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>Dalston.SR2</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka-server</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <optional>true</optional>        </dependency>        <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>

开启服务端

    服务提供者,将服务注册到服务注册中心,其它消费者则可以通过服务注册中心发现服务,获取服务并消费。
@EnableEurekaServer  //开启服务注册中心@SpringBootApplicationpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}

application.properties

spring.application.name=server####配置访问端口server.port = 9999eureka.instance.hostname=localhost#####不需要注册eureka.client.register-with-eureka=false#####不需要检索服务eureka.client.fetch-registry=false###eureka 自我保护###eureka.server.enable-self-preservation=false###eureka client间隔多久去拉取服务注册信息  默认30秒###eureka.client.registry-fetch-interval-seconds=30###eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间 默认90秒###eureka.instance.lease-expiration-duration-in-seconds=10###eureka client发送心跳给server端的频率 默认30秒###eureka.instance.lease-renewal-interval-in-seconds=5###清理无效节点的时间间隔###eureka.server.eviction-interval-timer-in-ms=0

client

开启客户端

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

application.properties

spring.application.name= clientserver.port=8888eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/

server分片

hosts

127.0.0.1 pear1127.0.0.1 pear2

application-server1.properties

server.port=9998eureka.instance.hostname=test1spring.application.name=servereureka.client.serviceUrl.defaultZone=http://test1:9998/eureka/,http://test2:9999/eureka/eureka.server.enable-self-preservation=false  //关闭保护机制###eureka.instance.prefer-ip-address= true  //默认false 不适用ip

application-server2.properties

server.port=9998eureka.instance.hostname=test1spring.application.name=servereureka.client.serviceUrl.defaultZone=http://test1:9998/eureka/,http://test2:9999/eureka/eureka.server.enable-self-preservation=false###eureka.instance.prefer-ip-address= true  //默认false 不适用ip

application-client.properties

spring.application.name= clientserver.port=8888eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka/,http://pear2:9999/eureka/