项目文件:下载 206KB,可以用于 对比文件 及 目录结构,提前 查看效果,上文传送地址:SpringCloud 聚合项目 IDEA 创建 。
Eureka
一、创建父工程 LinZeCloud。
父工程已经创建过了,不懂的 前往上一篇 ,也可直接下载上一篇项目文件:下载 200KB,下载后就不用搭了。
然后删除其他的文件,如下图就OK 。
1.1 工程创建。
1.2 文件配置。
1.2.0 编辑 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">
<!-- 子工程引用 -->
<modules>
<!-- <module>linze-publics</module> -->
</modules>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.lz</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>12</java.version>
<spring-cloud.version>Hoxton.RC1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.4</version>
</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>
</repository>
</repositories>
</project>
二、创建子工程 linze-publics 公共文件。
注:用于存放公共文件。
2.1 工程创建。
2.2 创建目录与文件。
2.2.0 编辑 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>cn.lz</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>publics</artifactId>
<dependencies>
</dependencies>
</project>
2.2.1 创建 src/main/java/cn/lz/cloud/publics 目录,如下图所示。
2.2.2 创建 Bugs 文件,内容如下。
package cn.lz.cloud.publics.vo;
/**
* Created by IntelliJ IDEA.
*
* @author NingZe
* description:
* path: LinZeCloud-cn.lz.cloud.publics.vo-Bugs
* date: 2019/11/13 0013 15:11
* version: 02.06
* To change this template use File | Settings | File Templates.
*/
public class Bugs {
private String bid;
private String bname;
private String bprice;
private String bport;
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBprice() {
return bprice;
}
public void setBprice(String bprice) {
this.bprice = bprice;
}
public String getBport() {
return bport;
}
public void setBport(String bport) {
this.bport = bport;
}
public Bugs() {
}
public Bugs(String bid, String bname, String bprice, String bport) {
this.bid = bid;
this.bname = bname;
this.bprice = bprice;
this.bport = bport;
}
}
三、创建子工程 linze-eureka-server 服务注册中心。
注:用于注册各种微服务,以便于其他微服务找到和访问。
3.1 工程创建。
3.2 文件配置。
3.2.0 编辑 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>cn.lz</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.2.1 编辑 application.yml。
# 开发环境配置。
server:
# 注册中心的端口。
port: 8761
# 注册中心配置。
eureka:
instance:
# 主机名称。
hostname: localhost
client:
# 是否注册到服务器。因为它本身就是服务器,所以就无需把自己注册到服务器了。
registerWithEureka: false
# 是否获取服务器的注册信息,和上面同理,这里也设置为 false。
fetchRegistry: false
serviceUrl:
# http://${eureka.instance.hostname}:${server.port}/eureka/ 自己作为服务器,公布出来的地址。
# 比如后续某个微服务要把自己注册到 eureka server, 那么就要使用这个地址:http://localhost:8761/eureka/
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
# 微服务的名称 eureka-server
name: eureka-server
3.2.2 编辑 EurekaServerApplication。
@EnableEurekaServer 表示这是个 EurekaServer 服务注册中心 。
package cn.lz.cloud.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
到此服务注册中心创建完成,启动 EurekaServerApplication 后,访问 http://127.0.0.1:8761/ ,如下图。
四、创建子工程 linze-data-service 数据微服务。
注:注册数据微服务到服务中心。
4.1 工程创建。
4.2 创建文件。
4.2.0 编辑 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>cn.lz</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>data-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
4.2.1 编辑 application.yml。
# server.port 就不设置了,因为要启动多个端口,如 8001、8002、8003 ...... 。
spring:
application:
# 数据微服务名称
name: linze-data-service
# 注册到服务中心
eureka:
client:
serviceUrl:
# 设置注册中心的地址: http://localhost:8761/eureka/ , 与 eureka-server 中的配置 application.yml 遥相呼应
defaultZone: http://localhost:8761/eureka/
4.2.2 创建 BugService 服务。
package cn.lz.cloud.dataservice.service;
import cn.lz.cloud.publics.vo.Bugs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @author NingZe
* description:
* path: LinZeCloud-cn.lz.cloud.dataservice.service-BugsService
* date: 2019/11/13 0013 16:21
* version: 02.06
* To change this template use File | Settings | File Templates.
*/
@Service
public class BugService {
/**
* 端口号,用于区分数据来源
*/
@Value("${server.port}")
String port;
public List<Bugs> listBugs() {
return new ArrayList<>() {{
add(new Bugs("1", "无尽之刃", "3400", "" + port));
add(new Bugs("2", "斯塔缇克电刃", "2600", "" + port));
add(new Bugs("3", "无尽之刃", "3400", "" + port));
add(new Bugs("4", "无尽之刃", "3400", "" + port));
add(new Bugs("5", "饮血剑", "3700", "" + port));
add(new Bugs("6", "多米尼克领主的致意", "2600", "" + port));
}};
}
}
鼠标放到此处 alt + enter 添加模块就好了。
然后在 pom 里可以看到,自动引用了模块。
4.2.3 创建 BugsController 控制器。
@RestController == @Controller + @ResponseBody
package cn.lz.cloud.dataservice.controller;
import cn.lz.cloud.dataservice.service.BugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by IntelliJ IDEA.
*
* @author NingZe
* description:
* path: LinZeCloud-cn.lz.cloud.dataservice.controller-BugsController
* date: 2019/11/14 0014 15:18
* version: 02.06
* To change this template use File | Settings | File Templates.
*/
@RestController
public class BugsController {
@Autowired
BugsService bs;
@RequestMapping("bugs")
public Object products() {
return bs.listBugs();
}
}
4.2.4 编辑 pom.xml。
表示这是个web服务,会提供控制层,自动配置模块。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4.2.5 编辑 DataServiceApplication。
@EnableEurekaClien 表示注册成为客户端。
package cn.lz.cloud.dataservice;
import cn.hutool.core.net.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class DataServiceApplication {
public static void main(String[] args) {
// 因为要启动多个,例如 8001、8002、8003 ......
// 所以采用自动切换端口,去启动,启动的时候我们等待第一个启动完成后,在启动第二个
int port = 8001;
for (int i = 1; i <= 9; i++) {
port = Integer.parseInt(800 + "" + i);
// 端口是否占用
if (!NetUtil.isUsableLocalPort(port)) {
System.err.printf("端口%d被占用了,无法正常启动,正在更换端口,请稍等片刻...... %n", port);
if (port == 8009) {
System.exit(1);
}
continue;
}
break;
}
System.out.printf("端口%d,正在启动中...... %n", port);
new SpringApplicationBuilder(DataServiceApplication.class).properties("server.port=" + port).run(args);
}
}
添加两个 数据微服务。
我们挨个启动,EurekaServerApplication、DataServiceApplication8001、DataServiceApplication8002。
访问 http://127.0.0.1:8761/ ,如下图。
访问 http://127.0.0.1:8001/bugs/ ,http://127.0.0.1:8002/bugs/ ,如下图。
到此数据微服务就注册完了,这边就不用数据微服务做视图操作了,等待下一步与视图结合展示。也就是 微服务之间的访问 。
五、创建子工程 linze-view-service-feign 视图微服务。
注:注册视图微服务到服务中心。
Feign 是对 Ribbon 的封装,使用注解的方式,调用起来更简单,也是主流的方式。
5.1 工程创建。
5.2 文件配置。
5.2.0 编辑 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>cn.lz</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>view-service-feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>cn.lz</groupId>
<artifactId>publics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
5.2.1 编辑 application.yml。
# server.port 就不设置了,因为要启动多个端口,如 8011、8012、8013 ...... 。
# Spring的配置
spring:
application:
name: linze-view-service-feign
# 模板引擎配置
profiles: default
freemarker:
template-loader-path: classpath:/templates/views/ftl/
cache: false
check-template-location: true
content-type: text/html; charset=UTF-8
expose-request-attributes: true
expose-session-attributes: true
request-context-attribute: request
suffix: .ftl
# 注册到服务中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
5.2.2 创建 BugsClientFeign 客户端。
(LINZE-DATA-SERVICE) 是 数据服务在 eureka 注册中心的名称。
将会通过数据服务的注册名称去调用对应的接口。比如是 bugs ,那么访问的就是 http://127.0.0.1/[8001、8002]/bugs/ 的链接。
package cn.lz.cloud.viewservicefeign.client;
import cn.lz.cloud.publics.vo.Bugs;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @author NingZe
* description:
* path: LinZeCloud-cn.lz.cloud.viewservicefeign.client-BugsClientFeign
* date: 2019/11/11 0011 11:27
* version: 02.06
* To change this template use File | Settings | File Templates.
*/
@FeignClient("LINZE-DATA-SERVICE")
public interface BugsClientFeign {
@GetMapping("bugs")
List<Bugs> listBugs();
}
5.2.3 创建 BugsService 服务。
package cn.lz.cloud.viewservicefeign.service;
import cn.lz.cloud.publics.vo.Bugs;
import cn.lz.cloud.viewservicefeign.client.BugsClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @author NingZe
* description:
* path: LinZeCloud-cn.lz.cloud.viewservicefeign.service-BugsService
* date: 2019/11/14 0014 15:04
* version: 02.06
* To change this template use File | Settings | File Templates.
*/
@Service
public class BugsService {
@Autowired
BugsClientFeign bugsClientFeign;
public List<Bugs> listBugs() {
return bugsClientFeign.listBugs();
}
}
5.2.4 创建 BugsController 控制器。
package cn.lz.cloud.viewservicefeign.controller;
import cn.lz.cloud.viewservicefeign.service.BugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by IntelliJ IDEA.
*
* @author NingZe
* description:
* path: LinZeCloud-cn.lz.cloud.viewservicefeign.controller-BugsController
* date: 2019/11/14 0014 15:10
* version: 02.06
* To change this template use File | Settings | File Templates.
*/
@Controller
public class BugsController {
/**
* 端口号,用于区分视图来源
*/
@Value("${server.port}")
String port;
@Autowired
BugsService bs;
@RequestMapping("bugs")
public String bugs(Model model) {
model.addAttribute("bugs", bs.listBugs());
model.addAttribute("port", port);
return "bugs";
}
}
5.2.5 创建目录 /templates/views/ftl/ 及 bugs.ftl 模板。
<#--
Created by IntelliJ IDEA.
User: NingZe
Date: 2019/11/13 0013
Time: 09:39
To change this template use File | Settings | File Templates.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bugs</title>
</head>
<style>
table {
border-collapse: collapse;
width: 450px;
margin: 20px auto;
height: 235px;
font-family: SimSun;
font-size: 14px;
}
td, th {
border: 1px solid gray;
}
</style>
<body>
<table>
<tr>
<th>装备编号</th>
<th>装备名称</th>
<th>装备价格</th>
<th>数据微服务端口</th>
</tr>
<#list bugs as b>
<tr>
<th>${b.bid}</th>
<th>${b.bname}</th>
<th>${b.bprice}</th>
<th>${b.bport}</th>
</tr>
</#list>
<tr>
<th align="center" colspan="2">
视图微服务端口
</th>
<th align="center" colspan="2">
${port}
</th>
</tr>
<tr>
<th align="center" colspan="4">
${.now}
</th>
</tr>
</table>
</body>
</html>
5.2.6 编辑 ViewServiceFeignApplication。
@EnableEurekaClien,表示注册成为客户端。
@EnableDiscoveryClient, 表示用于发现 eureka 注册中心的微服务。
@EnableFeignClients, 表适用于启用 feign 客户端。
package cn.lz.cloud.viewservicefeign;
import cn.hutool.core.net.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ViewServiceFeignApplication {
public static void main(String[] args) {
// 因为要启动多个,例如 8111、8012、8013 ......
// 所以采用自动切换端口,去启动,启动的时候我们等待第一个启动完成后,在启动第二个
int port = 8011;
for (int i = 1; i <= 9; i++) {
port = Integer.parseInt(801 + "" + i);
if (!NetUtil.isUsableLocalPort(port)) {
System.err.printf("端口%d被占用了,无法正常启动,正在更换端口,请稍等片刻...... %n", port);
if (port == 8019) {
System.exit(1);
}
continue;
}
break;
}
System.out.printf("端口%d,正在启动中...... %n", port);
new SpringApplicationBuilder(ViewServiceFeignApplication.class).properties("server.port=" + port).run(args);
}
}
添加一个 视图微服务。
我们挨个启动,
EurekaServerApplication 服务注册中心
DataServiceApplication8001、DataServiceApplication8002 数据服务
ViewServiceFeignApplication8011视图服务
访问 http://127.0.0.1:8011/bugs 如下图。
多刷新几遍,就会发现这个 数据微服务端口 有时候是 8001,有时候是 8002。
从而观察到访问 数据服务 集群,客户端 负载均衡 的效果。
注:视图微服务的话我们也可以多启动一个,可以做到 集群,但现在无法实现 负载均衡。
下一篇 [ zuul 网关 ] 将讲解如何将 多个数据微服务 与 多个视图微服务 分别整合在两个服务地址进行 负载均衡。
六、总结与拓展。
绘图工具:亿图,安装及破解教程。
6.1 执行流程图。
如下图所示:
1.首先 数据微服务 和 视图微服务 都被 eureka 管理起来了。
2.数据服务 是由 两个实例 的 集群 组成的,端口分别是 8001、8002。
3.视图微服务 是通过 注册中心 调用 微服务, 然后 负载均衡 到 8001 或者 8002 端口的应用上。
本文案例的简要执行流程。
从图中可以看到, view.BugsClientFeign 的方法名为 listBugs 而 data.BugsController 的方法名为 products,
注意这是根据请求的映射名称(/bugs)映射到某个特定的方法(listBugs),也就是映射名称(/bugs),这个的话是必须一样的。
6.2 请求方式。
对了,还是建议把 数据微服务 项目中的 BugsController 改一下,强迫症表示不舒服。
@RestController
public class BugsController {
@Autowired
BugsService bs;
@RequestMapping("bugs")
public Object listBugs() {
return bs.listBugs();
}
}
如果要在以上 controller 中 加上一层映射名。
@RequestMapping(“bc”)
@RequestMapping("bc")
@RestController
public class BugsController {
@Autowired
BugsService bs;
@RequestMapping("bugs")
public Object listBugs() {
return bs.listBugs();
}
}
那就要修改 视图微服务 项目中的 BugsClientFeign,而不是 BugsController,因为请求数据的时候是 BugsClientFeign 去访问的 。
方式一
@RequestMapping("bc")
@FeignClient("LINZE-DATA-SERVICE")
public interface BugsClientFeign {
@GetMapping("/bugs")
List<Bugs> listBugs();
}
方式二
@FeignClient("LINZE-DATA-SERVICE")
public interface BugsClientFeign {
@GetMapping("bc/bugs")
List<Bugs> listBugs();
}