<h3>SpringCloud核心</h3>
<blockquote>
<p>基于HTTP协议,这是它和Dubbo的最本质区别。Dubbo的核心是基于RPC。</p>
</blockquote>
<p><strong>Eureka</strong>:注册中心</p>
<p><strong>Ribbon</strong>:客户端负载均衡</p>
<p><strong>Feign</strong>:远程接口的声明式调用</p>
<p><strong>Hystrix</strong>:服务的熔断、降级、监控</p>
<p><strong>Zuul</strong>:网关</p>
<p><img alt="" height="407" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-0b9a0b5a9073975bfdf807a0c531cd76.png" width="924"></p>
<h1>一、环境搭建 </h1>
<blockquote>
<p>java 12 + SpringBoot 2.1.6.RELEASE + SpringCloud Greenwich.SR2</p>
</blockquote>
<p><strong> 1. SpringCloud必须与SpringBoot配合使用。创建父工程,集中管理依赖,不必每个项目都重新导依赖。</strong></p>
<p> <img alt="" height="283" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-7c7cc2d67354c2b55afc54362f3e439c.png" width="427"></p>
<pre class="blockcode"><code class="language-XML"><dependencies>
<!-- 导入 SpringCloud 需要使用的依赖信息 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type> <!-- import 依赖范围表示将 spring-cloud-dependencies 包中的依赖信息导入 -->
<scope>import</scope>
</dependency>
<!-- 导入 SpringBoot 需要使用的依赖信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies></code></pre>
<p><strong>2. 创建provider项目(导入SpringBoot Web、配置端口号)</strong></p>
<p><img alt="" height="325" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-b80b00fa625f6be2cf4d4c70a1d5255f.png" width="465"></p>
<p><strong>handler方法</strong></p>
<pre class="blockcode"><code class="language-java">@RestController
public class EmployeeHandler {
@RequestMapping("/provider/get/employee/remote")
public Employee getEmployeeRemote() {
return new Employee(555, "tom555", 555.55);
}
}</code></pre>
<p><strong>3. 创建consumer项目(导入SpringBoot Web、配置端口号)</strong></p>
<p><img alt="" height="381" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-07adddd7fd8d881c27a9c5c652180225.png" width="465"></p>
<p>编写配置类,注入RestTemplate类。</p>
<pre class="blockcode"><code class="language-java">@Configuration
public class SpringCloudConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}</code></pre>
<p>编写handler,远程调用方法。</p>
<pre class="blockcode"><code class="language-java">@RestController
public class HumanResourceHandler {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/consumer/get/employee")
public Employee getEmployeeRemote(){
// 1.远程调用主机地址
String host="http://localhost:1000";
// 2.远程调用具体地址
String url="/provider/get/employee/remote";
// 3.调用远程方法
return restTemplate.getForObject(host+url,Employee.class);
}
}</code></pre>
<p><strong>4. 启动 <span style="color:#3399ea;">provider </span>和 <span style="color:#3399ea;">consumer </span>项目,测试。</strong></p>
<p><img alt="" height="94" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-d1d5a2726765cc4efe869b09710251f2.png" width="554"></p>
<p><strong>5. 创建Eureka项目</strong></p>
<p><img alt="" height="302" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-b321c039e0dd58b29e6e113e91c64c75.png" width="372"></p>
<p>导入Eureka依赖</p>
<pre class="blockcode"><code class="language-XML"><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency></code></pre>
<p>在SpringBoot启动类上,开启EurekaServer注解。</p>
<pre class="blockcode"><code class="language-java">@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class);
}
}</code></pre>
<p>配置application.yml</p>
<pre class="blockcode"><code>server:
port: 5000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #自己就是注册中心,不需要注册自己
fetch-registry: false #自己就是注册中心,不需要“从注册中心取回信息”
service-url: #客户端访问Eureka的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/</code></pre>
<p><strong>6. 启动Eureka项目,出现注册界面,配置成功。</strong></p>
<p><img alt="" height="315" src="https://beijingoptbbs.oss-cn-b |
|