项目地址见文章底部
一、创建maven主工程
1、创建主 maven 工程 f-cloud
2、修改项目 maven 设置
3、由于国内访问中央仓库较慢,在 settings 配置文件中已修改为阿里云的仓库地址
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
4、在主项目的 pom.xml 中添加依赖,其他项目模块依赖此内容。
<modelVersion>4.0.0</modelVersion>
<groupId>com.fazi</groupId>
<artifactId>f-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>f-cloud</name>
<description>Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
maven 主工程搭建完成。
二、创建 eureka 注册中心 f-eureka
1、右键 f-cloud 项目,新建 f-eureka 模块
f-eureka 注册服务端创建完成。
2、修改 pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.fazi</groupId>
<artifactId>f-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>f-eureka</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.fazi</groupId>
<artifactId>f-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
在 f-cloud 主项目下的 pom.xml 中添加如下模块配置
// 和 parent 同级
<modules>
// 只添加一个
<module>f-eureka</module>
// 以下两个在后边创建完成生产者和消费者之后再添加
//<module>f-provide</module>
//<module>f-consumer</module>
</modules>
3、找到 f-eureka 项目主程序入口,添加注解 @EnableEurekaServer
4、在 resources 目录下新建 application.yml 文件(也可以使用 application.properties 文件,只是文件内容格式不同,详细可百度)
附:
(1)正常的情况是先加载yml,接下来加载properties文件。如果相同的配置存在于两个文件中。最后会使用properties中的配置。最后读取的优先集最高。
(2)两个配置文件中的端口号不一样会读取properties中的端口号。
并添加配置信息
server:
port: 8700
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceZone:
defaultZone: http://${euraka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: f-eureka
5、启动 FEurekaApplication.java 程序,并访问地址: http://localhost:8700/
此时 Eureka 服务端程序搭建完成。
3、搭建生产者模块 f-provide
1、基本步骤同 f-eureka,但是注意在 Spring Cloud Discovery 中选择 Eureka Discovery Client。
2、修改 pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.fazi</groupId>
<artifactId>f-provide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>f-provide</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.fazi</groupId>
<artifactId>f-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
// 注意此处的 Finchley.SR1, 和 spring boot 存在版本对应关系
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</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>
在 f-cloud 的 pom.xml 中把 <module>f-provide</module> 的注释去掉
3、copy f-eureka 模块下的 application.yml 到 f-provide 的 resources目录下,并修改内容为
server:
port: 8701
servlet:
context-path: /f-provide
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8700/eureka/
instance:
instance-id: f-provide
prefer-ip-address: true
spring:
application:
name: f-provide
4、打开 f-provide 项目入口 java类 FProvideApplication.java,并修改如下:
package com.fazi.fprovide;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
public class FProvideApplication {
public static void main(String[] args) {
SpringApplication.run(FProvideApplication.class, args);
}
@Value("${server.port}")
String port;
@GetMapping(value = "/fazi/get/{name}")
public String get(@PathVariable("name") String name){
return "你好啊" + name + ", port:" + port;
}
}
5、启动 f-provide 项目入口 FProvideApplication.java,并访问地址: http://localhost:8700/,看到多了 f-provide,表示成功。
6、访问地址: http://localhost:8701/f-provide/fazi/get/hello,如下则表示成功。
4、搭建消费者模块 f-consumer
1、基本步骤同 f-eureka,但是注意在 Spring Cloud Discovery 中选择 Eureka Discovery Client。
2、修改 pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.fazi</groupId>
<artifactId>f-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>f-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.fazi</groupId>
<artifactId>f-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</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>
在 f-cloud 的 pom.xml 中把 <module>f-consumer</module> 的注释去掉
3、copy f-eureka 模块下的 application.yml 到 f-provide 的 resources目录下,并修改内容为
server:
port: 8702
servlet:
context-path: /f-consumer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8700/eureka/
instance:
instance-id: f-consumer
prefer-ip-address: true
spring:
application:
name: f-consumer
4、打开 f-consumer 项目入口 java类 FConsumerApplication.java,添加注解 @EnableEurekaClient
5、启动 f-consumer 项目入口 FConsumerApplication.java,并访问地址: http://localhost:8700/,看到多了 f-consumer,表示成功。
5、消费者通过 Eureka 中暴露的接口访问服务。
1、在 f-consumer 下新建如下目录结构,并添加类
类的内容分别为
package com.fazi.fconsumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName ConfigBean
* @Description TODO
* @Author fazi
* @Date 2020/2/25 23:01
* @Version 1.0
**/
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced// Spring Cloud Ribbon 是基于 Netflix Ribbon 实现的一套客户端
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package com.fazi.fconsumer.controller;
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;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName ConsumerController
* @Description TODO
* @Author fazi
* @Date 2020/2/25 23:02
* @Version 1.0
**/
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://f-provide";
@GetMapping(value = "/getRest/{name}")
public String getRest(@PathVariable("name") String name){
return restTemplate.getForObject(REST_URL_PREFIX + "/f-provide/fazi/get/" + name, String.class);
}
}
2、重新启动 f-consumer 项目入口 FConsumerApplication.java,并访问地址:http://localhost:8702/f-consumer/getRest/hello
表示成功。
后续集群、其他 Spring Cloud 内容持续更新。
多关注,不迷路。
项目地址:https://gitee.com/biyanfa/f-cloud/tree/base/
公众号: