Perfree

SpringCloud (一) : 服务注册与发现
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代...
扫描右侧二维码阅读全文
17
2019/02

SpringCloud (一) : 服务注册与发现

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

注册中心

本篇文章源码地址case-01-start

创建springboot工程,选中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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.perfree</groupId>
    <artifactId>spring-cloud-start</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>spring-cloud-start</name>
    <description>spring-cloud-start</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</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>
        </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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
  1. 开启一个eureka服务

在启动类添加@EnableEurekaServer注解,代表为一个服务注册组件

声明一个server

eureka是一个高可用的组件,在默认情况下erureka server也是一个eureka client ,必须要指定一个server,在application.properties配置如下:

#端口号
server.port=8881
#Eureka实例所在的主机名
eureka.instance.hostname=127.0.0.1
#是否在eureka服务器上注册自己的信息以供其他服务发现,server端不需要,所以写成false,默认true
eureka.client.register-with-eureka=false
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
eureka.client.fetch-registry=false

启动并访问

像普通springboot项目那样启动我们的服务,访问http://127.0.0.1:8881/

因为还没有服务来注册,所以是还没有服务被发现~

创建一个服务提供者

创建步骤同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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.perfree</groupId>
    <artifactId>spring-cloud-client</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>spring-cloud-client</name>
    <description>spring-cloud-client</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</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>
        </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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

声明自己是一个eurekaclient

在启动类添加@EnableEurekaClient注解

application.properties配置文件

#端口号
server.port=8882
#应用名称
spring.application.name=myClient
#服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8881/eureka

启动服务,观察变化

当我们启动之后,再来观察http://127.0.0.1:8881/ 页面,如下

你会发现一个服务已经注册在服务中了,服务名为MYCLIENT,端口为8882

接下来,我们添加一个controller,提供一个hello的接口:
新建controller包,在里边新建HelloController类,如下:

package com.perfree.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    @RequestMapping("/hello")
    public String hello() {
        return "Hello";
    }
}

可以看到,该接口只返回了Hello字符串,接下来我们创建服务消费者进行调用

服务消费者

为了让我们更能够体会到Eureka所发挥的作用,我们下面来构建一个服务消费者,该服务消费者将调用spring-cloud-client所提供的服务。

创建项目

勾选如下:

完整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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.perfree</groupId>
    <artifactId>spring-cloud-ribbon</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>spring-cloud-ribbon</name>
    <description>spring-cloud-ribbon</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

spring-cloud-starter-ribbon,该子项目提供客户端负载均衡功能,可以自动从Eureka服务器中获取服务提供者的地址列表,从而能够发起相应的调用

application.properties配置文件

#端口号
server.port=8883
#应用名称
spring.application.name=myRibbon
#服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8881/eureka

启动类注入RestTemplate

package com.perfree;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudRibbonApplication.class, args);
    }
    
    //注入RestTemplate
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

编写HelloController调用服务Hello接口

package com.perfree.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
    
    @Autowired
    private RestTemplate restTemplate;
    
    @RequestMapping("/")
    public String hello() {
        return restTemplate.getForEntity("http://127.0.0.1:8882/hello", String.class).getBody()+" Spring Cloud";
    }
}

启动ribbon项目

启动成功之后可以看到注册中心:

我们接着访问http://127.0.0.1:8883/

已经调用成功~

Last modification:February 17th, 2019 at 05:41 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment