柚子快報(bào)激活碼778899分享:Eureka學(xué)習(xí)筆記
柚子快報(bào)激活碼778899分享:Eureka學(xué)習(xí)筆記
一、Eureka搭建
我使用的是idea 首先新建項(xiàng)目 地址是默認(rèn)的Spring地址,點(diǎn)擊next 項(xiàng)目名稱大小寫要統(tǒng)一,否則會(huì)報(bào)這個(gè)錯(cuò)誤:Artifact contains illegal characters 類型選默認(rèn)類型會(huì)報(bào)500 java版本和使用的版本要一致 如果默認(rèn)的spring地址沒有jdk1.8可以選擇 https://start.aliyun.com/ 這個(gè)阿里云的地址
Eureka組件需要搜索 選中后會(huì)在Selected Dependencies中 點(diǎn)擊next,選擇項(xiàng)目名稱和存儲(chǔ)路徑,點(diǎn)擊finish,項(xiàng)目建立成功,如果jdk版本不對(duì)可以在modules和settings中進(jìn)行更改 也可以直接在阿里云地址進(jìn)行選擇配置然后下載壓縮包解壓后使用,效果是一樣的
pom文件中的Springboot版本和springcloud版本一定要一致否則eureka包無法引入
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
配置 默認(rèn)的配置文件是在resource下面的application.properties,在springboot的項(xiàng)目中,目前支持兩種配置文件的形式,還有一種是yaml,我這里使用的所有配置全為yml形式。 application.yaml
server:
port: 8761
spring:
application:
name: eureka-serve
eureka:
server:
enable-self-preservation: false
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:8761/eureka/
application.properties
spring.application.name=eureka-server
server.port=8761
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms: 300
# 注冊(cè)中心職責(zé)是維護(hù)服務(wù)實(shí)例,false:不檢索服務(wù)。
eureka.client.fetch-registry=false
eureka.service-url.defaultZone=http://localhost:8761/eureka/
enable-self-preservation: 防止由于Eureka的機(jī)制導(dǎo)致Client被錯(cuò)誤顯示在線,僅在開發(fā)環(huán)境使用,生產(chǎn)環(huán)境需緩存此信息,防止因網(wǎng)絡(luò)波動(dòng)導(dǎo)致服務(wù)頻繁上下線。 register-with-eureka: 不像注冊(cè)中心注冊(cè)自己 service-url-defaultZone: 此eureka server的應(yīng)用注冊(cè)地址
eureka網(wǎng)頁地址localhost:8761
二、服務(wù)的提供與Feign調(diào)用
按照上面的流程新建兩個(gè)項(xiàng)目這兩項(xiàng)目我分別起名為consumers和producer,下文代碼中所有package 路徑以自己的為準(zhǔn) 服務(wù)提供者producer application.yml配置文件
server:
port: 8080
spring:
application:
name: spring-cloud-producer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
pom.xml配置文件新增了 org.springframework.boot spring-boot-starter-web spring-boot-starter-web: 這個(gè)包是通用的web開發(fā)包,里面包含了spring-web、spring-webmvc等包
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
啟動(dòng)類ProducerApplication.java 增加@EnableEurekaClient,如果是其他注冊(cè)中心可以使用注解@EnableDiscoveryClient來進(jìn)行服務(wù)的注冊(cè)
package com.*.producer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
Controller類,在producer.controller文件夾下新增HelloController類
package com.*.producer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer is ready";
}
}
現(xiàn)在在可以先啟動(dòng)Eureka,再啟動(dòng)我們剛寫好的producer服務(wù)提供者,啟動(dòng)成功后,訪問鏈接http://localhost:8761/,可以看到我們的的服務(wù)提供者producer已經(jīng)成功注冊(cè)在注冊(cè)中心上了。 至此,服務(wù)的提供者已經(jīng)配置完成。
服務(wù)消費(fèi)者consumers
pom.xml org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign 新增兩個(gè)開發(fā)包 spring-boot-starter-web: 這個(gè)包是通用的web開發(fā)包,里面包含了spring-web、spring-webmvc等包
spring-cloud-starter-openfeign: 這個(gè)包是springcloud對(duì)于Feign的封裝,F(xiàn)eign是一個(gè)聲明式的Web服務(wù)客戶端。它支持Feign本身的注解、JAX-RS注解以及SpringMVC的注解。Spring Cloud集成Ribbon和Eureka以在使用Feign時(shí)提供負(fù)載均衡的http客戶端。
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
配置文件application.yml
server:
port: 8081
spring:
application:
name: spring-cloud-consumers
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
啟動(dòng)類ConsumersApplication.java 同上,增加@EnableEurekaClient,如果是其他注冊(cè)中心可以使用注解@EnableDiscoveryClient來進(jìn)行服務(wù)的注冊(cè)
package com.*.consumers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumersApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumersApplication.class, args);
}
}
@EnableFeignClients: 這個(gè)注解是通知SpringBoot在啟動(dòng)的時(shí)候,掃描被 @FeignClient 修飾的類,@FeignClient這個(gè)注解在進(jìn)行遠(yuǎn)程調(diào)用的時(shí)候會(huì)用到。
Feign遠(yuǎn)程調(diào)用 Feign是一個(gè)聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡(jiǎn)單, 它的使用方法是定義一個(gè)接口,然后在上面添加注解,同時(shí)也支持JAX-RS標(biāo)準(zhǔn)的注解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對(duì)Feign進(jìn)行了封裝,使其支持了Spring MVC標(biāo)準(zhǔn)注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負(fù)載均衡。
創(chuàng)建一個(gè)remote接口
package com.*.consumers.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
name:遠(yuǎn)程服務(wù)名,及spring.application.name配置的名稱 此類中的方法和遠(yuǎn)程服務(wù)中contoller中的方法名和參數(shù)需保持一致 web層調(diào)用遠(yuǎn)程接口 Controller
package com.*.consumers.controller;
import com.springcloud.consumers.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
現(xiàn)在,一個(gè)最簡(jiǎn)單的服務(wù)注冊(cè)和調(diào)用的例子就完成了。
測(cè)試 簡(jiǎn)單調(diào)用 順次啟動(dòng)eureka、producer、consumer三個(gè)項(xiàng)目
啟動(dòng)成功后,先在瀏覽器輸入http://localhost:8080/hello?name=springcloud
可以看到頁面顯示:hello springcloud,producer is ready
證明我們的producer已經(jīng)正常啟動(dòng),提供的服務(wù)也正常
接下來,我們測(cè)試服務(wù)消費(fèi)者,在瀏覽器中輸入:http://localhost:8081/hello/spring
可以看到頁面顯示:hello spring,producer is ready
說明客戶端已經(jīng)成功的通過feign調(diào)用了遠(yuǎn)程服務(wù)hello,并且將結(jié)果返回到了瀏覽器。
負(fù)載均衡 將上面的producer復(fù)制一份,修改名稱為producer2,修改pom.xml中的\ \ 為producer2,修改其中的Controller:
package com.*.producer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer2 is ready";
}
}
修改application.yml配置文件啟動(dòng)端口為8082
啟動(dòng)我們剛復(fù)制好的producer2,這時(shí)可以看一下注冊(cè)中心Eureka,我們現(xiàn)在已經(jīng)有兩個(gè)producer服務(wù)了。
這時(shí)我們?cè)偃ピL問:http://localhost:8081/hello/spring
第一次返回結(jié)果:hello spring,producer is ready
第二次返回結(jié)果:hello spring,producer2 is ready
連續(xù)刷新頁面,兩個(gè)結(jié)果會(huì)交替出現(xiàn),說明注冊(cè)中心提供了服務(wù)負(fù)載均衡功能。將服務(wù)數(shù)提高到N個(gè),會(huì)發(fā)現(xiàn)測(cè)試結(jié)果一樣,請(qǐng)求會(huì)自動(dòng)輪詢到每個(gè)服務(wù)端來處理。
柚子快報(bào)激活碼778899分享:Eureka學(xué)習(xí)筆記
參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。