快速搭建基本的 SpringCloud 项目:
能够基于此文档快速实现 SpringCloud 的基本搭建,集成 Eureka注册中心,实现注册与发现,并实现远程过程调用的具体实现
通过此项目代码实现核心代码的封装:
因博主之前老板有过一个需求,是为了防止新员工将公司代码拷贝走,然后自己使用的问题,
为了防止此情况,有以下两种方案:
a. 可以将原有的 SpringBoot 项目参照以下步骤 改造 成 SpringCloud 的项目,只给新员工 非核心 的代码,
b. 如果觉得改造成 SpringCloud 项目麻烦,也可以将核心代码拆分出来,形成一个新的项目,然后通过 http 的方式调用,且将接口使用秘钥的方式加密校解密校验接口
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.2.RELEASEversion>
<relativePath/>
parent>
<groupId>com.xyygroupId>
<artifactId>springcloudtestartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>springcloudtestname>
<description>Demo project for Spring Bootdescription>
<modules>
<module>mastermodule>
<module>coremodule>
modules>
<properties>
<java.version>1.8java.version>
<spring-cloud.version>Greenwich.RELEASEspring-cloud.version>
<spring-cloud-eureka.version>2.1.0.RELEASEspring-cloud-eureka.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
<version>${spring-cloud-eureka.version}version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>${spring-cloud-eureka.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.5.3version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
<repository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/snapshoturl>
<releases>
<enabled>falseenabled>
releases>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
<pluginRepository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/snapshoturl>
<releases>
<enabled>falseenabled>
releases>
pluginRepository>
pluginRepositories>
project>
<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.0modelVersion>
<groupId>com.xyygroupId>
<artifactId>eureka-serverartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>eureka-servername>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.5.RELEASEversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
<version>2.1.0.RELEASEversion>
dependency>
dependencies>
project>
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}
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);
}
}
<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.0modelVersion>
<parent>
<groupId>com.xyygroupId>
<artifactId>springcloudtestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.xyygroupId>
<artifactId>coreartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>corename>
<description>Demo project for Spring Bootdescription>
project>
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秒后执行
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;
}
}
<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.0modelVersion>
<parent>
<groupId>com.xyygroupId>
<artifactId>springcloudtestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.xyygroupId>
<artifactId>masterartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>mastername>
<description>Demo project for Spring Bootdescription>
project>
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秒后执行
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);
}