springcloud服務發現(feign)
阿新 • • 發佈:2019-01-30
服務註冊:http://blog.csdn.net/qq_40075229/article/details/78978853
1.新建maven(war)專案:springcloud-feignclient
pom.xml新增依賴:spring-cloud-starter-feign
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
2.src/main/resources目錄下新建application.yml
該檔案配置:
server:
port: 8066
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: feignclient
#服務名不能帶"_" ,多個相同的服務的服務名必須相同
#配置賦值均衡演算法 :隨機演算法
#被呼叫的服務的服務名
SENDMAIL:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
BestAvailableRule 選擇一個最小的併發請求的server
AvailabilityFilteringRule 過濾掉那些因為一直連線失敗的被標記為circuit tripped的後端server,並過濾掉那些高併發的的後端server(active connections 超過配置的閾值)
WeightedResponseTimeRule 根據相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。
RetryRule對選定的負載均衡策略機上重試機制。
RoundRobinRuleroundRobin方式輪詢選擇server
RandomRule隨機選擇一個server
ZoneAvoidanceRule複合判斷server所在區域的效能和server的可用性選擇server
3.新增介面SendEmail
@FeignClient("SENDMAIL") //被呼叫的服務的服務名
public interface SendEmail {
//該方法與被呼叫的服務中的方法一致
@PostMapping("/psend")
public String sendMail(@RequestBody Map<String,Object> map);
}
4.新建contrller:SendController
@RestController
public class SendController {
@Autowired
private SendEmail sendEmail;
//測試負載均衡
/*@Autowired
private LoadBalancerClient loadBalancer;
@RequestMapping("/choosePub")
public String choosePub() {
StringBuffer sb=new StringBuffer();
for(int i=0;i<=10;i++) {
ServiceInstance ss=loadBalancer.choose("SENDMAIL");//從兩個sendmail中選擇一個 這裡涉及到選擇演算法
sb.append(ss.getUri().toString()+"<br/>");
}
return sb.toString();
} */
@PostMapping("/mail")
public String sendMail(String send_to,String send_subject,String send_content){
String results =null;
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("send_to", send_to);
map.put("send_subject", send_subject);
map.put("send_content", send_content);
//呼叫SENDMAIL服務中的方法
results = sendEmail.sendMail(map);
} catch (Exception e) {
return "傳送失敗";
}
return results;
}
}
5.webapp目錄新建 send.html
<form action="/mail" method="post">
郵件接收人:<input type="text" name="send_to" value="郵件接收人的郵箱"><br/>
郵件主題:<input type="text" name="send_subject" value="柒個我"><br/>
郵件內容:<textarea rows="40" cols="50" name="send_content" >柒個不一樣的我</textarea><br>
<input type="submit" value="提交">
</form>
6.啟動類:Main
@EnableEurekaClient
@EnableFeignClients //開啟feign客戶端
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
程式啟動順序: springcloud-eurekaserver-->springboot-sendemail-->springcloud-feignclient
瀏覽器訪問:http://localhost:8066/send.html 點選提交
頁面出現傳送成功,則代表服務呼叫服務成功
1.新建maven(war)專案:springcloud-feignclient
pom.xml新增依賴:spring-cloud-starter-feign
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
2.src/main/resources目錄下新建application.yml
該檔案配置:
server:
port: 8066
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: feignclient
#服務名不能帶"_" ,多個相同的服務的服務名必須相同
#配置賦值均衡演算法 :隨機演算法
#被呼叫的服務的服務名
SENDMAIL:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
BestAvailableRule 選擇一個最小的併發請求的server
AvailabilityFilteringRule 過濾掉那些因為一直連線失敗的被標記為circuit tripped的後端server,並過濾掉那些高併發的的後端server(active connections 超過配置的閾值)
WeightedResponseTimeRule 根據相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。
RetryRule對選定的負載均衡策略機上重試機制。
RoundRobinRuleroundRobin方式輪詢選擇server
RandomRule隨機選擇一個server
ZoneAvoidanceRule複合判斷server所在區域的效能和server的可用性選擇server
3.新增介面SendEmail
@FeignClient("SENDMAIL") //被呼叫的服務的服務名
public interface SendEmail {
//該方法與被呼叫的服務中的方法一致
@PostMapping("/psend")
public String sendMail(@RequestBody Map<String,Object> map);
}
4.新建contrller:SendController
@RestController
public class SendController {
@Autowired
private SendEmail sendEmail;
//測試負載均衡
/*@Autowired
private LoadBalancerClient loadBalancer;
@RequestMapping("/choosePub")
public String choosePub() {
StringBuffer sb=new StringBuffer();
for(int i=0;i<=10;i++) {
ServiceInstance ss=loadBalancer.choose("SENDMAIL");//從兩個sendmail中選擇一個 這裡涉及到選擇演算法
sb.append(ss.getUri().toString()+"<br/>");
}
return sb.toString();
} */
@PostMapping("/mail")
public String sendMail(String send_to,String send_subject,String send_content){
String results =null;
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("send_to", send_to);
map.put("send_subject", send_subject);
map.put("send_content", send_content);
//呼叫SENDMAIL服務中的方法
results = sendEmail.sendMail(map);
} catch (Exception e) {
return "傳送失敗";
}
return results;
}
}
5.webapp目錄新建 send.html
<form action="/mail" method="post">
郵件接收人:<input type="text" name="send_to" value="郵件接收人的郵箱"><br/>
郵件主題:<input type="text" name="send_subject" value="柒個我"><br/>
郵件內容:<textarea rows="40" cols="50" name="send_content" >柒個不一樣的我</textarea><br>
<input type="submit" value="提交">
</form>
6.啟動類:Main
@EnableEurekaClient
@EnableFeignClients //開啟feign客戶端
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
程式啟動順序: springcloud-eurekaserver-->springboot-sendemail-->springcloud-feignclient
瀏覽器訪問:http://localhost:8066/send.html 點選提交
頁面出現傳送成功,則代表服務呼叫服務成功