SpringCloud

论坛 期权论坛     
选择匿名的用户   2021-5-28 02:11   49   0
<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 &#43; SpringBoot 2.1.6.RELEASE &#43; 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">&lt;dependencies&gt;
    &lt;!-- 导入 SpringCloud 需要使用的依赖信息 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
        &lt;version&gt;Greenwich.SR2&lt;/version&gt;
        &lt;type&gt;pom&lt;/type&gt; &lt;!-- import 依赖范围表示将 spring-cloud-dependencies 包中的依赖信息导入 --&gt;
        &lt;scope&gt;import&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;!-- 导入 SpringBoot 需要使用的依赖信息 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
        &lt;version&gt;2.1.6.RELEASE&lt;/version&gt;
        &lt;type&gt;pom&lt;/type&gt;
        &lt;scope&gt;import&lt;/scope&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;</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">&#64;RestController
public class EmployeeHandler {

    &#64;RequestMapping(&#34;/provider/get/employee/remote&#34;)
    public Employee getEmployeeRemote() {
        return new Employee(555, &#34;tom555&#34;, 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">&#64;Configuration
public class SpringCloudConfig {

    &#64;Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}</code></pre>
<p>编写handler,远程调用方法。</p>
<pre class="blockcode"><code class="language-java">&#64;RestController
public class HumanResourceHandler {

    &#64;Autowired
    private RestTemplate restTemplate;

    &#64;RequestMapping(&#34;/consumer/get/employee&#34;)
    public Employee getEmployeeRemote(){
        // 1.远程调用主机地址
        String host&#61;&#34;http://localhost:1000&#34;;
        // 2.远程调用具体地址
        String url&#61;&#34;/provider/get/employee/remote&#34;;
        // 3.调用远程方法
        return restTemplate.getForObject(host&#43;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">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-netflix-eureka-server&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
<p>在SpringBoot启动类上,开启EurekaServer注解。</p>
<pre class="blockcode"><code class="language-java">&#64;EnableEurekaServer
&#64;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
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP