spring cloud 使用Eureka 進行服務(wù)治理方法
服務(wù)治理可以說是微服務(wù)架構(gòu)中最為核心和基礎(chǔ)的模塊,它主要用來實現(xiàn)各個微服務(wù)實例的自動化注冊和發(fā)現(xiàn)。
Spring Cloud Eureka是Spring Cloud Netflix 微服務(wù)套件的一部分,主要負責完成微服務(wù)架構(gòu)中的服務(wù)治理功能。
本文通過簡單的小例子來分享下如何通過Eureka進行服務(wù)治理:
- 搭建服務(wù)注冊中心
- 注冊服務(wù)提供者
- 服務(wù)發(fā)現(xiàn)和消費
==========我是華麗的分割線========================
一、搭建服務(wù)注冊中心
先列出完整目錄結(jié)構(gòu):

搭建過程如下:
1.創(chuàng)建maven工程:eureka(具體實現(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進行版本管理 -->
<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)建啟動類
/**
*
* @EnableEurekaServer
* 用來指定該項目為Eureka的服務(wù)注冊中心
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
4.配置application.properties文件
#設(shè)置tomcat服務(wù)端口號
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.啟動服務(wù)并訪問,我們會看到這樣的畫面:

二、注冊服務(wù)提供者
先列出完整目錄結(jié)構(gòu):

搭建過程如下:
1.創(chuàng)建maven工程:hello-service(具體實現(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)建啟動類
/***
*
* @EnableDiscoveryClient
* 讓服務(wù)使用eureka服務(wù)器
* 實現(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.啟動并測試
1.)啟動后再hello-service的控制臺會有這種字樣(xxx代表你的PC名)
eureka的控制臺會打印出如下字樣(xxx代表你的PC名)
2.)再次訪問localhost:1111,會發(fā)現(xiàn)有服務(wù)注冊到注冊中心了

三、服務(wù)發(fā)現(xiàn)和消費
完整目錄結(jié)構(gòu)如下:

搭建過程:
1.創(chuàng)建maven工程(具體實現(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 依賴 ,用來實現(xiàn)負載均衡,我們這里只是使用,先不作其他介紹-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
</project>
這里比hello-service服務(wù)提供者,多了ribbon的依賴
3.創(chuàng)建啟動類
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {
//@Bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean
@Bean
@LoadBalanced //@LoadBalanced實現(xiàn)負載均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
這里也要用到@EnableDiscoveryClient, 讓服務(wù)使用eureka服務(wù)器, 實現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
4.創(chuàng)建controller
@RestController
public class ConsumerController {
//這里注入的restTemplate就是在com.sam.ConsumerApp中通過@Bean配置的實例
@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 #這里的配置項目和服務(wù)提供者hello-service一樣
6.啟動,測試1.)啟動eureka。為了展示負責均衡的效果,我們的hello-service啟動兩個服務(wù),啟動兩個服務(wù)的具體步驟如下




以上是hello-service1的啟動步驟,端口號為9090;同樣方法設(shè)置hello-service2,端口號為9091(具體實現(xiàn)略)。
2.)啟動hello-consumer
3.)再次訪問http://localhost:1111/,會發(fā)現(xiàn)有2個hello-service服務(wù)(端口號一個是9090,一個是9091),1個hello-consume服務(wù)
4.) 多次訪問http://localhost:9999/hello-consumer,會發(fā)現(xiàn)hello-service1和hello-service2會輪流被調(diào)用(已經(jīng)實現(xiàn)了負責均衡),可以通過兩者的控制臺打印內(nèi)容確認(還記得我們在hello-service的controller中有個loggerlogger.info("*********" + instance.getServiceId());嗎?對,就是這個打?。?br />




四、總結(jié)
以上實例實現(xiàn)了基本的服務(wù)治理:
- 通過spring-cloud-starter-eureka-server和@EnableEurekaServer實現(xiàn)服務(wù)注冊中心
- 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注冊到服務(wù)注冊中心
- 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用注冊中心并發(fā)現(xiàn)服務(wù),通過spring-cloud-starter-ribbon來實現(xiàn)負載均衡消費服務(wù)
PS:這里說明下,我用的IDE是Spring Tool Suite,是spring定制版的eclipse,方便我們使用spring進行開發(fā),有興趣的朋友可以自行百度了解下。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
spring security與corsFilter沖突的解決方案
這篇文章主要介紹了spring security與corsFilter沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java利用httpclient通過get、post方式調(diào)用https接口的方法
這篇文章主要介紹了Java利用httpclient通過get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

