第一篇:springCloud 服务的注册与发现

一. spring Cloud 简介

        spring cloud 基于 spring boot , 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、时间总线、全局锁、决策锁、分布式会话等。

       基于spring cloud 开发的应用程序特别适合在Docker或者其他专业平台Paas(平台即服务,如Cloud Foundry)部署,所以又称作原生云应用(Cloud Native Application)。

二. 创建服务注册中心

      在这里,我们需要在用到的组件上Spring Cloud Netflix的Eureka , eureka 是一个服务注册模块。

       2.1 首先创建一个maven主工程

       2.2 然后创建2个model工程,一个model工程为注册中心,即Eureka Server ,另一个作为Eureka Client 

             先创建一个server,以server为例,详细说明创建过程:

             右键工程 -> 创建mould -> 选择spring initialir ,如下图

下一步 -> 选择 Spring Cloud Config -> Config Server,然后一直下一步就可以了

 

 创建完后的工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaServer</name>
    <description>Demo project for Spring Cloud Eureka</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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.3 启动一个服务注册中心

      只需要一个注解 @EnableEurekaServer ,这个注解在spring boot 工程的启动类上加,即可

/**
 * 启动一个服务注册中心,只需要一个注解:@EnableEurekaServer
 * 这个注解需要在spring boot 工程的启动 application类上添加
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

2.4 Eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,(可以在内存中完成),在默认的情况下,eureka server 也是一个eureka client ,因此必须要指定一个server。eureka server的配置文件如下:

server:
  port: 9001

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
通过eureka.client.registerWithEureka:false 和fetchRegistry:false来表明自己是一个eureka server

2.5 eureka server 是有界面的,启动工程,打开浏览器访问:

      http://localhost:9001,界面如下:

因为没有服务来注册,所以显示为:No instances available  

 

三. 创建一个服务提供者(eureka client)

当 client 向 server 注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个 client 实例接收心跳消息。 如果心跳超时,则通常将该实例从注册 server 中删除。

创建过程同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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eurekaclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>config
    <name>eurekaClient</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
            <version>2.2.1.RELEASE</version>
        </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>

 通过注解@EnableEurekaClient ,标明自己是一个  eureka client 

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

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

    @Value("${server.port}")
    String port;
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi "+name+",i am from port:" +port;
    }

}

还需要在配置文件中报名自己的服务注册中心地址,application.yml 配置文件如下:

server:
  port: 9002

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/

spring:
  application:
    name: service-hi

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个 name 。
启动工程,打开http://localhost:9001 ,即eureka server 的网址:

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

这时打开 http://localhost:9002/hi?name=Tom ,你会在浏览器上看到 :

hi Tom,i am from port:9002

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值