SpringCloud搭建netflix-eureka微服务集群的过程详解
作者:追极 发布时间:2023-09-02 18:11:52
1.打开官网稍微学习一下,了解一下spring cloud是个什么东西,大概有哪些组件等
https://spring.io/projects/spring-cloud
https://docs.spring.io/spring-cloud-netflix/docs/current/reference/html/
2.新建项目
打开网址:https://start.spring.io/
选择需要引入的组件,然后下载下来即可
3.更改项目结构
为了测试的方便,需将项目结构更改为多模块的项目。
步骤如下:
(1) 依次新建子模块register-center/provider/consumer,删除父模块中多余的src、target等文件夹
(2) 修改父模块的pom文件:仅保留<dependencyManagement>配置节,<dependencies>配置节全部注释掉,因为可在子模块按需添加依赖。
(3) 修改register-center的pom中的依赖配置
<dependencies>
<!-- Eureka注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
(4) 修改provider和consumer的pom中依赖配置
<dependencies>
<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>
</dependencies>
4.新建相应的测试类和配置文件
4.1 register-center模块
启动类
package com.hdwang.springcloudtest.registercenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Application {
/**
* 运行点对点模式(集群模式)时,通过添加VM参数启动不同的注册中心节点实例
* 例如:-Dspring.profiles.active=peer2
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
yml配置
spring:
application:
#应用名
name: register-center
freemarker:
template-loader-path: classpath:/templates/
prefer-file-system-access: false
#激活的配置,可运行时添加参数进行修改 -Dspring.profiles.active=peer2
profiles:
active: peer1
# #Eureka独立模式配置,仅有一个注册中心节点
#server:
# port: 8090
#eureka:
# instance:
# hostname: localhost
# client:
# #仅仅作为注册中心,既不提供服务也不订阅服务
# registerWithEureka: false
# fetchRegistry: false
# #注册中心地址
# serviceUrl:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# Eureka点对点模式,保证注册中心高可用,注册的实例信息会在点与点之间相互同步
eureka:
client:
serviceUrl:
defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/
---
#每个注册中心节点不同的配置
spring:
profiles: peer1
server:
port: 8091
eureka:
instance:
#在本机hosts中配置即可
hostname: peer1.com
---
spring:
profiles: peer2
server:
port: 8092
eureka:
instance:
hostname: peer2.com
---
spring:
profiles: peer3
server:
port: 8093
eureka:
instance:
hostname: peer3.com
4.2 provider模块
启动类
package com.hdwang.springcloudtest.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
/**
* 运行时添加VM参数: -Dserver.port=8082可以启动多个provider实例
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
服务注册类
package com.hdwang.springcloudtest.provider.restservice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 注册的服务
*/
@RestController
public class RestService {
/**
* 日志
*/
private static final Logger LOG = LoggerFactory.getLogger(RestService.class);
@RequestMapping("/sayHello")
public String sayHello(String name) {
LOG.info("sayHello was called");
return "hello, " + name;
}
}
yml配置
spring:
application:
#应用名,也是eureka的服务名
name: provider
freemarker:
template-loader-path: classpath:/templates/
prefer-file-system-access: false
server:
#运行时,添加参数-Dserver.port=8082运行新的provider实例
port: 8081
eureka:
client:
#注册中心地址
serviceUrl:
#注册中心独立模式
#defaultZone: http://localhost:8090/eureka/
#注册中心点对点模式
defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/
4.3 consumer配置
启动类
package com.hdwang.springcloudtest.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
服务调用测试类
package com.hdwang.springcloudtest.consumer.controller;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.*;
/**
* 测试
*/
@RestController
public class TestController {
/**
* 使用服务名才能负载均衡,不能使用直接使用地址
*/
private static final String REST_URL_PREFIX = "http://provider";
@Autowired
private EurekaClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/testGet")
public String testGet() {
ResponseEntity<String> res = restTemplate.getForEntity(REST_URL_PREFIX + "/sayHello?name={1}", String.class, getName());
return res.getBody();
}
@GetMapping("/testPost")
public String testPost() {
MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
params.add("name", getName());
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity< MultiValueMap<String, Object>> request = new HttpEntity<>(params, null);
ResponseEntity<String> res = restTemplate.postForEntity(REST_URL_PREFIX + "/sayHello", request, String.class);
return res.getBody();
}
private String getName() {
List<String> greetings = Arrays.asList("Bob", "Alice", "Jack");
Random rand = new Random();
int randomNum = rand.nextInt(greetings.size());
return greetings.get(randomNum);
}
}
RestTemplate负责均衡配置类
package com.hdwang.springcloudtest.consumer.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;
@Configuration
public class RestTemplateConfig {
/**
* 构建有负载均衡功能的RestTemplate实例对象
*
* @return RestTemplate实例对象
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
yml配置
spring:
application:
#应用名,也是Eureka的服务名
name: cosumer
server:
port: 8088
eureka:
client:
#注册中心地址
serviceUrl:
#注册中心独立模式
#defaultZone: http://localhost:8090/eureka/
#注册中心点对点模式
defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/
5.运行测试
5.1本机hosts配置
127.0.0.1 peer1.com
127.0.0.1 peer2.com
127.0.0.1 peer3.com
5.2 编辑运行配置
三个注册中心节点运行配置
两个服务提供者的运行配置
5.3 运行程序
(1) 启动注册中心
依次启动RegisterCenter1->RegisterCenter2->RegisterCenter3,启动成功后,可访问http://localhost:8091/ 或http://localhost:8092/ 或http://localhost:8093/ 查看是否启动成功
(2)启动服务提供者provider
依次启动Provider1->Provider2, 随便访问一个注册中心地址首页即可查看状态,如下图
(3) 启动消费者cosumer
(4) 在浏览器中进行测试
测试地址:http://localhost:8088/testPost / http://localhost:8088/testGet
(5) 在Provider1/Provider2的控制台中可以看到输出结果
2021-04-07 15:26:56.043 INFO 8796 --- [nio-8081-exec-1] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:26:58.860 INFO 8796 --- [nio-8081-exec-2] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:26:59.535 INFO 8796 --- [nio-8081-exec-3] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:26:59.925 INFO 8796 --- [nio-8081-exec-4] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:27:00.266 INFO 8796 --- [nio-8081-exec-5] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:27:00.663 INFO 8796 --- [nio-8081-exec-6] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:27:00.938 INFO 8796 --- [nio-8081-exec-7] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:26:58.602 INFO 17828 --- [nio-8082-exec-1] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:26:59.194 INFO 17828 --- [nio-8082-exec-2] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:26:59.737 INFO 17828 --- [nio-8082-exec-3] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:27:00.109 INFO 17828 --- [nio-8082-exec-4] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:27:00.414 INFO 17828 --- [nio-8082-exec-5] c.h.s.provider.restservice.RestService : sayHello was called
2021-04-07 15:27:00.815 INFO 17828 --- [nio-8082-exec-6] c.h.s.provider.restservice.RestService : sayHello was called
恭喜!至此,Spring Clound 微服务集群框架您已经搭建成功!
附录
github地址:https://github.com/hdwang123/springcloudtest
参考文章:
https://www.zhihu.com/question/283286745/answer/763040709
https://www.cnblogs.com/qdhxhz/p/9357502.html
https://www.cnblogs.com/cjsblog/p/8005766.html
https://blog.csdn.net/weixin_44448094/article/details/88535475
来源:https://www.cnblogs.com/hdwang/archive/2021/04/07/14628372.html


猜你喜欢
- Mybatis-plus全局id生成策略在配置文件中加入以下代码后就不需要在实体类种的id上添加@TableId(value = “id”,
- 本文实例总结了C#配置文件Section节点处理方法。分享给大家供大家参考。具体如下:很多时候在项目开发中,我们都需要用配置文件来存储一些关
- 一个专门实现访问sql server数据库增删改查的操作代码,分享给大家,具体内容如下using System;using System.C
- 本文实例讲述了java用接口、多态、继承、类计算三角形和矩形周长及面积的方法。分享给大家供大家参考。具体如下:定义接口规范:/** * @
- 前言当我们通过前端向后端发起一个请求调用后端接口时,经常会遇到404的问题。网上关于对404问题介绍的一大堆,其实404问题的本质就两点。在
- 本文实例讲述了C#自定义类型强制转换的用法。分享给大家供大家参考。具体分析如下:先来举一个小例子类定义:public class MyCur
- 背景近期因实际项目需要,在特定操作下触发定位请求,取到用户位置及附近位置。问题:经初步选型,最终决定接入百度定位,按照百度定位SDK And
- 赋值运算符也有和算数操作符所结合的用法之前附录中有提及,用法是:比如要将x加上4,然后再赋值给x,就可以写成x+=4. public cla
- 写在前面从Java 1.0开始,引入java.io包;到Java 1.4再扩展了java.nio包;再到java 1.7又添加了新的流类,使
- 本文实例为大家分享了Java猜拳游戏的具体代码,供大家参考,具体内容如下先来看一下效果图: 首先我们创建一个Person类,这个类
- 首先什么是注解?@Override就是注解,它的作用是:1、检查是否正确的重写了父类中的方法。 2、标明代码,这是一个重写的方法。1、体现在
- 前言上文讲的MyBatis部署运行且根据官网运行了一个demo:一步到位部署运行MyBatis3源码<保姆级>jdbc再贴一个J
- 模板方法模式模板方法模式法(Template Method)定义一个操作中的算法骨架,而将算法的一些步骤延迟到子类中,使得子类可以不改变该算
- 初学Android编程,Android SDK中提供的Sample代码自然是最好的学习材料。 
- 默认情况下Spring Boot使用了内嵌的Tomcat服务器,项目最终被打成jar包运行,每个jar包可以被看作一个独立的Web服务器。传
- 本文实例讲述了C#实现闪动托盘图标效果的方法。分享给大家供大家参考,具体如下:在用户正在登录QQ或者使用Firemail邮件系统自动收取邮件
- 本文介绍Android平台进行数据存储的五大方式,分别如下:1 使用SharedPreferences存储数据2 文件存储数据 &
- 前几篇主要集中在注册中心eureka的使用上,接下来可以创建服务提供者provider来注册到eureka。demo源码见: https:/
- 1 前言为什么我们在使用SpringBoot框架开发Java Web应用需要引入大量的starter?例如,我们引入Redis就在Maven
- 今天来了一个问题:软键盘无法弹出。分析后是因为系统判断当前有外接硬键盘,就会隐藏软键盘。但实际情况并不是这么简单,该问题只有在特定条件下偶现