- 極簡Spring Cloud實(shí)戰(zhàn)
- 胡勁寒
- 709字
- 2019-10-10 18:57:58
2.1 使用Eureka
接下來嘗試一個(gè)Eureka的示例。
新建一個(gè)Maven項(xiàng)目,在pom.xml中添加對Eureka服務(wù)端的依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> 新建EurekaServerApplication.java,添加@EnableEurekaServer注解: @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
啟動(dòng)程序后,打開http://localhost:8080/就可以看到如圖2-2所示的監(jiān)控頁面,圖中展示了向Eureka注冊的所有客戶端。

圖2-2 服務(wù)注冊Eureka的監(jiān)控顯示結(jié)果
2.1.1 Eureka服務(wù)提供方
啟動(dòng)一個(gè)服務(wù)提供方并注冊到Eureka。新建一個(gè)項(xiàng)目并在pom.xml中添加依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
新建EurekaProviderApplication.java,并對外暴露一個(gè)sayHello的HTTP接口。
@SpringBootApplication @EnableEurekaClient @RestController public class EurekaProviderApplication { @RequestMapping("/sayHello") public String sayHello(String name){ return "hello "+name; } public static void main(String[] args) { new SpringApplicationBuilder(EurekaProviderApplication.class).web(true). run(args); } }
在application.yml配置Eureka服務(wù)端地址以及自身服務(wù)名稱:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #在注冊中心配置Eureka地址 spring: application: name: myprovider #自身名字
啟動(dòng)EurekaProviderApplication后,再去看Eureka的監(jiān)控頁面,應(yīng)該能看到如圖2-3所示信息,表明服務(wù)已經(jīng)注冊到Eureka。

圖2-3 在Eureka上注冊成功
注意
還有一種方法就是直接使用原生的com.netflix.discovery.EurekaClient(對應(yīng)Spring Cloud的DiscoveryClient)。通常,盡量不要直接使用原始的Netflix的Eureka Client,因?yàn)镾pring已經(jīng)對其進(jìn)行封裝抽象,應(yīng)盡可能使用DiscoveryClient。
2.1.2 Eureka服務(wù)調(diào)用方
一旦在應(yīng)用中使用了@EnableDiscoveryClient或者@EnableEurekaClient,就可以從Eureka Server中使用服務(wù)發(fā)現(xiàn)功能。
利用如下代碼,通過DiscoveryClient可以從Eureka服務(wù)端獲得所有提供myprovider服務(wù)的實(shí)例列表,并根據(jù)獲得的ServiceInstance對象獲取每個(gè)提供方的相關(guān)信息,如端口IP等,從中選取第一個(gè)提供方,通過Spring的RestTemplate發(fā)起HTTP請求進(jìn)行調(diào)用。對于獲取到的提供方集合,我們根據(jù)什么規(guī)則去選取哪個(gè)提供方?能否做到負(fù)載均衡呢?這將在第5章詳細(xì)介紹。
@SpringBootApplication @EnableDiscoveryClient @Slf4j @RestController public class EurekaClientApplication { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } @Autowired private DiscoveryClient client; @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/sayHello" , method = RequestMethod.GET) public String sayHello(String name) { List<ServiceInstance> instances = client.getInstances("myprovider"); if (! instances.isEmpty()) { ServiceInstance instance = instances.get(0); log.info(instance.getUri().toString()); String result=restTemplate.getForObject(instance.getUri().toString()+ "/sayHello? name="+name, String.class); return result; } return "failed"; } public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
此時(shí),我們通過HTTP請求調(diào)用/sayHello接口,則可以看到服務(wù)調(diào)用方從Eureka獲取服務(wù)提供方信息,并進(jìn)行調(diào)用的日志信息了。
提示
不要在@PostConstruct方法以及@Scheduled中(或者任何ApplicationContext還沒初始完成的地方)使用EurekaClient。其需要等待SmartLifecycle(phase=0)初始化完成才可以。
- ATmega16單片機(jī)項(xiàng)目驅(qū)動(dòng)教程
- Effective STL中文版:50條有效使用STL的經(jīng)驗(yàn)(雙色)
- 現(xiàn)代辦公設(shè)備使用與維護(hù)
- 嵌入式系統(tǒng)設(shè)計(jì)教程
- AMD FPGA設(shè)計(jì)優(yōu)化寶典:面向Vivado/SystemVerilog
- Apple Motion 5 Cookbook
- 基于Apache Kylin構(gòu)建大數(shù)據(jù)分析平臺(tái)
- Visual Media Processing Using Matlab Beginner's Guide
- 電腦高級(jí)維修及故障排除實(shí)戰(zhàn)
- VMware Workstation:No Experience Necessary
- FPGA實(shí)驗(yàn)實(shí)訓(xùn)教程
- 微控制器的應(yīng)用
- 可編程邏輯器件項(xiàng)目開發(fā)設(shè)計(jì)
- FPGA實(shí)戰(zhàn)訓(xùn)練精粹
- 筆記本電腦的結(jié)構(gòu)、原理與維修