第一章、SpringCloud注册于发现代码案例

203 阅读2分钟

一、文档说明:

       SpringCloud的案例比较复杂,很多案例都是相互的,以后编写的文档也会基于这个文档进行讲解。为了方便项目管理我们搭建一个空项目,以后的所有项目(module)案例都放到这个空项目里面。

创建空项目方法步骤如下:

1、创建一个空文件夹如下图:

2、用idea打开即可

下面开始正式学习SpringCloud

二、EurekaServer

1.1、创建maven工程

选择空项目创建一个maven工程

选择maven next

依次填写组名 项目名和版本号 next进入下一步

下一步

创建成功如下图

1.2、pom依赖

打开eurekaserver下的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>springcloud</groupId>
    <artifactId>01_eurekaserver</artifactId>
    <version>1.0-SNAPSHOT</version>

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

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

    </dependencies>

</project>

1.3、创建yml文件

yml文件内容如下:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false
spring:
  application:
    name: eurka-server

1.4、创建启动类

1.5、启动类编写

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurecaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurecaServerApplication.class,args);
    }
}

1.6、最后项目目录结构如下图:

1.7、浏览器访问测试

运行main方法启动EUREKASERVER,然后访问下面地址

访问地址:http://localhost:8761

看到上面效果说明启动成功

三、EurekaClient

1、创建maven工程

创建过程省略 

2、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>springcloud</groupId>
    <artifactId>02_eurekaclient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

3、application.yml文件

server:
  port: 8762
spring:
  application:
    name: EURAKACLIENT
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4、启动类

package com;
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 );
    }
}

5、测试是否注册成功

分别启动两个服务、EurekaServer和EurecaClient

访问: http://localhost:8761

6、创建controller

       为了下面的学习我们再创建一个controller类作为一个服务提供者,对外暴露一个restfull接口,让其他服务组件调用。这个类将会在第二章节中使用到,当前文档没有用到。

package com.controller;

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

@RestController
public class EurekaClientController {
    @Value("${server.port}")
    String port;

    @RequestMapping("/testClient")
    public String testClient(){
        return "我是EurekaClient,我被成功访问了,当前服务的端口号为: "+port;
    }
}