一、服務注冊與發(fā)現(xiàn)
這里會用到Spring Cloud Netflix涎跨,該項目是Spring Cloud的子項目之一隅很,主要內(nèi)容是對Netflix公司一系列開源產(chǎn)品的包裝叔营,它為Spring Boot應用提供了自配置的Netflix OSS整合绒尊。通過一些簡單的注解婴谱,開發(fā)者就可以快速的在應用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)躯泰。它主要提供的模塊包括:服務發(fā)現(xiàn)(Eureka)斟冕,斷路器(Hystrix),智能路由(Zuul)景描,客戶端負載均衡(Ribbon)等超棺。
這里的核心內(nèi)容是服務發(fā)現(xiàn)模塊:Eureka
創(chuàng)建“服務注冊中心”
1.創(chuàng)建基于web的Maven項目(springcloud)
2.創(chuàng)建服務注冊中心棠绘。在springcloud項目中創(chuàng)建SpringBoot項目(springboot):
image.png
image.png
勾選Cloud Discovery–>Eureka server氧苍。以方便導包
image.png
3編寫springboot項目
3.1查看pom.xml文件
<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASE<!-- lookup parent from repository -->com.handspringclouddemo0.0.1-SNAPSHOTspringclouddemoDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone
4在啟動類上加上注解 如下
通過@EnableEurekaServer注解啟動一個服務注冊中心提供給其他應用進行對話。這一步非常的簡單罢荡,只需要在一個普通的Spring Boot應用中添加這個注解就能開啟此功能,比如下面的例子:
packagecom.hand;
import org.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassSpringclouddemoApplication{publicstaticvoidmain(String[] args){ SpringApplication.run(SpringclouddemoApplication.class, args); }}
5 修改application.yml文件
yml文件的好處浪南,天然的樹狀結(jié)構(gòu)络凿,一目了然
---#端口號server: port: 8760eureka: instance: hostname: localhost client:# eureka.client.registerWithEureka :表示是否將自己注冊到Eureka Server喷众,默認為true到千。# 由于當前這個應用就是Eureka Server憔四,故而設為falseregister-with-eureka:false# eureka.client.fetchRegistry :表示是否從Eureka Server獲取注冊信息了赵,默認為true甸赃。因為這是一個單點的Eureka Server络断,# 不需要同步其他的Eureka Server節(jié)點的數(shù)據(jù)貌笨,故而設為false襟沮。fetch-registry:falseservice-url:# eureka.client.serviceUrl.defaultZone :設置與Eureka Server交互的地址开伏,#查詢服務和注冊服務都需要依賴這個地址硅则。默認是defaultZone: http://{server.port}/eureka/
6 啟動項目后訪問
可以看到下面的頁面怎虫,其中還沒有發(fā)現(xiàn)任何服務:
image.png
7.搭建服務端(生產(chǎn)者)
創(chuàng)建springBoot項目同上
查看pom.xml文件
<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASEcom.handproducer0.0.1-SNAPSHOTproducerDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone
8修改application.yml文件
注:端口不能與上面的相同蘸际。這里的服務name:service-hi 可以根據(jù)自己情況定義粮彤。
---server: port:8762eureka: client: service-url: defaultZone: http://localhost:8760/eureka/spring: application: name: service-producer
9 編寫啟動類
packagecom.hand.producer;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClient@RestControllerpublicclassProducerApplication{publicstaticvoidmain(String[] args){ SpringApplication.run(ProducerApplication.class, args); }@Value("${server.port}") String port;@RequestMapping("/hi")publicStringhome(@RequestParam String name){return"hi "+ name +",i am from port:"+ port; }}
運行服務
http://localhost:8761/hi?name=xiong.zhang@hand-china.com
image.png
可以看到,我們定義的服務被注冊了圈澈。如下圖所示:
image.png
9.創(chuàng)建消費者
9.1 創(chuàng)建消費者modul递递,流程如上述工程創(chuàng)建流程登舞。
9.2查看pom.xml文件
<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASEcom.handcustomer0.0.1-SNAPSHOTcustomerDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone
10 yml配置
---server: port:8763eureka: client: service-url: defaultZone: http://localhost:8760/eureka/spring: application: name: service-customerfeign: hystrix: enabled :true
11 編寫啟動類
@EnableDiscoveryClient表明標注類是消費者菠秒,加入restTemplate以消費相關(guān)的服務
packagecom.hand.customer;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.cloud.client.loadbalancer.LoadBalanced;importorg.springframework.context.annotation.Bean;importorg.springframework.web.client.RestTemplate;@SpringBootApplication@EnableDiscoveryClientpublicclassCustomerApplication{publicstaticvoidmain(String[] args){ SpringApplication.run(CustomerApplication.class, args); }@Bean@LoadBalancedRestTemplaterestTemplate(){returnnewRestTemplate(); }}
12 .創(chuàng)建service和controller
12.1 service層
packagecom.hand.customer.controller;importcom.hand.customer.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;packagecom.hand.customer.service;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.web.client.RestTemplate;/**
Created with IntelliJ IDEA.
Author: Patrick
Date: 2019/2/25
Time: 23:12
Description:
*/@ServicepublicclassHelloService{@AutowiredRestTemplate restTemplate;publicStringhiService(String name){returnrestTemplate.getForObject("http://SERVICE-PRODUCER/hi?name="+ name, String.class); }}
12.2 controller層
packagecom.hand.customer.controller;importcom.hand.customer.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/**
Created with IntelliJ IDEA.
Author: Patrick
Date: 2019/2/25
Time: 23:38
Description:
*/@RestControllerpublic class HelloControler { @AutowiredHelloService helloService; @RequestMapping(value="/hi") public String hi(@RequestParam String name) {returnhelloService.hiService(name); }}
再次查看服務
image.png
在瀏覽器中輸入http://localhost:8763/hi?name=admin
作者:佛祖0
鏈接:
簡書著作權(quán)歸作者所有践叠,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。