Eureka服務(wù)提供者、服務(wù)消費(fèi)者

1.Eureka在上一章節(jié)我們已經(jīng)創(chuàng)建成功,初始化的Eureka是沒有服務(wù)注冊(cè)的。那么這一章節(jié)就開始創(chuàng)建服務(wù)提供者。

<?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.example</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>
<!--區(qū)別一下Eureka注冊(cè)中心 依賴的是server的jar包、服務(wù)提供者的依賴包是client~~-->
    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.example.eureka;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//消費(fèi)者提供
@SpringBootApplication
@EnableEurekaClient //在程序類啟動(dòng)的時(shí)候像eureka注冊(cè)中心注冊(cè)一個(gè)服務(wù)
@RestController //restful注解
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

    //端口號(hào)
    @Value("${server.port}")
    private String port;
    //提供一個(gè)hi的測(cè)試方法
    @RequestMapping(value = "/hi")
    private String sayHei(){
        return "hello Word"+port+"test";
    }
}
yml配置文件配置的相關(guān)信息
server:
  port: 8091 #設(shè)置端口號(hào)

spring:
  application:
    name: service-hi #后續(xù)通過(guò)這里配置的名字進(jìn)行調(diào)用服務(wù)

eureka:
  client:
    serviceUrl: 
     defaultZone: http://localhost:8761/eureka/  #表名自己的服務(wù)中心的地址

注冊(cè)完會(huì)看到一個(gè)叫service-hi的服務(wù)端口號(hào)為8091 那么就代表我們的服務(wù)注冊(cè)成功了


image.png

服務(wù)消費(fèi)者(Feign與、RestTemplate的調(diào)用)

Feign的調(diào)用

//在SpringBootApplication啟動(dòng)類上打上@EnableFeignClients注解

package com.example.servicefeignapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients//開啟feign服務(wù)
public class ServicefeignapplicationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServicefeignapplicationApplication.class, args);
    }
}

創(chuàng)建一個(gè)service接口類去調(diào)用服務(wù)

package com.example.servicefeignapplication.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Auther: 創(chuàng)建一個(gè)接受服務(wù)的接口 FeignClinet表示調(diào)用的服務(wù)
 * @Date: 2018/7/18 0018 09:54
 * @Description:
 */
@FeignClient(value = "service-hi")
public interface ServiceTest {
    //調(diào)用服務(wù)名為service-hi的服務(wù)方法
    @RequestMapping(value = "/hi")
    public String say();
}

給web層暴露一個(gè)controller可以進(jìn)行訪問(wèn)方法

package com.example.servicefeignapplication.controller;

import com.example.servicefeignapplication.service.ServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: TG
 * @Date: 2018/7/18 0018 09:56
 * @Description:
 */
@RestController
public class ServiceController {
    //這里會(huì)進(jìn)行報(bào)錯(cuò),啟動(dòng)沒有任何問(wèn)題 原因是因?yàn)闊o(wú)法識(shí)別 差找不到對(duì)應(yīng)的bean
    @Autowired
    private ServiceTest serviceTest;

    @GetMapping(value = "hello")
    public String hello(){
        System.out.println(serviceTest.say());
        return serviceTest.say();
    }
}

yml文件的配置

server:
  port: 8888
spring:
  application:
    name: serverBase 服務(wù)名
eureka:
  client:
   serviceUrl:
    defaultZone: http://localhost:8761/eureka/

2.采用RestTemplate調(diào)用服務(wù)

啟動(dòng)類

package com.example.serviceribbon;

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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient 
public class ServiceRibbonApplication {

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

    //開啟負(fù)載均衡
    @Bean
    @LoadBalanced
     RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
service
package com.example.serviceribbon.service;

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

/**
 * @Auther: TG
 * @Date: 2018/7/17 0017 15:06
 * @Description:
 */
@Service
public class RibbonService {

    @Autowired
    private RestTemplate restTemplate;

    //調(diào)用service服務(wù)中心的數(shù)據(jù)
    public String getMessage(){
        System.out.println("調(diào)用注冊(cè)的服務(wù)--->");
        return restTemplate.getForObject("http://service-hi/hi",String.class);
    }
}
package com.example.serviceribbon.service;

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

/**
 * @Auther: TG
 * @Date: 2018/7/17 0017 15:09
 * @Description:
 */
@RestController
public class RibbonController {

    @Autowired
    private RibbonService ribbonService;

    @RequestMapping(value = "sayBase")
    public String sayBase(){
        return ribbonService.getMessage();
    }
}
yml配置文件配置
server:
  port: 80  #設(shè)置端口為80
eureka:
  client:
    serviceUrl:
     defaultZone : http://localhost:8761/eureka/
spring:
  application:
    name: service-ribbon

@EnableEurekaClient采用Eureka服務(wù)注冊(cè)中心的時(shí)候采用
@EnableDiscoveryClient使用其他服務(wù)注冊(cè)中心的時(shí)候采用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,837評(píng)論 18 139
  • (git上的源碼:https://gitee.com/rain7564/spring_microservices_...
    sprainkle閱讀 15,128評(píng)論 17 20
  • 這周因?yàn)楦鞣N事,周總結(jié)來(lái)得有些晚。昨天發(fā)了一條豆瓣廣播:就要過(guò)完學(xué)生時(shí)代所有的假期了,從此再無(wú)寒暑假,好惆悵啊。想...
    South_Lin閱讀 288評(píng)論 0 1
  • 寫這篇文章,是為了和朋友們交流學(xué)習(xí)。在我30歲的時(shí)候,因?yàn)榧依锏淖児屎凸ぷ鞯牟豁?,?dǎo)致自己產(chǎn)生了嚴(yán)重的心理疾病。...
    沙之家閱讀 589評(píng)論 0 2
  • 夜晚,很安靜的時(shí)刻,沒有了白天的喧雜音,和各種人事。這也是我想事的時(shí)候,想想今天又要這樣的過(guò)去了,時(shí)間真的很快。這...
    斌郭貓閱讀 394評(píng)論 0 3