1. 程式人生 > >Spring Cloud 入門教程(二): 服務消費者(rest+ribbon)(Greenwich.RELEASE)

Spring Cloud 入門教程(二): 服務消費者(rest+ribbon)(Greenwich.RELEASE)

一、準備工作,eclipse執行兩個例項

1、修改  eurekaclient1 中 application.yml 檔案

server:
  port: 8762
spring:
  application:
    name: eurekaclient
eureka:
  instance:
     instance-id: eurekaclient1
     appname: ${spring.application.name}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

2、同級目錄下複製一份 application.yml 檔案 命名為  application-two.yml

server:
  port: 8763
spring:
  application:
    name: eurekaclient
eureka:
  instance:
     instance-id: eurekaclient2
     appname: ${spring.application.name}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

3、啟動  EurekaClientApplication.java (第一個例項)

4、啟動 EurekaClientApplication.java  (第二個例項)

右鍵->Run As  -> Run Configurations.. ->Arguments-> Programe Arguments 輸入 --spring.profiles.active=two   點選 run

5、檢視 多個例項

6、http://localhost:8761

 

二、新建maven 工程 service-ribbon (服務消費者 )

1、修改pom.xml

<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>wg</groupId>
  <artifactId>service-ribbon</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>service-ribbon</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    <java.version>1.8</java.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

2、建立啟動類(位於所有類的上級包下)

package wg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {
	public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

3、建立application.yml

server:
  port: 8764
spring:
  application:
    name: service-ribbon
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4、建立service 

package wg.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
	@Autowired
    RestTemplate restTemplate;

    public String helloService() {
        return restTemplate.getForObject("http://EUREKACLIENT/",String.class);
    }
}

5、建立controller

package wg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import wg.service.HelloService;

@RestController
public class HelloController {
	@Autowired
    HelloService helloService;

    @RequestMapping(value = "/hello")
    public String hi() {
        return helloService.helloService( );
    }
}

6、啟動專案,多次訪問:http://localhost:8764/hello

交替出現

7、訪問 http://localhost:8761/

相關推薦

Spring Cloud 入門教程(): 服務消費者rest+ribbon(Greenwich.RELEASE)

一、準備工作,eclipse執行兩個例項 1、修改  eurekaclient1 中 application.yml

Spring Cloud 入門教程(三): 服務消費者Feign(Greenwich.RELEASE)

一、準備工作 同上節 二、新建maven 工程 service-feign (服務消費者 ) 1、修改pom.xm

第二篇: 服務消費者rest+ribbon(Greenwich版本)

一、在前一章的基礎上啟動eureka-server和兩個eureka-client例項(在application.properties更改下埠號)新增一個模組service-ribbon 修改父工程的pom.xml檔案在<Modules>節點處增加ser

Spring Cloud入門教程服務消費者 Feign(Finchley版本+Boot2.0)

什麼是Feign?         Feign是受到Retrofit,JAXRS-2.0和WebSocket的影響,它是一個java的到http客戶端繫結的開源專案。 Feign的主要目標是將Java Http 客戶端變得簡單。 推薦部落格: 常見錯誤:

spring-cloud服務消費者rest+ribbon(Finchley版本)

在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign ribbon是一個負載均衡客戶端,可以很好的控制http和tcp的一些行為。Feign預

企業級 SpringCloud 教程 服務消費者rest+ribbon

一、ribbon簡介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbo

Spring Cloud之路: 服務消費者rest+ribbon

在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於ribbon+rest。 一、Rib

史上最簡單的SpringCloud教程 | 第二篇: 服務消費者rest+ribbon

image tree 開啟 then rom cat learn 替代 官網 最新Finchley版本:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/或者http://blog.csdn.n

企業級 SpringCloud 服務消費者rest+ribbon

ota ces 說明 源地址 地址 ted mapping rgs www 一、ribbon簡介 Ribbon is a client side load balancer which gives you a lot of control over the behavio

史上最簡單的SpringCloud教程 | 第二篇: 服務消費者rest+ribbon(Finchley版本)

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於rib

史上最簡單的SpringCloud教程 | 第二篇: 服務消費者rest+ribbon

2017年04月08日 23:25:26 226350人閱讀 評論(226) 收藏

Spring Cloud 入門教程(十一): 斷路器聚合監控(Hystrix Turbine)(Greenwich.RELEASE)

參考網址:https://blog.csdn.net/forezp/article/details/81041125 一、準備工

在eclipse上部署springcloud小例子--第二篇:服務消費者rest+ribbon

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於ribbon+rest。 &n

Springcloud電子商城系統 java B2B2C-服務消費者rest+ribbon

一、ribbon簡介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon

SpringCloud | 第二篇: 服務消費者rest+ribbon

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於r

SpringCloud2服務消費者rest+ribbon

1.準備工作 這一篇文章基於上一篇文章的工程。啟動eureka-server 工程,埠為 8761。分別以埠 8762 和 8763 啟動 service-hi 工程。訪問 localhost:8761 你會發現,service-hi 在eureka-server 註冊了2個例項,這就相當於一個小的叢集。

Spring Cloud 入門教程 - Eureka服務註冊與發現

spring spring cloud spring cloud eureka spring boot 簡介 在微服務中,服務註冊與發現對管理各個微服務子系統起著關鍵作用。隨著系統水平擴展的越來越多,系統拆分為微服務的數量也會相應增加,那麽管理和獲取這些微服務的URL就會變得十分棘手,如果我們

Spring Cloud 入門Eureka -Consumer服務消費

ppi package AR con .so 1.8 Coding ng- int 這裏介紹:LoadBalancerClient接口,它是一個負載均衡客戶端的抽象定義,下面我們就看看如何使用Spring Cloud提供的負載均衡器客戶端接口來實現服務的消費。 引用之前的文

Spring Cloud 入門Eureka -Consumer服務消費聲明式Feign

load control interface github example TP oca des 整合   Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。我們只需要通過創建接口並用

Spring Cloud 入門教程(一): 服務註冊

1.  什麼是Spring Cloud? Spring提供了一系列工具,可以幫助開發人員迅速搭建分散式系統中的公共元件(比如:配置管理,服務發現,斷路器,智慧路由,微代理,控制匯流排,一次性令牌,全域性鎖,主節點選舉, 分散式session, 叢集狀態)。協調分散式環境中各