服務治理可以說是微服務架構中最為核心和基礎的模塊,它主要用來實現各個微服務實例的自動化注冊和發現。
Spring Cloud Eureka是Spring Cloud Netflix 微服務套件的一部分,主要負責完成微服務架構中的服務治理功能。
本文通過簡單的小例子來分享下如何通過Eureka進行服務治理:
- 搭建服務注冊中心
- 注冊服務提供者
- 服務發現和消費
==========我是華麗的分割線========================
一、搭建服務注冊中心
先列出完整目錄結構:
搭建過程如下:
- 創建maven工程:eureka(具體實現略)
- 修改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>
- 創建啟動類
/** * * @EnableEurekaServer * 用來指定該項目為Eureka的服務注冊中心 */ @EnableEurekaServer @SpringBootApplication public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } }
- 配置application.properties文件
#設置tomcat服務端口號 server.port=1111 #設置服務名稱 spring.application.name=eureka-service eureka.instance.hostname=localhost #注冊中心不需要注冊自己 eureka.client.register-with-eureka=false #注冊中心不需要去發現服務 eureka.client.fetch-registry=false #設置服務注冊中心的URL eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
- 啟動服務並訪問,我們會看到這樣的畫面:
二、注冊服務提供者
先列出完整目錄結構:
搭建過程如下:
- 創建maven工程:hello-service(具體實現略)
- 修改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>
- 創建啟動類
/*** * * @EnableDiscoveryClient * 讓服務使用eureka服務器 * 實現服務注冊和發現 * */ @EnableDiscoveryClient @SpringBootApplication public class HelloApp { public static void main(String[] args) { SpringApplication.run(HelloApp.class, args); } }
- 創建controller
@RestController public class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired DiscoveryClient discoveryClient; @RequestMapping("/hello") public String hello() { ServiceInstance instance = discoveryClient.getLocalServiceInstance(); //打印服務的服務id logger.info("*********" + instance.getServiceId()); return "hello,this is hello-service"; } }
- 配置application.properties文件
server.port=9090 #設置服務名 spring.application.name=hello-service #設置服務注冊中心的URL,本服務要向該服務注冊中心注冊自己 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
- 啟動並測試
- )啟動后再hello-service的控制台會有這種字樣(xxx代表你的PC名)
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
eureka的控制台會打印出如下字樣(xxx代表你的PC名)
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
- )再次訪問localhost:1111,會發現有服務注冊到注冊中心了
- )啟動后再hello-service的控制台會有這種字樣(xxx代表你的PC名)
三、服務發現和消費
完整目錄結構如下:
搭建過程:
- 創建maven工程(具體實現略)
- 修改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 依賴 ,用來實現負載均衡,我們這里只是使用,先不作其他介紹--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project>
這里比hello-service服務提供者,多了ribbon的依賴 - 創建啟動類
@EnableDiscoveryClient @SpringBootApplication public class ConsumerApp { //@Bean 應用在方法上,用來將方法返回值設為為bean @Bean @LoadBalanced //@LoadBalanced實現負載均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
這里也要用到@EnableDiscoveryClient, 讓服務使用eureka服務器, 實現服務注冊和發現
- 創建controller
@RestController public class ConsumerController { //這里注入的restTemplate就是在com.sam.ConsumerApp中通過@Bean配置的實例 @Autowired RestTemplate restTemplate; @RequestMapping("/hello-consumer") public String helloConsumer() { //調用hello-service服務,注意這里用的是服務名,而不是具體的ip+port restTemplate.getForObject("http://hello-service/hello", String.class); return "hello consumer finish !!!"; } }
- 配置application.properties文件
server.port=9999 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka #這里的配置項目和服務提供者hello-service一樣
- 啟動,測試
- )啟動eureka。為了展示負責均衡的效果,我們的hello-service啟動兩個服務,啟動兩個服務的具體步驟如下
以上是hello-service1的啟動步驟,端口號為9090;同樣方法設置hello-service2,端口號為9091(具體實現略)。
- )啟動hello-consumer
- )再次訪問http://localhost:1111/,會發現有2個hello-service服務(端口號一個是9090,一個是9091),1個hello-consume服務
- ) 多次訪問http://localhost:9999/hello-consumer,會發現hello-service1和hello-service2會輪流被調用(已經實現了負責均衡),可以通過兩者的控制台打印內容確認(還記得我們在hello-service的controller中有個loggerlogger.info("*********" + instance.getServiceId());嗎?對,就是這個打印)
- )啟動eureka。為了展示負責均衡的效果,我們的hello-service啟動兩個服務,啟動兩個服務的具體步驟如下
四、總結
以上實例實現了基本的服務治理:
- 通過spring-cloud-starter-eureka-server和@EnableEurekaServer實現服務注冊中心
- 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用並注冊到服務注冊中心
- 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用注冊中心並發現服務,通過spring-cloud-starter-ribbon來實現負載均衡消費服務
PS:這里說明下,我用的IDE是Spring Tool Suite,是spring定制版的eclipse,方便我們使用spring進行開發,有興趣的朋友可以自行百度了解下。