服務提供者與服務消費者
Spring Cloud版本:Hoxton.SR5
1. 簡介
上一章已經搭建了一個服務注冊中心,微服務中所有服務調用都通過服務注冊中心進行,因此就存在調用方和被調用方。也就是服務提供者和服務消費者。
實際情況中可能一個服務既是提供者又是消費者,本章不做討論。
服務提供者:被其他微服務調用的一方
服務消費者:調用其他微服務的一方
2. 服務提供者
2.1 服務搭建
-
創建一個Spring Boot項目,引入如下依賴:
spring-cloud-starter-netflix-eureka-client
:進行服務注冊spring-boot-starter-web
:接口開發spring-boot-starter-actuator
:監控、健康檢查
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency>
-
在啟動類上添加
@EnableDiscoveryClient
注解,啟動時自動進行服務注冊@EnableDiscoveryClient @SpringBootApplication public class SclEurekaClientProviderApplication { public static void main(String[] args) { SpringApplication.run(SclEurekaClientProviderApplication.class, args); } }
-
配置文件application.yml
server: port: 8080 spring: application: name: eureka-client-provider eureka: client: register-with-eureka: true # 是否注冊到服務注冊中心 fetch-registry: false # 是否從注冊中心抓取信息,若不存在服務調用設置為false即可,反之則設置為true service-url: defaultZone: http://root:123456@test1:8100/eureka-server1/eureka,http://root:123456@test2:8200/eureka-server2/eureka,http://root:123456@test3:8300/eureka-server3/eureka # 服務注冊中心地址 management: endpoint: health: show-details: always # 總是展示詳細的健康檢查信息
-
開發一個簡單的接口
@RestController @RequestMapping("/provider") public class TestController { @GetMapping("/info") public Map<String, String> get() throws Exception { Map<String, String> map = new HashMap<>(); map.put("application.name", "eureka-client-provider"); map.put("server.port", "8080"); return map; } }
-
啟動項目,並訪問服務注冊中心、接口、健康檢查端點等
2.2 常見問題
上面訪問接口可發現,返回的是xml格式的數據,並不是我們期待的json格式數據。
因為eureka默認依賴了jackson-dataformat-xml
。xml的優先級高於json,因此返回的是xml格式的數據。只需將jackson-dataformat-xml
依賴排除即可。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</exclusion>
</exclusions>
</dependency>
3. 服務提供者
-
創建一個Spring Boot項目,引入如下依賴:
-
spring-cloud-starter-netflix-eureka-client
:進行服務注冊 -
spring-boot-starter-web
:接口開發 -
spring-boot-starter-actuator
:監控、健康檢查 -
spring-cloud-starter-openfeign
:聲明式接口調用 -
spring-cloud-starter-netflix-hystrix
:熔斷器,當接口調用異常時直接進行熔斷防止出現“雪崩”效應<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
-
在啟動類添加
@EnableHystrix
、@EnableFeignClients
、@EnableDiscoveryClient
,啟用熔斷器、openfeign、服務注冊等@EnableHystrix @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class SclEurekaClientConsumerApplication { public static void main(String[] args) { SpringApplication.run(SclEurekaClientConsumerApplication.class, args); } }
-
配置文件application.yml
server: port: 8090 spring: application: name: eureka-client-consumer eureka: client: register-with-eureka: true # 是否注冊到服務注冊中心 fetch-registry: true # 是否從注冊中心抓取信息,若不存在服務調用設置為false即可,反之則設置為true service-url: defaultZone: http://root:123456@test1:8100/eureka-server1/eureka,http://root:123456@test2:8200/eureka-server2/eureka,http://root:123456@test3:8300/eureka-server3/eureka # 服務注冊中心地址 feign: hystrix: enabled: true # 啟用Hystrix management: endpoints: web: exposure: include: info,health,hystrix-stream # actuator暴露端點 endpoint: health: show-details: always # 總是展示詳細的健康檢查信息
-
添加openFeign配置類,指定日志輸入等級
@Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
-
添加Feign接口進行接口調用,並指定接口異常進行熔斷后的回調處理方法
@FeignClient里的name屬性的值對應服務注冊中心的服務名稱。即列表里的Application
@FeignClient(name = "eureka-client-provider", configuration = FeignConfig.class, fallbackFactory = TestFeignFallback.class) public interface TestFeign { @GetMapping("/provider/info") Map<String, String> getTest(); } @Component class TestFeignFallback implements FallbackFactory<TestFeign> { private static final Logger log = LoggerFactory.getLogger(TestFeignFallback.class); @Override public TestFeign create(Throwable throwable) { return new TestFeign() { @Override public Map<String, String> getTest() { log.info("接口調用異常,觸發熔斷機制。 異常原因: ", throwable); return new HashMap<>(); } }; } }
-
添加一個接口,在接口中調用服務提供者提供的接口
@RestController @RequestMapping("/consumer") public class TestController { private final TestFeign feign; @Autowired public TestController(TestFeign feign) { this.feign = feign; } @GetMapping("/info") public Map<String, String> get() { return this.feign.getTest(); } }
-
啟動項目,並訪問服務注冊中心、接口、健康檢查端點、Hystrix監控端點等