Eureka Client的使用
生活随笔
收集整理的這篇文章主要介紹了
Eureka Client的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前我們講了注冊中心,也就是Eureka Server的使用,我們來講一下服務注冊,Eureka Client的使用,基本上第一件事就是把Eureka Server給啟動起來nohup java -jar eureka-0.0.1-SNAPSHOT.jar &10.40.8.152:8761這就方便了不用每次都去啟動它kill -9 4681我們要把一個應用注冊到一個應用中心去,那你要往哪個地址注冊,你總得配置一下,不然我怎么知道往哪注冊呢,eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka在啟動類上加注解@EnableEurekaClient@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableDiscoveryClient
public @interface EnableEurekaClient {}奇怪的是這個應用名怎么叫UNKNOWN,我們來改一下名字spring.application.name=client這就是修改應用的名字有的時候我們希望點過來是另外一個地址,他不是這個IP,這個我們怎么做到自定義呢http://10.40.8.144:7900/info配置里面有這么一個屬性eureka.instance.hostname=clientNameemergency! eureka may be incorrectly claiming instances are up when they're not. renewals are lesser than threshold and hence the instances are not being expired just to be safe.如果你不停的重啟,會導致一個什么問題呢,可能會出現一串警告了,Eureka Server端和這些應用,Client端他們采用的是心跳的機制,Server端會不停的去檢查,Client端是否還存活,那么他會在一定的時間統計出來,上線率,就是一個比例,定義某個比例的時候呢,就會爆出這么一個警告,意思就是說,你這個client上線率太低了,可能我都不知道你是上線還是下線,那么怎么辦呢,那么我就當做你是上線,就是寧可信其有,不可信其無,這其實是他的一種自我保護的模式,當然在開發環境,可以把它給關掉,避免你以后要來調用這個微服務的時候,這上面展示的在線,實際上他已經是下線狀態了,所以開發環境我們最好給他關掉eureka.server.enableSelfPreservation=false這個配置你只能在開發環境把它關閉,生產環境一定不要這么來設置,你要往哪個注冊地址去注冊,你要配置上的,配置完了啟動了,其實還是不能注冊上去,你要在啟動主類上加上注解@EnableEurekaClient,然后再看看我們的配置,你如果有需要的話呢,可以加上,給他起一個別名,另外應用的名字你最好給他寫上,不然應用的名字顯示的就是UNKNOWN,Eureka Server的自我保護,開發環境你可以把它給關閉,這樣方便以后我們做微服務的調用,不會出錯
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><!-- Spring Boot進行單元測試的模塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version><packaging>pom</packaging><name>microcloud02</name><url>http://maven.apache.org</url><!-- <modules><module>microservice-hystrix-dashboard</module></modules> --><properties><jdk.version>1.8</jdk.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><dependency> <!-- SpringCloud離不開SpringBoot,所以必須要配置此依賴包 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build> --></project>
#debug=true
server.port=7900logging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%neureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=client
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
#eureka.instance.hostname=clientName
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);}
}
?
總結
以上是生活随笔為你收集整理的Eureka Client的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eureka Server
- 下一篇: Eureka的高可用