Eureka(服務注冊中心),主要包括對服務功能的注冊、調用、熔斷、降級、負載等。
有了服務中心項目的關系有哪些變化呢,用幾張圖來解釋一下(暫缺,后續找到好的畫圖軟件補上):
這樣好像還看不出來有哪些簡便的地方,但是當項目多達十幾個或幾十個時項目與項目之間的關系就非常復雜了,而且當有一個服務IP地址發生改變時與其相關的幾個服務的依賴地址也要進行相應的改變,這在實際的開發運維中給人員帶來極大的不便。當所有的服務都通過注冊中心進行調用,就不用了關心具體的服務的IP地址,只需要向服務中心申請就可以了,如何調用就是服務中心自己的事情了 ,而且有了服務中心就可以實現許多高級的功能了,比如負載均衡。
在開發時用maven創建SpringCloud項目,通過新增Springboot模塊實現SpringCloud的基礎設施的開發。需引入的依賴至少包括SpringBoot,SpringCloud,我用的版本是1.5.3和,下面是具體的依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在Eureka中有兩個組件:Eureka服務端和Eureka客戶端,服務端用於注冊服務器,客戶端是一個java客戶端,用來簡化與服務端的交互,輪詢負載均衡器,並提供對服務故障切換的支持。用一張圖來解釋下Eureka實現過程中的調用關系(暫缺):
三個角色:
Eureka-Service:注冊服務中心
Service-Provider:服務提供者,將服務注冊到注冊中心供消費者調用
Service-Consumer:服務消費者,從服務中心獲取其他服務
一。實現注冊服務中心
1.添加依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.配置文件
spring.application.name=spring-cloud-eureka server.port=8000 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
在默認的配置中,服務中心也會將自己當做客戶端來注冊自己,需要將其禁用,
eureka.client.register-with-eureka=false 表示是否將自己注冊到服務中心,默認true
eureka.client.fetch-registry=false 表示是否從服務中心獲取服務列表,默認true
3.添加注解
在啟動類中,添加@EnableEurekaServer注解
@SpringBootApplication @EnableEurekaServer public class SpringCloudEurekaApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaApplication.class, args); } }
4.集群
作為微服務最核心的組件,所有的服務都依賴於此組件,配置單一的組件顯然風險很大,因此配置集群就非常有必要,在實際生產環境中至少要配置兩套以上服務中心,形成集群,當其中某個服務宕掉時,另一個服務還可以繼續對外提供服務
在host 文件中注冊以下code備用(在System32中的host文件)
127.0.0.1 peer1 127.0.0.1 peer2 127.0.0.1 peer3
配置文件
--- spring: application: name: spring-cloud-eureka profiles: peer1 server: port: 8000 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/ --- spring: application: name: spring-cloud-eureka profiles: peer2 server: port: 8001 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/ --- spring: application: name: spring-cloud-eureka profiles: peer3 server: port: 8002 eureka: instance: hostname: peer3 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
分別以peer1,peer2,peer3啟動服務中心
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
登錄瀏覽器:http://localhost:8080/,可以看到另外兩個服務中心已被注冊,至此集群完成。

二.服務注冊和調用
上文中已經完成了服務中心的注冊,現在要將生產者注冊到服務中心,並給服務消費者調用
(一).服務生產者
1.依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.配置文件(假設eureka的端口是8080)
server.port=8001 spring.application.name = spring-cloud-producer eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3.啟動類上加載@EnableEurekaClinet注解,並在啟動類中加入一個hello方法
@SpringBootApplication @EnableEurekaClient @RestController public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge"; } }
(二).服務消費者
1.依賴,同服務生產者一樣
2.配置文件
spring.application.name=spring-cloud-consumer server.port=8002 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3.在啟動類上加載@EnableEurekaClient和@EnableFeignClients注解
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
@EnableFeignClients:遠程調用
Feign是一個聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,然后在上面添加注解,同時也支持JAX-RS標准的注解。Feign也支持可拔插式的編碼器和解碼器。
Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標准注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。
4.編寫接口
@FeignClient(name="spring-cloud-producer") public interface HelloRemote(){ @RequestMapping("/hello") public String hello(@RequestParam("name") String name); }
5.將接口注冊到Controller中調用
@RestController public class HelloController{ @Autowired private HelloRemote helloRemote; @RequestMapping("/hello111/{name}") public String getHello(@PathVariable String name){ return helloRemote.hello(name); } }
至此服務消費者配置完成,輸入http://localhost:8002/hello111/zhangsan, 根據FeignClient中的name對應服務的spring.application.name找到對應服務中的映射URL,顯示
hello zhangsan,this is first messge
服務生產者的負載均衡
在服務生產者中加入spring.profiles.active =producer1/producer2(分別等於,然后在配置文件中 部署兩個 端口8081,8082),將producer2 hello方法中加入this is second messge
分別啟動jar包(可以在IDEA中配置啟動類中--spring.profiles.active=xxx),再次輸入http://localhost:8002/hello111/zhangsan,首先顯示this is first messge,再次刷新,this is second messge
