Eureka服務提供者、服務消費者

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

<?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注冊中心 依賴的是server的jar包、服務提供者的依賴包是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;

//消費者提供
@SpringBootApplication
@EnableEurekaClient //在程序類啟動的時候像eureka注冊中心注冊一個服務
@RestController //restful注解
public class EurekaApplication {

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

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

spring:
  application:
    name: service-hi #后續(xù)通過這里配置的名字進行調用服務

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

注冊完會看到一個叫service-hi的服務端口號為8091 那么就代表我們的服務注冊成功了


image.png

服務消費者(Feign與、RestTemplate的調用)

Feign的調用

//在SpringBootApplication啟動類上打上@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服務
public class ServicefeignapplicationApplication {

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

創(chuàng)建一個service接口類去調用服務

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

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

給web層暴露一個controller可以進行訪問方法

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 {
    //這里會進行報錯,啟動沒有任何問題 原因是因為無法識別 差找不到對應的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 服務名
eureka:
  client:
   serviceUrl:
    defaultZone: http://localhost:8761/eureka/

2.采用RestTemplate調用服務

啟動類

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);
    }

    //開啟負載均衡
    @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;

    //調用service服務中心的數據
    public String getMessage(){
        System.out.println("調用注冊的服務--->");
        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  #設置端口為80
eureka:
  client:
    serviceUrl:
     defaultZone : http://localhost:8761/eureka/
spring:
  application:
    name: service-ribbon

@EnableEurekaClient采用Eureka服務注冊中心的時候采用
@EnableDiscoveryClient使用其他服務注冊中心的時候采用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發(fā)現,斷路器,智...
    卡卡羅2017閱讀 134,981評論 19 139
  • (git上的源碼:https://gitee.com/rain7564/spring_microservices_...
    sprainkle閱讀 15,139評論 17 20
  • 這周因為各種事,周總結來得有些晚。昨天發(fā)了一條豆瓣廣播:就要過完學生時代所有的假期了,從此再無寒暑假,好惆悵啊。想...
    South_Lin閱讀 296評論 0 1
  • 寫這篇文章,是為了和朋友們交流學習。在我30歲的時候,因為家里的變故和工作的不順,導致自己產生了嚴重的心理疾病。...
    沙之家閱讀 591評論 0 2
  • 夜晚,很安靜的時刻,沒有了白天的喧雜音,和各種人事。這也是我想事的時候,想想今天又要這樣的過去了,時間真的很快。這...
    斌郭貓閱讀 396評論 0 3