建立一個項目(用的是idea開發工具) (1) 如圖進行選擇和配置 前端
server:
port: 9999 #服務註冊中心端口號
eureka:
instance:
hostname: 127.0.0.1 #服務註冊中心IP地址
client:
registerWithEureka: false #是否向服務註冊中心註冊本身
fetchRegistry: false #是否檢索服務
serviceUrl: #服務註冊中心的配置內容,指定服務註冊中心的位置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
複製代碼
(4) 啓動看效果 java
#####2、先建立一個服務者(並註冊到eureka上) (1)建立一個項目,跟建立euraka項目大體選擇同樣,就不上圖了。只有選擇依賴的時候,多選一個 spring web。由於服務者要提供服務未來要用到controller這種註解。 (2)修改代碼。 使其變成eureka的客戶端:在Spring-boot的啓動類上經過註解@EnableEurekaClient 代表本身是一個eurekaclient. 提供服務:寫一個controller,並編寫具體接口。web
package com.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class BasketballController {
/**
* 假如這個客戶端要提供一個getUser的方法
* @return
*/
@GetMapping(value = "/buy")
@ResponseBody
public Map<String,Object> getUser(){
Map<String,Object> data = new HashMap<>();
// data.put("id",id);
data.put("size","7#");
data.put("from","陽光籃球場");
return data;
}
}
複製代碼
(3)配置調整 將application.properties 改成 application.yml,並修改內容spring
eureka:
client:
serviceUrl: #註冊中心的註冊地址
defaultZone: http://127.0.0.1:9999/eureka/
server:
port: 8081 #服務端口號
spring:
application:
name: service-provider #服務名稱--調用的時候根據名稱來調用該服務的方法
複製代碼
(4) 啓動看效果 bash
#####3、該來人買籃球了:建立一個消費者(並註冊到eureka上) (1)建立一個項目,選擇與服務者同樣。要選擇eureka和spring web,由於也要發起請求。 (2)修改代碼,使其變成eureka的客戶端並能夠調用服務。(我在裏面加了其餘的註解,喜歡研究的能夠嘗試的去掉看看有沒有影響)服務器
package com.eureka.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableEurekaClient
@EntityScan
@ServletComponentScan
@ComponentScan
@RestController
public class ConsumerApplication {
@Autowired
RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
/**
* Rest服務端使用RestTemplate發起http請求,而後獲得數據返回給前端
*
* @return
*/
@GetMapping(value = "/getUser")
@ResponseBody
public Map<String, Object> getUser() {
Map<String, Object> data = restTemplate.getForObject("http://service-provider/buy", Map.class);
return data;
}
}
複製代碼
(3)別忘了修改配置文件,使其註冊到eureka上。app
eureka:
client:
serviceUrl: #註冊中心的註冊地址
defaultZone: http://127.0.0.1:9999/eureka/
server:
port: 9000 #服務端口號
spring:
application:
name: service-consumer #服務名稱--調用的時候根據名稱來調用該服務的方法
複製代碼
(4)啓動看效果 eureka上已經有了這個instance了。 負載均衡
下一篇寫eureka的簡單負載均衡。dom