Spring Cloud(一) — 服务注册中心-注册服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25406669/article/details/85612888
  • SpringClod简介

    首先,pring Cloud是基于Spring Boot的, 最适合用于管理Spring Boot创建的各个微服务应用。
    Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,主节点选举, 分布式session, 集群状态)。协调分布式环境中各个系统,为各类服务提供模板性配置。使用Spring Cloud, 开发人员可以搭建实现了这些样板的应用,并且在任何分布式环境下都能工作得非常好,小到笔记本电脑, 大到数据中心和云平台。
    Spring Cloud使用erureka server, 然后所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。

  • 创建一个pom类型的maven工程 作为父目录(com-springcloud-demo-all):
    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>

    <groupId>com.springcloud.demo</groupId>
    <artifactId>com-springcloud-demo-all</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--<modules>-->
        <!--<module>springcloud-eurekaService</module>-->
        <!--<module>springcloud-eurekaclient-A</module>-->
        <!--<module>spring-cloud-config-server</module>-->
        <!--<module>spring-cloud-config-client</module>-->
        <!--<module>spring-cloud-service-ribbon</module>-->
        <!--<module>springcloud-eurekaclient-b</module>-->
        <!--<module>spring-cloud-service-feign</module>-->
        <!--<module>spring-cloud-monitor-turbine</module>-->
        <!--<module>spring-cloud-service-zuul</module>-->
        <!--<module>spring-cloud-config-client-two</module>-->
    <!--</modules>-->
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
                <exclusions>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 创建Eureka Server

1.在父目录下创建一个eureka server的maven的项目 springcloud-eurekaService 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>com-springcloud-demo-all</artifactId>
        <groupId>com.springcloud.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

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

2.在resources文加下创建配置文件application.yml:

server:
  port: 8761
spring:
  profiles:
    active: dev
eureka:
  server:
    enable-self-preservation: false
  instance:
    preferIpAddress: true
    hostname: ${spring.cloud.client.ipAddress}
    instanceId: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    registerWithEureka: false  # 其中registerWithEureka:false和fetchRegistry:false 表明自己是一个eureka server
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

创建启动类ServerApplication:

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

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

4.项目接口目录如下:
项目目录

5.启动eureka server,然后访问http://localhost:8761, 界面如下, “No instances available” 表示无client注册
服务

  • 创建Eureka Client

1.创建一个Maven工程springcloud-eurekaclient-a, 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>com-springcloud-demo-all</artifactId>
        <groupId>com.springcloud.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eurekaclient-a</artifactId>

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

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

2.创建主类EurekaClientApplication
使用@EnableEurekaClient注解表明是client

package com.eurekaservice.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

contrller类:

package com.eurekaservice.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${server.port}")
    private int port;

    @RequestMapping(value = "/index")
    public String index(){
        return "Hello World!,端口:"+port;
    }
}

3.eureka client的配置文件appication.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
     lease-renewal-interval-in-seconds: 1
     appname: eurekaclient-helloworld
#      leaseRenewalIntervalInSeconds: 5 #客户端每隔n秒向服务端发送数据包
#      leaseExpirationDurationInSeconds: 10 #客户端告知服务端:若在n秒内没有向服务器发送信息,则服务端将其从服务列表中删除
server:
  port: 8762
spring:
  application:
    name: eurekaclient-helloworld

#配置日志级别
logging:
  level:
    com.netflix: DEBUG

4.Client启动后, 可以访问http://localhost:8762
client
5.再次访问服务器端口, 可以看到Service Helloworld已经自动注册到之前的server中
服务注册

这样就完成了 服务向服务中心的注册。

猜你喜欢

转载自blog.csdn.net/qq_25406669/article/details/85612888
今日推荐