衆所周知,SpringCloud是一個微服務框架,本質上是基於SpringBoot的一整套實現微服務的框架。包含了服務發現、負載均衡、斷路器、服務網關、分佈式配置等組件。其中服務註冊、發現又有Netflix的Eureka,阿里的Dubbo,Apache的Consul等,本篇文章將以Eureka進行講解。git
Eureka是Netflix開發的服務發現框架,自己是一個基於REST的服務。由兩個組件組成:github
圖片來源於互聯網,如侵權可聯繫博主web
Eureka的做用就是將咱們定義的API接口註冊到Eureka服務器上,方便管理,調用的時候只須要知道服務名就能夠,再也不經過IP加端口號的方式調用,利於解耦。spring
將src
文件夾刪除(若是有的話)打開pom.xml
文件,添加以下代碼。緩存
<!--必須指定該父模塊,否則後面子模塊啓動會報錯,很麻煩--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--父模塊類型必須爲pom--> <packaging>pom</packaging> <!--包含子模塊--> <modules> </modules> <!--在父模塊添加web依賴,子模塊可繼承該依賴--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
打開Server模塊的pom.xml
文件,修改 <parent>
標籤服務器
<parent> <groupId>org.sakura</groupId> <artifactId>eureka</artifactId> <version>1.0-SNAPSHOT</version> </parent>
打開主模塊的pom.xml
文件,在<modules>
標籤添加相應的子模塊app
<!--包含子模塊--> <modules> <module>Server</module> </modules>
將Server模塊中的application.properties
重命名爲application.yml
,並添加以下信息,也可直接使用負載均衡properties
文件類型(需修改以下代碼)
# Eureka 服務註冊與發現的組件 server: port: 8080 spring: application: #服務名,很重要 name: server eureka: instance: hostname: localhost #將prefer-ip-address設爲開啓時,將默認顯示服務的地址,而非主機名 # prefer-ip-address: true #以IP地址註冊到服務中心,相互註冊使用IP地址 # prefer-ip: 127.0.0.1 #顯式設置服務的地址 client: # 下面兩個 false 代表本身是 server,而非 client register-with-eureka: false # 不要使用 eureka 服務進行註冊,即在管理界面不可見 fetch-registry: false # 不要在本地緩存註冊表信息 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # defaultZone: http://127.0.0.1:${server.port}/eureka/ server: #開啓自我保護模式 enable-self-preservation: false #清理無效節點,默認60*1000毫秒,即60秒 eviction-interval-timer-in-ms: 5000
修改Server模塊的啓動類,添加@EnableEurekaServer
註解便可框架
@EnableEurekaServer @SpringBootApplication public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
pom.xml
,與Server模塊同樣修改application.yml
分佈式
server: port: 8081 spring: application: # 服務名,很重要 name: client eureka: instance: hostname: localhost #以IP地址註冊到服務中心,相互註冊使用IP地址 # prefer-ip-address: true client: service-url: #服務註冊地址 defaultZone: http://${eureka.instance.hostname}:8080/eureka
修改啓動類,添加@EnableEurekaClient
註解(@EnableDiscoveryClient
或不加均可以)
@EnableEurekaClient @SpringBootApplication public class Client1Application { public static void main(String[] args) { SpringApplication.run(Client1Application.class, args); } }
SpringCloud有兩種服務調用的方式
SpringCloud Ribbon是一個基於HTTP和TCP的客戶端負載均衡工具,它基於Netflix Ribbon實現。幾乎存在於每個SpringCloud構建的微服務和基礎設施中。由於微服務間的調用,API網關的請求轉發等內容,實際上都是經過Ribbon來實現的。
再建立一個Client2服務,配置文件中除端口與以前的Client不一樣外,其它都一致,服務名也同樣,這是爲了實現負載均衡。
server: port: 8082 spring: application: name: client eureka: instance: hostname: localhost #以IP地址註冊到服務中心,相互註冊使用IP地址 # prefer-ip-address: true client: service-url: #服務註冊地址 defaultZone: http://${eureka.instance.hostname}:8080/eureka
爲兩個Client各添加一個API接口
@RestController public class HelloController { @GetMapping("/hello") public String sayHello(@RequestParam(required = true,name = "name") String name){ return "Hello " + name + ", 8081"; } }
@RestController public class HelloController { @GetMapping("/hello") public String sayHello(@RequestParam(required = true,name = "name") String name){ return "Hello " + name + ", 8082"; } }
修改配置文件和pom
server: port: 8083 spring: application: name: ribbon eureka: instance: hostname: localhost #以IP地址註冊到服務中心,相互註冊使用IP地址 # prefer-ip-address: true client: service-url: #服務註冊地址 defaultZone: http://${eureka.instance.hostname}:8080/eureka
修改啓動類,需添加一個RestTemplate bean
@EnableDiscoveryClient @SpringBootApplication public class RibbonApplication { public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } /** * 負載均衡配置
*/ @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } } ```
在Ribbon模塊中加入一個Service和一個Controller
@Service public class HelloService { @Autowired RestTemplate restTemplate; public String helloService(String name){ return restTemplate.getForObject("http://client/hello?name="+name,String.class); } }
@RestController public class HelloController { @Autowired HelloService helloService; @GetMapping("/hello") public String hello(String name){ return helloService.helloService(name); } }
能夠看到,兩個Client和一個Ribbon都已經註冊上去了
Feign是基於Ribbon實現的工具,採用基於接口的註解
修改配置文件和pom
server: port: 8084 spring: application: name: feign eureka: instance: hostname: localhost #以IP地址註冊到服務中心,相互註冊使用IP地址 # prefer-ip-address: true client: service-url: #服務註冊地址 defaultZone: http://${eureka.instance.hostname}:8080/eureka
修改啓動類,添加@EnableEurekaClient
和@EnableFeignClients
@EnableFeignClients @EnableEurekaClient @SpringBootApplication public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } }
建立一個Service接口
@FeignClient(value = "client") //value值爲須要調用的服務名 public interface IFeignService { @GetMapping("/hello") //這裏的地址爲須要調用的服務裏相應的接口地址 String hello(@RequestParam(value = "name")String name); }
使用上面的接口(聲明完上面Feign接口後,其餘Spring管理的類,如Service、Controller均可以直接注入使用,IDEA可能會提示不能注入,可忽略)
@RestController public class HelloController { @Autowired private IFeignService iFeignService; @GetMapping("/feign/hello") public String sayHello(String name){ return iFeignService.hello(name); } }
以上相對全面簡潔的介紹了SpringCloud中服務的註冊、發現與調用。代碼已上傳至Github