圖例介紹
流程介紹
以購(gòu)買(mǎi)商品為例:
- 消費(fèi)者(想買(mǎi)籃球的消費(fèi)者)發(fā)起購(gòu)買(mǎi)的請(qǐng)求。
- 這個(gè)請(qǐng)求先到服務(wù)發(fā)現(xiàn)者(euraka:可以理解為管理所有的服務(wù)的系統(tǒng)),服務(wù)發(fā)現(xiàn)者尋找并返回對(duì)應(yīng)的商家(providerA,下文舉例子是陽(yáng)光籃球場(chǎng))。
- 消費(fèi)者就自動(dòng)的發(fā)起請(qǐng)求到商家(陽(yáng)光籃球場(chǎng))。
- 商家返回一個(gè)籃球,并印有商家(陽(yáng)光籃球場(chǎng))的標(biāo)識(shí)。
具體代碼編寫(xiě)流程
一、先創(chuàng)建一個(gè)Eureka-Server服務(wù)注冊(cè)中心(服務(wù)的注冊(cè)和發(fā)現(xiàn)模塊)
創(chuàng)建一個(gè)項(xiàng)目(用的是idea開(kāi)發(fā)工具)
(1) 如圖進(jìn)行選擇和配置
其實(shí)就是引入了eureka的包:spring-cloud-starter-netflix-eureka-server
(2) 代碼調(diào)整,使它真正變成eureka服務(wù)器
Spring-Boot工程的啟動(dòng)類(lèi)上加 @EnableEurekaServer 注解。
(3) 配置調(diào)整
將application.properties 改為 application.yml,并修改內(nèi)容
server:
port: 9999 #服務(wù)注冊(cè)中心端口號(hào)
eureka:
instance:
hostname: 127.0.0.1 #服務(wù)注冊(cè)中心IP地址
client:
registerWithEureka: false #是否向服務(wù)注冊(cè)中心注冊(cè)自己
fetchRegistry: false #是否檢索服務(wù)
serviceUrl: #服務(wù)注冊(cè)中心的配置內(nèi)容,指定服務(wù)注冊(cè)中心的位置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
(4) 啟動(dòng)看效果
在instances部分就是將來(lái)會(huì)注冊(cè)到euraka上的服務(wù)商和消費(fèi)者。但現(xiàn)在并沒(méi)有,因?yàn)檫€沒(méi)創(chuàng)建呢。
二、先創(chuàng)建一個(gè)服務(wù)者(并注冊(cè)到eureka上)
(1)創(chuàng)建一個(gè)項(xiàng)目,跟創(chuàng)建euraka項(xiàng)目大致選擇一樣,就不上圖了。只有選擇依賴(lài)的時(shí)候,多選一個(gè)
spring web。因?yàn)榉?wù)者要提供服務(wù)將來(lái)要用到controller這種注解。
(2)修改代碼。
使其變成eureka的客戶端:在Spring-boot的啟動(dòng)類(lèi)上通過(guò)注解@EnableEurekaClient 表明自己是一個(gè)eurekaclient.
提供服務(wù):寫(xiě)一個(gè)controller,并編寫(xiě)具體接口。
package com.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class BasketballController {
/**
* 假如這個(gè)客戶端要提供一個(gè)getUser的方法
* @return
*/
@GetMapping(value = "/buy")
@ResponseBody
public Map<String,Object> getUser(){
Map<String,Object> data = new HashMap<>();
// data.put("id",id);
data.put("size","7#");
data.put("from","陽(yáng)光籃球場(chǎng)");
return data;
}
}
(3)配置調(diào)整
將application.properties 改為 application.yml,并修改內(nèi)容
eureka:
client:
serviceUrl: #注冊(cè)中心的注冊(cè)地址
defaultZone: http://127.0.0.1:9999/eureka/
server:
port: 8081 #服務(wù)端口號(hào)
spring:
application:
name: service-provider #服務(wù)名稱(chēng)--調(diào)用的時(shí)候根據(jù)名稱(chēng)來(lái)調(diào)用該服務(wù)的方法
(4) 啟動(dòng)看效果
eureka上也已經(jīng)有了一個(gè)客戶端了:
三、該來(lái)人買(mǎi)籃球了:創(chuàng)建一個(gè)消費(fèi)者(并注冊(cè)到eureka上)
(1)創(chuàng)建一個(gè)項(xiàng)目,選擇與服務(wù)者一樣。要選擇eureka和spring web,因?yàn)橐惨l(fā)起請(qǐng)求。
(2)修改代碼,使其變成eureka的客戶端并可以調(diào)用服務(wù)。(我在里面加了其他的注解,喜歡研究的可以嘗試的去掉看看有沒(méi)有影響)
package com.eureka.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableEurekaClient
@EntityScan
@ServletComponentScan
@ComponentScan
@RestController
public class ConsumerApplication {
@Autowired
RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
/**
* Rest服務(wù)端使用RestTemplate發(fā)起http請(qǐng)求,然后得到數(shù)據(jù)返回給前端
*
* @return
*/
@GetMapping(value = "/getUser")
@ResponseBody
public Map<String, Object> getUser() {
Map<String, Object> data = restTemplate.getForObject("http://service-provider/buy", Map.class);
return data;
}
}
(3)別忘了修改配置文件,使其注冊(cè)到eureka上。
eureka:
client:
serviceUrl: #注冊(cè)中心的注冊(cè)地址
defaultZone: http://127.0.0.1:9999/eureka/
server:
port: 9000 #服務(wù)端口號(hào)
spring:
application:
name: service-consumer #服務(wù)名稱(chēng)--調(diào)用的時(shí)候根據(jù)名稱(chēng)來(lái)調(diào)用該服務(wù)的方法
(4)啟動(dòng)看效果
eureka上已經(jīng)有了這個(gè)instance了。
發(fā)起買(mǎi)籃球的請(qǐng)求看一下,注意地址:是9000,是這個(gè)消費(fèi)者。之前的服務(wù)者是8081。返回陽(yáng)光籃球場(chǎng)。
四、eureka其實(shí)還可以實(shí)現(xiàn)簡(jiǎn)單的負(fù)載均衡:如果有多個(gè)籃球商進(jìn)行賣(mài)籃球,那消費(fèi)者去買(mǎi)的時(shí)候能均分到各個(gè)籃球商。實(shí)現(xiàn)負(fù)載均衡比較簡(jiǎn)單。
下一篇寫(xiě)eureka的簡(jiǎn)單負(fù)載均衡。