Spring Cloud Finchley版本Demo笔记,注册中心、服务注册、服务发现、和注册中心高可用

一、新建Maven,编写父pom.xml

1、打开idea,File->New->Project,新建maven项目:

2、不用选,直接next:

3、指定groupid和artifactid:

4、finish:

5、删除不必要的包,可以把src删掉:

6:、设置父pom.xml,并添加一个子模块eureka:

<?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.cobra</groupId>
    <artifactId>springcloud</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>SpringCloudDemo</name>
    <description>Demo project for Spring Cloud</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>eureka</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.RELEASE</spring-cloud.version>
    </properties>

    <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>

二、新建子模块,eureka注册中心:

1、new->module:

2、选择Spring Initializr:

3、指定groupid和artifactid,next:

4、选择web、eureka server:

5、next->finish得到:

6、修改修改eureka模块的pom.xml,继承父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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.cobra</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.cobra</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

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

</project>

7、修改配置文件后缀名为.yml,配置相关参数:

#端口号
server:
  port: 8761
spring:
  application:
    name: eureka
#hostname
eureka:
  instance:
    hostname: localhost
  client:
    #向自己注册,不显示自己等
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka

8、在启动类上加@EnableEurekaServer,声明自己是一个eureka注册中心:

9:启动eureka模块,并访问http://localhost:8761/

10:如果改配置文件里的register-with-eureka: true,重启则会显示自己:

三、服务注册:

1、新建模块server,选择的依赖为web、eureka discovery:

2、修改子模块的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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.cobra</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.cobra</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

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

</project>

3、并在父pom.xml添加对应module

4、将配置文件改为.yml,配置参数

#端口号
server:
  port: 8090
#服务名
spring:
  application:
    name: server
#向注册中心注册
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

5、在启动类上加@EnableEurekaClient,声明是个服务端:

6、刷新http://localhost:8761/,发现server注册成功:

四、注册中心高可用

注册中心是微服务中最重要环节,必须保证高可用,防止一台服务挂了,还有可用的注册中心,供服务消费者发现服务提供者:

高可用实现分两步:1)、启用多台注册中心,注册中心之间相互注册;2)、服务向这些启动的服务中心分别注册。

1、假设启用三台注册中心,新建application-server1.yml、application-server2.yml、application-server3.yml,

需要注意的是:

#  instance:
#    hostname: localhost

需要注释。

2、application.yml配置为:

spring:
  profiles:
    active: server1

application-server1.yml配置为:

#端口号
server:
  port: 8761
spring:
  application:
    name: eureka
#hostname
eureka:
#  instance:
#    hostname: localhost
  client:
    #向自己注册,不显示自己等
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/

application-server2.yml配置为:

#端口号
server:
  port: 8762
spring:
  application:
    name: eureka2
#hostname
eureka:
#  instance:
#    hostname: localhost
  client:
    #向自己注册,不显示自己等
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/

application-server3.yml配置为:

#端口号
server:
  port: 8763
spring:
  application:
    name: eureka3
#hostname
eureka:
#  instance:
#    hostname: localhost
  client:
    #向自己注册,不显示自己等
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

3、在父pom.xml添加<packaging>pom</packaging>,到eureka目录下执行mvn clean install将项目打包:

4、server配置不变的情况下(只向8761注册),会发现8762、8763也发现了server:

5、但是这并不够,当8761挂掉,虽然8762、8763还能发现sever,但是当sever重启,8762、8763里面的server依然存在,重启8762、8763就无法发现server了,因此还是有问题。

正确的做法应该是server也同时向多个注册中心注册:

#端口号
server:
  port: 8090
#服务名
spring:
  application:
    name: server
#向注册中心注册
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka,http://localhost:8763/eureka/

猜你喜欢

转载自blog.csdn.net/weixin_37138899/article/details/88409251