spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法
服務(wù)治理可以說是微服務(wù)架構(gòu)中最為核心和基礎(chǔ)的模塊,它主要用來實(shí)現(xiàn)各個(gè)微服務(wù)實(shí)例的自動(dòng)化注冊和發(fā)現(xiàn)。
Spring Cloud Eureka是Spring Cloud Netflix 微服務(wù)套件的一部分,主要負(fù)責(zé)完成微服務(wù)架構(gòu)中的服務(wù)治理功能。
本文通過簡單的小例子來分享下如何通過Eureka進(jìn)行服務(wù)治理:
- 搭建服務(wù)注冊中心
- 注冊服務(wù)提供者
- 服務(wù)發(fā)現(xiàn)和消費(fèi)
==========我是華麗的分割線========================
一、搭建服務(wù)注冊中心
先列出完整目錄結(jié)構(gòu):
搭建過程如下:
1.創(chuàng)建maven工程:eureka(具體實(shí)現(xiàn)略)
2.修改pom文件,引入依賴
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <!-- 使用dependencyManagement進(jìn)行版本管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka server依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
3.創(chuàng)建啟動(dòng)類
/** * * @EnableEurekaServer * 用來指定該項(xiàng)目為Eureka的服務(wù)注冊中心 */ @EnableEurekaServer @SpringBootApplication public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } }
4.配置application.properties文件
#設(shè)置tomcat服務(wù)端口號(hào) server.port=1111 #設(shè)置服務(wù)名稱 spring.application.name=eureka-service eureka.instance.hostname=localhost #注冊中心不需要注冊自己 eureka.client.register-with-eureka=false #注冊中心不需要去發(fā)現(xiàn)服務(wù) eureka.client.fetch-registry=false #設(shè)置服務(wù)注冊中心的URL eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
5.啟動(dòng)服務(wù)并訪問,我們會(huì)看到這樣的畫面:
二、注冊服務(wù)提供者
先列出完整目錄結(jié)構(gòu):
搭建過程如下:
1.創(chuàng)建maven工程:hello-service(具體實(shí)現(xiàn)略)
2.修改pom文件,引入依賴
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>hello-service</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka 客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
3.創(chuàng)建啟動(dòng)類
/*** * * @EnableDiscoveryClient * 讓服務(wù)使用eureka服務(wù)器 * 實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn) * */ @EnableDiscoveryClient @SpringBootApplication public class HelloApp { public static void main(String[] args) { SpringApplication.run(HelloApp.class, args); } }
4.創(chuàng)建controller
@RestController public class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired DiscoveryClient discoveryClient; @RequestMapping("/hello") public String hello() { ServiceInstance instance = discoveryClient.getLocalServiceInstance(); //打印服務(wù)的服務(wù)id logger.info("*********" + instance.getServiceId()); return "hello,this is hello-service"; } }
5.配置application.properties文件
server.port=9090 #設(shè)置服務(wù)名 spring.application.name=hello-service #設(shè)置服務(wù)注冊中心的URL,本服務(wù)要向該服務(wù)注冊中心注冊自己 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
6.啟動(dòng)并測試
1.)啟動(dòng)后再hello-service的控制臺(tái)會(huì)有這種字樣(xxx代表你的PC名)
eureka的控制臺(tái)會(huì)打印出如下字樣(xxx代表你的PC名)
2.)再次訪問localhost:1111,會(huì)發(fā)現(xiàn)有服務(wù)注冊到注冊中心了
三、服務(wù)發(fā)現(xiàn)和消費(fèi)
完整目錄結(jié)構(gòu)如下:
搭建過程:
1.創(chuàng)建maven工程(具體實(shí)現(xiàn)略)
2.修改pom文件,引入依賴
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka 客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 引入ribbon 依賴 ,用來實(shí)現(xiàn)負(fù)載均衡,我們這里只是使用,先不作其他介紹--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project>
這里比hello-service服務(wù)提供者,多了ribbon的依賴
3.創(chuàng)建啟動(dòng)類
@EnableDiscoveryClient @SpringBootApplication public class ConsumerApp { //@Bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean @Bean @LoadBalanced //@LoadBalanced實(shí)現(xiàn)負(fù)載均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
這里也要用到@EnableDiscoveryClient, 讓服務(wù)使用eureka服務(wù)器, 實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
4.創(chuàng)建controller
@RestController public class ConsumerController { //這里注入的restTemplate就是在com.sam.ConsumerApp中通過@Bean配置的實(shí)例 @Autowired RestTemplate restTemplate; @RequestMapping("/hello-consumer") public String helloConsumer() { //調(diào)用hello-service服務(wù),注意這里用的是服務(wù)名,而不是具體的ip+port restTemplate.getForObject("http://hello-service/hello", String.class); return "hello consumer finish !!!"; } }
5.配置application.properties文件
server.port=9999 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka #這里的配置項(xiàng)目和服務(wù)提供者h(yuǎn)ello-service一樣
6.啟動(dòng),測試1.)啟動(dòng)eureka。為了展示負(fù)責(zé)均衡的效果,我們的hello-service啟動(dòng)兩個(gè)服務(wù),啟動(dòng)兩個(gè)服務(wù)的具體步驟如下
以上是hello-service1的啟動(dòng)步驟,端口號(hào)為9090;同樣方法設(shè)置hello-service2,端口號(hào)為9091(具體實(shí)現(xiàn)略)。
2.)啟動(dòng)hello-consumer
3.)再次訪問http://localhost:1111/,會(huì)發(fā)現(xiàn)有2個(gè)hello-service服務(wù)(端口號(hào)一個(gè)是9090,一個(gè)是9091),1個(gè)hello-consume服務(wù)
4.) 多次訪問http://localhost:9999/hello-consumer,會(huì)發(fā)現(xiàn)hello-service1和hello-service2會(huì)輪流被調(diào)用(已經(jīng)實(shí)現(xiàn)了負(fù)責(zé)均衡),可以通過兩者的控制臺(tái)打印內(nèi)容確認(rèn)(還記得我們在hello-service的controller中有個(gè)loggerlogger.info("*********" + instance.getServiceId());嗎?對(duì),就是這個(gè)打印)
四、總結(jié)
以上實(shí)例實(shí)現(xiàn)了基本的服務(wù)治理:
- 通過spring-cloud-starter-eureka-server和@EnableEurekaServer實(shí)現(xiàn)服務(wù)注冊中心
- 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注冊到服務(wù)注冊中心
- 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用注冊中心并發(fā)現(xiàn)服務(wù),通過spring-cloud-starter-ribbon來實(shí)現(xiàn)負(fù)載均衡消費(fèi)服務(wù)
PS:這里說明下,我用的IDE是Spring Tool Suite,是spring定制版的eclipse,方便我們使用spring進(jìn)行開發(fā),有興趣的朋友可以自行百度了解下。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù)功能
這篇文章主要介紹了Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04java實(shí)現(xiàn)任意矩陣Strassen算法
這篇文章主要介紹了java實(shí)現(xiàn)任意矩陣Strassen算法的相關(guān)資料,需要的朋友可以參考下2016-02-02Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法
這篇文章主要介紹了Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用Atomikos技術(shù)整合多數(shù)據(jù)源的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03