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