javascript
SpringCloud-Eureka-ProviderConsumer
Eureka-Provider 服務的提供者
新建一個服務提供者項目
1、導入pom文件
<properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR3</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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-test</artifactId><scope>test</scope></dependency></dependencies><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>2、在啟動類上加注解
@SpringBootApplication @EnableDiscoveryClient // 這個注解加不加都可以,因為Eureka public class EurekaProviderApplication {public static void main(String[] args) {SpringApplication.run(EurekaProviderApplication.class, args);} }上邊那個@EnableDiscoverClient 注解加不加都行的原因會在后邊表名
3、在Eureka-Provider項目中添加一個簡單的接口
@RestController public class EurekaProviderController {@GetMapping("/provider")public String provider(@RequestParam String aaa){return "eureka-provider-return" + aaa;} }4、以上配置完成之后啟動Eureka-Provider
啟動后會在控制臺輸出
DiscoveryClient_EUREKA-PROVIDER/192.168.1.4:eureka-provider:8000: registering service...
? 同時看localhost:8761 頁面的Instance currently registered 會多出一條信息
? Application AMIs Availability Zones Status
? EUREKA-PROVIDER n/a (1) (1) UP (1) - 192.168.1.4:eureka-provider:8000
Eureka-Consumer 服務的調用者
調用者的配置和上邊提供者類似,applicatioin.yml配置修改server.port=8100
1、啟動類修改
@SpringBootApplication // 另一個發現服務的注解可以不用謝(SpringCloud版本要在Edgware之后) public class EurekaConsumerApplication {@Bean@LoadBalancedRestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(EurekaConsumerApplication.class, args);} }2、編寫一個調用接口的類
@RestController public class EurekaConsumerController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/consumer")@GetMapping("/consumer")public String consumer(@RequestParam String aaa){return restTemplate.getForObject("http://EUREKA-PROVIDER/provider?aaa=" + aaa,String.class );} }3、啟動服務調用者
? 啟動后,調用調用者的觸發地址localhost:8100/consumer?aaa=consumerSemdParamter
? 瀏覽器顯示內容:
eureka-provider-returnconsumerSemdParamter
上邊遺留的一個問題,eureka-client不加@EnableDiscoveryClient以將自己注冊到注冊中心
-
先看EurekaClientAutoConfiguration類中
@Configuration @EnableConfigurationProperties @ConditionalOnClass(EurekaClientConfig.class) @Import(DiscoveryClientOptionalArgsConfiguration.class) @ConditionalOnBean(EurekaDiscoveryClientConfiguration.Marker.class) @ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true) @ConditionalOnDiscoveryEnabled @AutoConfigureBefore({ NoopDiscoveryClientAutoConfiguration.class,CommonsClientAutoConfiguration.class, ServiceRegistryAutoConfiguration.class }) @AutoConfigureAfter(name = {"org.springframework.cloud.autoconfigure.RefreshAutoConfiguration","org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration","org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration" }) public class EurekaClientAutoConfiguration { }
? 可以看到該類加載的條件是需要有EurekaDiscoveryClientConfiguration.Maker.class的Bean存在,并且eureka.client.enabled為true,因為該值默認為true,所以不需要關注,因此重要的就是EurekaDiscoveryClientConfiguration.Maker.class這個,而這個類在Dalston之前的舊版本是不會自動加載的,而在Edgware之后,該類就配置到spring.factories文件中了,改文件中所配置的bean在springboot啟動的時候就會被加載(所以不需要手動配上注冊服務的注解了,springboot會自動配置)
總結
以上是生活随笔為你收集整理的SpringCloud-Eureka-ProviderConsumer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot-SpringClo
- 下一篇: SpringCloud-Eureka-服