Eureka是什么在岂,SpringCloud用Eureka來(lái)干什么等等相關(guān)概念就不細(xì)說(shuō)了绍弟,網(wǎng)上有大把大把的解釋锐锣,甚至下面的文章都不需要看腌闯,因?yàn)榫W(wǎng)上也有大把大把的列子,為了省去搜索的時(shí)間雕憔,可以參考:
Spring資料大全 中的SpringCloud相關(guān)文檔姿骏。本文的目的只是自己記錄一下
目的
- Eureka服務(wù)中心搭建
- 其他服務(wù)注冊(cè)到Eureka服務(wù)中心
- Eureka服務(wù)中心高可用
- Eureka服務(wù)中心相關(guān)配置詳解
需要三個(gè)項(xiàng)目: eureka-server:服務(wù)注冊(cè)中心,base-service-producer:服務(wù)提供方斤彼,hello-service:服務(wù)調(diào)用方(也可以是另外一個(gè)服務(wù))
Eureka服務(wù)中心搭建
新建項(xiàng)目:eureka-server
添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
添加注解
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
EurekaDiscoveryClient eurekaDiscoveryClient;
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
啟動(dòng)
訪問(wèn) http://localhost:1111/
其他服務(wù)注冊(cè)到Eureka服務(wù)中心
新建項(xiàng)目:base-service-producer
添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
配置文件
spring.application.name=base-service-producer
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=10003
啟動(dòng)
訪問(wèn):http://localhost:10003/base-service 有響應(yīng)分瘦,同時(shí),訪問(wèn)http://localhost:1111/eureka琉苇,發(fā)現(xiàn)base-service-producer:10003被注冊(cè)到注冊(cè)中心了
新建項(xiàng)目:hello-service
添加依賴
因?yàn)橹皇钦{(diào)用方嘲玫,該依賴可以不添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
配置文件
因?yàn)橹皇钦{(diào)用方,該配置可以不添加
#服務(wù)命名
spring.application.name=hello-service
#指定服務(wù)注冊(cè)中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=10000
#需要啟動(dòng)eureka-server后啟動(dòng),再看 http://localhost:1111/ 會(huì)發(fā)現(xiàn)hello-service已經(jīng)注冊(cè)到服務(wù)中心中去了
添加注釋
因?yàn)橹皇钦{(diào)用方并扇,該@EnableDiscoveryClient
可以不添加
@EnableDiscoveryClient
@SpringBootApplication
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
調(diào)用服務(wù)
@RequestMapping("/base")
public String baseSerivce() {
ServiceInstance instance = discoveryClient.getInstances("base-service-producer").get(0);
System.out.println(instance);
String url = "http://"+instance.getHost() + ":" + instance.getPort() + "/base-service";
String result = restTemplate.getForObject(url, String.class);
return result;
}
啟動(dòng)
當(dāng)然趁冈,是在服務(wù)中調(diào)用服務(wù),應(yīng)該再建立一個(gè)項(xiàng)目,訪問(wèn)hello-service的hello
Eureka服務(wù)中心高可用
application-peer1.properties
spring.application.name=high-eureka-service
server.port=1111
eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
#需要在/etc/hosts中 加上 ip到host的映射 127.0.0.1 peer2
#在target 中運(yùn)行
#java -jar high-eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
#java -jar high-eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
#后訪問(wèn) http://localhost:1112/ http://localhost:1111/ 可用看到將兩個(gè)注冊(cè)中心都當(dāng)做服務(wù)注冊(cè)到了注冊(cè)中心中
application-peer2.properties
spring.application.name=high-eureka-service
server.port=1112
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
#需要在/etc/hosts中 加上 ip到host的映射 127.0.0.1 peer1
啟動(dòng)
訪問(wèn) http://localhost:1111/
訪問(wèn) http://localhost:1112/
兩者互相注冊(cè)