GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 更新 IDEA 的十八般武艺 和 Eureka 基本原理,本地集群环境搭建。 · lzy1985/PassJava-Learning@e4c1b9d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • 110.Temp
      • 02.Win10升级到Win11.md
    • 13.SpringCloudNetflix
      • 01.Eureka基本原理和简单使用.md
      • 02.Eureka本地集群搭建.md
    • 88.Interview/05.DesignPattern
      • 06.单例模式.md
    • 99.tools
      • 06.Idea的十八般武艺.md
      • 10.IDEA插件.md
    • _navbar.md
    • _sidebar.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/110.Temp/02.Win10升级到Win11.md‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
![mark](http://cdn.jayh.club/blog/20211001/4KvUeQf4dcox.png?imageslim)
2+
3+
4+
5+
![mark](http://cdn.jayh.club/blog/20211001/GcH5DiCgBaxJ.png?imageslim)
6+
7+
8+
9+
![mark](http://cdn.jayh.club/blog/20211001/FUnf8IEpYcKK.png?imageslim)
10+
11+
12+
13+
![mark](http://cdn.jayh.club/blog/20211001/L4JDT8LxdriG.png?imageslim)
14+
15+
16+
17+
![mark](http://cdn.jayh.club/blog/20211001/F7lhOp5aC0X2.png?imageslim)
18+
Collapse file

‎docs/13.SpringCloudNetflix/01.Eureka基本原理和简单使用.md‎

Copy file name to clipboard
+262Lines changed: 262 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# Eureka 服务注册与发现的原理和简单使用
2+
3+
## 一、基本原理
4+
5+
现在有一个订单服务部署在一台机器上,另外有两个商品服务部署在两台机器上。
6+
7+
所有服务都注册到 eureka 上,当订单服务想要调用商品服务时,其实是从 eureka 上获取商品服务的地址。
8+
9+
![](http://cdn.jayh.club/blog/20211004/DjwfE5UOXRLG.png?imageslim)
10+
11+
当有一个商品服务宕机后,eureka 会把这个服务的注册信息移除掉,订单服务也不会调用这个商品服务。
12+
13+
![](http://cdn.jayh.club/blog/20211004/cWLSnC2YevIH.png?imageslim)
14+
15+
下面我们简单使用下 Eureka。
16+
17+
> 场景:首先有一个服务 A,一个服务 B,服务 B 不知道服务 A 的 IP 地址,只知道服务 A 的名字,服务 B 通过向 eureka 获取到了服务 A 的具体 IP 地址,然后成功调用服务 A 的 passjava API。
18+
19+
## 二、创建 Eureka Server
20+
21+
创建一个 Eureka 服务。
22+
23+
Spring Cloud 基于 Hoxton.SR10 版本,Spring Boot 基于 2.3.2.RELEASE。
24+
25+
### pom.xml 文件
26+
27+
```
28+
<parent>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-parent</artifactId>
31+
<version>2.3.2.RELEASE</version>
32+
</parent>
33+
34+
<dependencyManagement>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-dependencies</artifactId>
39+
<version>Hoxton.SR10</version>
40+
<type>pom</type>
41+
<scope>import</scope>
42+
</dependency>
43+
</dependencies>
44+
</dependencyManagement>
45+
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.springframework.cloud</groupId>
49+
<artifactId>spring-cloud-starter</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.cloud</groupId>
53+
<artifactId>spring-cloud-starter-config</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.cloud</groupId>
57+
<artifactId>spring-cloud-starter-eureka</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework.cloud</groupId>
61+
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
62+
</dependency>
63+
64+
</dependencies>
65+
```
66+
67+
### 启动类
68+
69+
```java
70+
/**
71+
* @Author: 公众号 | 悟空聊架构
72+
* @Date: 2021/9/13 7:28
73+
* @Site: www.passjava.cn
74+
* @Github: https://github.com/Jackson0714/eureka-learning
75+
*/
76+
@SpringBootApplication
77+
@EnableEurekaServer
78+
public class EurekaServerApplication {
79+
public static void main(String[] args) {
80+
SpringApplication.run(EurekaServerApplication.class, args);
81+
}
82+
}
83+
```
84+
85+
### 配置项, application.yml 文件
86+
87+
```yaml
88+
server:
89+
port: 8761
90+
eureka:
91+
client:
92+
register-with-eureka: false
93+
fetch-registry: false
94+
```
95+
96+
register-with-eureka: 不要把自己注册到 Eureka,因为它自己就是 Eureka 服务,让别人来注册的
97+
98+
fetch-registry: 不要到 Euraka 服务抓取注册信息
99+
100+
### 启动
101+
102+
浏览器访问 http://localhost:8761/
103+
104+
![](http://cdn.jayh.club/blog/20211004/jgLJTJYsh0a0.png?imageslim)
105+
106+
## 三、创建服务 A
107+
108+
### pom.xml 文件
109+
110+
```
111+
<dependencyManagement>
112+
<dependencies>
113+
<dependency>
114+
<groupId>org.springframework.cloud</groupId>
115+
<artifactId>spring-cloud-dependencies</artifactId>
116+
<version>Hoxton.SR10</version>
117+
<type>pom</type>
118+
<scope>import</scope>
119+
</dependency>
120+
</dependencies>
121+
</dependencyManagement>
122+
123+
<dependencies>
124+
<dependency>
125+
<groupId>org.springframework.boot</groupId>
126+
<artifactId>spring-boot-starter-web</artifactId>
127+
</dependency>
128+
<dependency>
129+
<groupId>org.springframework.cloud</groupId>
130+
<artifactId>spring-cloud-starter-config</artifactId>
131+
</dependency>
132+
<dependency>
133+
<groupId>org.springframework.cloud</groupId>
134+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
135+
</dependency>
136+
</dependencies>
137+
```
138+
139+
### application.yml 配置
140+
141+
```yaml
142+
server:
143+
port: 8091
144+
spring:
145+
application:
146+
name: ServiceA
147+
eureka:
148+
instance:
149+
hostname: localhost
150+
client:
151+
serviceUrl:
152+
defaultZone: http://localhost:8761/eureka
153+
```
154+
155+
### Controller 类
156+
157+
```java
158+
@RestController
159+
public class ServiceAController {
160+
161+
@RequestMapping(value = "/passjava/{name}",
162+
method = RequestMethod.GET)
163+
public String sayHello(@PathVariable("name") String name) {
164+
165+
return "hello" + name;
166+
}
167+
}
168+
```
169+
170+
### 启动类
171+
172+
```java
173+
/**
174+
* @Author: 公众号 | 悟空聊架构
175+
* @Date: 2021/9/13 7:28
176+
* @Site: www.passjava.cn
177+
* @Github: https://github.com/Jackson0714/eureka-learning
178+
*/
179+
@SpringBootApplication
180+
@EnableEurekaClient
181+
public class ServiceAApplication {
182+
183+
public static void main(String[] args) {
184+
SpringApplication.run(ServiceAApplication.class, args);
185+
}
186+
187+
}
188+
```
189+
190+
### 测试服务 A 是否已启动
191+
192+
Eureka 控制台可以看到服务 A 已启动
193+
194+
![mark](http://cdn.jayh.club/blog/20211004/lIAgy7xryGJo.png?imageslim)
195+
196+
测试服务 A 是否能访问
197+
198+
```sh
199+
http://localhost:8005/passjava/悟空
200+
```
201+
202+
返回结果:
203+
204+
![](http://cdn.jayh.club/blog/20211004/Def2YPTLQyvA.png?imageslim)
205+
206+
## 四、创建服务 B
207+
208+
### 服务 B 和 服务 A 的区别
209+
210+
- 多了一个 netflix。
211+
212+
- 服务 B 会调用服务 A。
213+
214+
``` YAML
215+
<dependency>
216+
<groupId>org.springframework.cloud</groupId>
217+
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
218+
</dependency>
219+
```
220+
221+
### 服务 B 调用服务 A 的 passjava api
222+
223+
```java
224+
public class ServiceBController {
225+
226+
@Bean
227+
@LoadBalanced
228+
public RestTemplate getRestTemplate() {
229+
return new RestTemplate();
230+
}
231+
232+
@RequestMapping(value = "/test/{name}", method = RequestMethod.GET)
233+
public String test(@PathVariable("name") String name) {
234+
RestTemplate restTemplate = getRestTemplate();
235+
return restTemplate.getForObject("http://ServiceA/passjava/" + name, String.class);
236+
}
237+
238+
}
239+
```
240+
241+
我们看到有一个 @LoadBalanced 注解,这里做下说明:
242+
243+
RestTemplate 用来实现单个 http 请求的,加了 @LoadBalanced 注解后,就可以实现负载均衡,访问
244+
245+
### 测试
246+
247+
启动服务 B,可以在 eureka 控制台看到服务 B 在注册列表上。
248+
249+
![](http://cdn.jayh.club/blog/20211004/ibEDfRSS193f.png?imageslim)
250+
251+
测试访问 test api
252+
253+
```SH
254+
http://localhost:9001/test/悟空2
255+
```
256+
257+
测试结果:
258+
259+
![](http://cdn.jayh.club/blog/20211004/uHaR0OwdhnVc.png?imageslim)
260+
261+
说明服务 B 拿到了 服务 A 的 url 信息,然后成功调用服务 A。
262+
Collapse file

‎docs/13.SpringCloudNetflix/02.Eureka本地集群搭建.md‎

Copy file name to clipboard
+109Lines changed: 109 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Eureka 本地集群搭建
2+
3+
## 组成 Eureka 注册中心集群
4+
5+
### 修改本地 hosts 配置
6+
7+
host 文件添加两台 Eureka server 的地址映射,都为本地 localhost 地址
8+
9+
``` SH
10+
127.0.0.1 eureka-server1
11+
127.0.0.1 eureka-server2
12+
```
13+
14+
### 修改 Eureka Server 配置
15+
16+
两个服务互相向对方注册,第一个 Eureka 开放 8761 端口,注册到第二个 Eureka 的 8762 端口。
17+
18+
第一个 Eureka Server 的配置并启动
19+
20+
```yaml
21+
server:
22+
port: 8761
23+
eureka:
24+
instance:
25+
hostname: eureka-server1
26+
client:
27+
serviceUrl:
28+
defaultZone: http://eureka-server1:8762/eureka/
29+
```
30+
31+
修改第二个 Eureka Server 的配置,并启动
32+
33+
```yaml
34+
server:
35+
port: 8762
36+
eureka:
37+
instance:
38+
hostname: eureka-server2
39+
client:
40+
serviceUrl:
41+
defaultZone: http://eureka-server2:8761/eureka/
42+
```
43+
44+
![](http://cdn.jayh.club/blog/20211004/l8lhcAaOWQAG.png?imageslim)
45+
46+
47+
48+
访问 http://localhost:8761/
49+
50+
![](http://cdn.jayh.club/blog/20211004/IIPFaKu6awwe.png?imageslim)
51+
52+
访问 http://localhost:8762/
53+
54+
![mark](http://cdn.jayh.club/blog/20211004/IIPFaKu6awwe.png?imageslim)
55+
56+
两台 Eureka Server 组成了一个集群环境
57+
58+
## 组成服务集群
59+
60+
将服务 A 配置成注册到 Eureka 集群。在 defaultZone 配置中添加第二个 eureka 的服务地址。
61+
62+
```yaml
63+
server:
64+
port: 8005
65+
spring:
66+
application:
67+
name: ServiceA
68+
eureka:
69+
instance:
70+
hostname: localhost
71+
client:
72+
serviceUrl:
73+
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
74+
```
75+
76+
然后启动服务 A。
77+
78+
然后将服务 A 的 port 改为 8006 后,再次启动一个服务 A,相当于部署了两个服务 A,后续可以作为测试负载均衡的功能。
79+
80+
![](http://cdn.jayh.club/blog/20211004/jcEyhhLD9bEg.png?imageslim)
81+
82+
发现一个 Eureka 的问题,注册列表总是刷新不出最新的注册信息,重启 Eureka 服务后就刷出来了。
83+
84+
接着我们把服务 A 的passjava 方法,打下日志,后面观察两个服务 A 分别被服务 B 调用了几次。
85+
86+
## 修改服务调用者的配置
87+
88+
将服务 B 配置成注册到 Eureka 集群。在 defaultZone 配置中添加第二个 eureka 的服务地址。
89+
90+
两个 Eureka 服务中都有服务 B 的注册信息。
91+
92+
![](http://cdn.jayh.club/blog/20211004/OwTWvb0AQ9qt.png?imageslim)
93+
94+
## 测试服务 B 调用服务 A
95+
96+
多次刷新浏览器,访问服务 B 的 API
97+
98+
```sh
99+
http://localhost:9091/test/悟空
100+
```
101+
102+
第一个服务 A 被服务 B 调用了 6 次:
103+
104+
![](http://cdn.jayh.club/blog/20211004/a06Hdz9HsaL1.png?imageslim)
105+
106+
第二个服务A 被服务 B 调用了 7 次:
107+
108+
![](http://cdn.jayh.club/blog/20211004/N0BmHrDaPtV2.png?imageslim)
109+

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.