服務治理是微服務架構中最爲核心和基礎的模塊,它主要用來實現各個微服務實例的自動化註冊與發現。
web
1.服務註冊:spring
在服務治理框架中,一般會構建一個註冊中心,每一個服務單元向註冊中心登記本身提供的服務,將主機與端口號、緩存
版本號、通訊協議等一些附件信息告知註冊中心,註冊中心按服務名稱分類組織服務清單,例如:架構
2.服務發現:app
調用方須要向服務註冊中心諮詢服務,並獲取全部服務的實例清單,以實現對具體服務實例的訪問。負載均衡
Spring Cloud Euraka使用Netflexi Euraka來實現服務的註冊與發現,它既包含了框架
服務端的組件也包含了客戶端的組件。spring-boot
Euraka 服務端:即服務註冊中心。微服務
Euraka 客戶端:主要處理服務的註冊與發現。Euraka客戶端向註冊中心註冊自身提供的服務並fetch
週期性地發送心跳來更新它的服務租約。同時,他能從服務端查詢當前註冊的服務信息並把它們
緩存到本地並週期性地更新服務狀態。
pom.xml中引入必要依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
啓用服務註冊中心:
//啓動一個服務註冊中心,提供給其餘應用進行對話。 //在默認設置下,該服務註冊中心也會將本身做爲客戶端嘗試註冊它本身 @EnableEurekaServer @SpringBootApplication public class TangcloudApplication { public static void main(String[] args) { SpringApplication.run(TangcloudApplication.class, args); } }
禁用默認的客戶端註冊行爲:
#設置服務註冊中心端口 server.port=1111 eureka.instance.hostname=localhost #不向註冊中心註冊本身 eureka.client.register-with-eureka=false #註冊中心不須要去檢索服務 eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eurka/
pom.xml:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
controller:
@RestController public class HelloController { @Autowired private DiscoveryClient client; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String index() { ServiceInstance serviceInstance = client.getLocalServiceInstance(); System.out.println("/hello, host: " + serviceInstance.getHost() + ", service_id: " + serviceInstance.getServiceId()); return "Hello World!"; } }
@EnableDiscoveryClient //激活DiscoveryClient實現 @SpringBootApplication public class Service1Application { public static void main(String[] args) { SpringApplication.run(Service1Application.class, args); } }
application.properties文件:
server.port=9669 spring.application.name=hello-service eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
Euraka高可用實際上就是將本身做爲服務向其餘服務註冊中心註冊本身,這樣就能夠
造成一組互相註冊的服務註冊中心,以實現服務清單的互相同步,達到高可用效果。
構建一個服務註冊中心集羣:
1)建立application-peer1.properties,做爲peer1服務中心的配置:
pring.application.name=eureka-server server.port=1111 eureka.instance.hostname=peer1 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
2)建application-peer1.properties,做爲peer1服務中心的配置:
spring.application.name=eureka-server server.port=1112 eureka.instance.hostname=peer2 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
3)服務提供方的配置:
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
下面構建一個服務消費者,它能夠發現服務及消費服務。其中服務的發現任務由Eureka的客戶端完成。
Ribbon是一個基於HTTP和TCP的客戶端負載均衡器,它在Eureka發現服務的基礎上,實現了一套對服務
的選擇策略。
首先,建立一個Spring Boot工程,並在以前hello-service的pom.xml基礎上新增:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
啓動類:
@SpringBootApplication @EnableDiscoveryClient //讓該應用註冊爲Eureka客戶端,得到服務發現的能力 public class ConsumerApplication { @Bean @LoadBalanced //開啓客戶端負載均衡 RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
application.properties
server.port=9671 spring.application.name=ribbon-consumer eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
controller:
@RestController public class ConsumerController { @Autowired RestTemplate restTemplate; //經過它來調用service-hello服務的/hello接口 @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET) public String helloConsumer() { return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody(); } }