今天要做的就是這一個部分,微服務的核心就是一個應用柔後有相互讀˙力的 微服務組成,不同的服務可以透過一些輕量級通訊協定來進行溝通 比如說 RPC, HTTP 等,服務可以獨立擴展生說,每個服務可以用個別不同的的語言進行開發。
Provider
Consumer
比較常見的有
用於獲取地址列表。它既可以是靜態的(提供一組固定的地址),也可以是動態的(從註冊中心中定期查詢地址列表)。
僅當使用動態ServerList時使用,用於在原始的服務列表中使用一定策略過慮掉一部分地址。
選擇一個最終的服務地址作為LB結果。選擇策略有輪詢、根據響應時間加權、斷路器(當Hystrix可用時)等。
Ribbon在工作時首選會通過ServerList來獲取所有可用的服務列表,然後通過ServerListFilter過慮掉一部分地址,最後在剩下的地址中通過IRule選擇出一台服務器作為最終結果。
以輪詢的方式依次將請求調度不同的服務器,即每次調度執行i = (i + 1) mod n,並選出第i台服務器。
隨機選擇狀態為UP的Server
根據相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。
複合判斷server所在區域的性能和server的可用性選擇server
這邊我們要產生兩個 Service
兩個 Service 配置檔只有 port 不同如下
application.yml;
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-provider
server:
port: 8081 // 另一個 設為8082
EurekaServiceProviderApplication.java
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaServiceProviderApplication {
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "Hello world" + port;
}
public static void main(String[] args) {
SpringApplication.run(EurekaServiceProviderApplication.class, args);
}
}
pom.xml
<--eureka 客戶端-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-server
</artifactId>
</dependency>
<--Ribbon 客戶端-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-ribbon
</artifactId>
</dependency>
EurekaServiceRibbonConsumer.java
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaServiceRibbonConsumer {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaServiceRibbonConsumer.class, args);
}
}
這邊 Ribbon 會去跟 Eureka 取得目前註冊列表目前可用的清單
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/hello")
public String hello() {
return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();
}
}
Eureka Server - > Service Provider *2 - > Ribbon Consumer
依序啟動後
Ribbon 預設策略為輪巡
大家可以試試看兩個 Provider 全關 是不是會出現轉圈圈,這個地方可以用下一章就是我們要用斷路器來避開這種問題。
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing