從 0 搭建 Spring Cloud 服務,完整教程!
逆鋒起筆關注后回復編程pdf領取編程大佬們所推薦的 23 種編程資料!
一、微服務基礎
1.什么是 SpringCloud?
二、微服務的搭建
2.1 工程初始化配置













<?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.yun</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springcloud-eureka-server</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!--引入springboot-parent父項目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<!--引入springcloud的euekea server依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!--指定下載源和使用springcloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>


2.2 Springboot的搭建 以及提供注冊服務 的 服務配置





server:
port: 8700 # 端口自己決定
# 指定當前eureka客戶端的注冊地址,也就是eureka服務的提供方,當前配置的服務的注冊服務方
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false #自身 不在向eureka注冊
fetch-registry: false #啟動時禁用client的注冊
instance:
hostname: localhost
#指定應用名稱
spring:
application:
name: eureka-server


package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //當前使用eureka的server
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}



2.3 客戶端client 提供真正服務的角色的配置, 它提供服務 在 服務注冊方server (注冊中心)進行注冊






server:
port: 8701 # 服務提供方
# 指定當前eureka客戶端的注冊地址,
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8700/eureka
instance:
hostname: localhost
#當前服務名稱
spring:
application:
name: eureka-service

package com.yun;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/Hello")
public class Controller {
@RequestMapping("/World")
public String helloWorld(String s){
System.out.println("傳入的值為:"+s);
return "傳入的值為:"+s;
}
}
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient//代表自己是一個服務提供方
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class,args);
}
}



2.4 服務的調(diào)用方式
第一種調(diào)用方式:restTemplate+ribbon


第二種調(diào)用方式:feign

2.4.1 restTemplate+ribbon
而客戶端負載均衡和服務端負載均衡最大的不同點在于上面所提到服務清單所存儲的位置。在客戶端負載均衡中,所有客戶端節(jié)點都維護著自己要訪問的服務端清單,而這些服務端端清單來自于服務注冊中心,比如上一章我們介紹的Eureka服務端。同服務端負載均衡的架構類似,在客戶端負載均衡中也需要心跳去維護服務端清單的健康性,默認會創(chuàng)建針對各個服務治理框架的Ribbon自動化整合配置,比如Eureka中的org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration,Consul中的org.springframework.cloud.consul.discovery.RibbonConsulAutoConfiguration。在實際使用的時候,我們可以通過查看這兩個類的實現(xiàn),以找到它們的配置詳情來幫助我們更好地使用它。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
server:
port: 8702 # 服務消費方
# 指定當前eureka客戶端的注冊地址,
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8700/eureka
instance:
hostname: localhost
#當前服務名稱
spring:
application:
name: eureka-consumer
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //當前使用eureka的server
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class,args);
}
}

package com.yun.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/Hello")
class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/Consumer")
public String helloWorld(String s){
System.out.println("傳入的值為:"+s);
//第一種調(diào)用方式
//String forObject = new RestTemplate().getForObject("http://localhost:8071/Hello/World?s=" + s, String.class);
//第二種調(diào)用方式
//根據(jù)服務名 獲取服務列表 根據(jù)算法選取某個服務 并訪問某個服務的網(wǎng)絡位置。
//ServiceInstance serviceInstance = loadBalancerClient.choose("EUREKA-SERVICE");
//String forObject = new RestTemplate().getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/Hello/World?s="+s,String.class);
//第三種調(diào)用方式 需要restTemplate注入的方式
String forObject = restTemplate.getForObject("http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);
return forObject;
}
}
@Autowired private LoadBalancerClient loadBalancerClient;
package com.yun.beans;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Beans {
//管理簡單對象
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
String forObject = restTemplate.getForObject("http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);




訪問服務消費方@RequestMapping指定的路徑及消費方的端口來訪問消費方的controller controller根據(jù)服務名去server方獲取獲取服務列表,獲取服務列表后根據(jù)隨機的模式負載勻衡后去選擇服務地址去訪問servicesupport:如下圖

2.5 Eureka server的高可用配置























作者:Anakki
來源:blog.csdn.net/qq_29519041/article/details/85238270
逆鋒起筆是一個專注于程序員圈子的技術平臺,你可以收獲最新技術動態(tài)、最新內(nèi)測資格、BAT等大廠大佬的經(jīng)驗、增長自身、學習資料、職業(yè)路線、賺錢思維,微信搜索逆鋒起筆關注!
最近好文
支持下
評論
圖片
表情

