(新手向)完整的快速创建 SpringCloud 项目代码并集成 Eureka,且实现远程过程调用

概述

以下为完整的 SpringCloud 项目代码,使用此文档,您可有以下收获
1. 快速搭建基本的 SpringCloud 项目
2. 通过此项目代码实现核心代码的封装
收获详情:
  1. 快速搭建基本的 SpringCloud 项目:

    能够基于此文档快速实现 SpringCloud 的基本搭建,集成 Eureka注册中心,实现注册与发现,并实现远程过程调用的具体实现

  2. 通过此项目代码实现核心代码的封装:
    因博主之前老板有过一个需求,是为了防止新员工将公司代码拷贝走,然后自己使用的问题
    为了防止此情况,有以下两种方案:
    a. 可以将原有的 SpringBoot 项目参照以下步骤 改造 成 SpringCloud 的项目,只给新员工 非核心 的代码,
    b. 如果觉得改造成 SpringCloud 项目麻烦,也可以将核心代码拆分出来,形成一个新的项目,然后通过 http 的方式调用,且将接口使用秘钥的方式加密校解密校验接口

快速开始

创建父工程,引入对应依赖,删除 src 目录(父工程不需要)
完整父工程 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.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.xyy</groupId>
  <artifactId>springcloudtest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>springcloudtest</name>
  <description>Demo project for Spring Boot</description>

  <modules>
    <module>master</module>
    <module>core</module>
  </modules>

  <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    <spring-cloud-eureka.version>2.1.0.RELEASE</spring-cloud-eureka.version>
  </properties>

  <dependencies>
    <!-- springCloud -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>

    <!-- Eureka客户端 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      <version>${spring-cloud-eureka.version}</version>
    </dependency>

    <!-- feign远程调用,包含了ribbon负载均衡 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
      <version>${spring-cloud-eureka.version}</version>
    </dependency>

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

  </dependencies>
  <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>
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
    <enabled>false</enabled>
    </snapshots>
    </pluginRepository>
    <pluginRepository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
    <enabled>false</enabled>
    </releases>
    </pluginRepository>
    </pluginRepositories>

    </project>
创建 eureka-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>

  <groupId>com.xyy</groupId>
  <artifactId>eureka-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>eureka-server</name>
  <description>Demo project for Spring Boot</description>

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

  <!--eureka依赖-->
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      <version>2.1.0.RELEASE</version>
    </dependency>

  </dependencies>




</project>
application.yml 配置文件内容
server:
  port: 9000

eureka:
  instance:
    hostname: 127.0.0.1
  client:
    register-with-eureka: false   #表示是否向注册中心注册自己
    fetch-registry: false   #false表示自己为注册中心
    service-url: #配置客户端所交互的Eureka(监控页面)
      defaultZone: http://${eureka.instance.hostname}:${server.port}
启动类内容, 添加 @EnableEurekaServer 注解,声明此项目为 Eureka 的服务端
package com.xyy.eurekaserver;

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

@SpringBootApplication
@EnableEurekaServer // 启动 Eureka 服务
public class EurekaServerApplication {

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

}

启动项目测试, 注意访问地址为 application.yml 文件中 defaultZone 的配置,此处为:http://localhost:9000

image.png

创建 core(服务提供者)模块
完整 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>com.xyy</groupId>
    <artifactId>springcloudtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.xyy</groupId>
  <artifactId>core</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>core</name>
  <description>Demo project for Spring Boot</description>



</project>
application.yml 配置文件内容
server:
  port: 9002
spring:
  application:
    name: spring-cloud-boot-provide-core
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9000/eureka
  instance:
    prefer-ip-address: true # 当其它服务获取地址时提供ip而不是hostname
    ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
    lease-renewal-interval-in-seconds: 30 # 30秒检测一次心跳
    lease-expiration-duration-in-seconds: 90 # 最小过期时长,间隔90秒就准备剔除,之后60秒后执行
启动类内容,添加 @EnableDiscoveryClient 注解,声明模块为 Eureka 的客户端
package com.xyy.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
public class CoreApplication {

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

}
添加测试接口
package com.xyy.core.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/demo/{name}")
    public String demo(@PathVariable String name){
        return "Hello World!" + name;
    }
}
依次启动 eureka-server > core ,测试是否成功
idea 中的启动截图

image.png

Applicaiton 中发现,已经将服务提供者注册到 Eureka 注册中心了。

image.png

测试接口

image.png

创建 master (服务消费者) 模块
完整 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>com.xyy</groupId>
    <artifactId>springcloudtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.xyy</groupId>
  <artifactId>master</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>master</name>
  <description>Demo project for Spring Boot</description>


</project>
application.yml 配置文件内容
server:
  port: 9003
spring:
  application:
    name: spring-cloud-boot-consumer-master
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9000/eureka
  instance:
    prefer-ip-address: true # 当其它服务获取地址时提供ip而不是hostname
    ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
    lease-renewal-interval-in-seconds: 30 # 30秒检测一次心跳
    lease-expiration-duration-in-seconds: 90 # 最小过期时长,间隔90秒就准备剔除,之后60秒后执行
启动类内容,添加 @EnableDiscoveryClient 注册服务,添加 @EnableFeignClients 开启远程调用
package com.xyy.master;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
@EnableDiscoveryClient // 注册服务
@EnableFeignClients // 开启远程调用
public class MasterApplication {

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

}
添加远程过程调用接口
package com.xyy.master.controller;

import com.xyy.master.service.ServiceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {
    @Autowired
    ServiceFeignClient serviceFeignClient;

    @GetMapping("/demo/{name}")
    public String index(@PathVariable("name") String name) {
        return serviceFeignClient.demo(name);
    }
}
添加远程过程调用服务
package com.xyy.master.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "spring-cloud-boot-provide-core" )
public interface ServiceFeignClient {

    //这里接口要与被生产者demo一致
    @GetMapping("/demo/{name}")
    String demo(@PathVariable("name") String name);
}
依次启动 eureka-server > core > master 测试是否成功
可以看到 服务提供者 与服务消费者都注册到了 Eureka 中。

image.png

测试消费者接口,注意这里的测试地址是:http://localhost:9003/demo/测试远程过程调用 ,端口为消费者模块的端口

image.png

完整目录结构

image.png
文章参考链接:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员西柚柚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值