2.0.0版本spring boot中spring cloud的使用

一、註冊中心server服務器

1.eureka-server中需要的pom文件

<spring-cloud.version>Finchley.RC1</spring-cloud.version> <!--重點-->

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <!--相比2.0之前有改動-->
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<!--使用spring cloud 需要引入的依賴管理-->
<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>

2.配置文件

eureka.instance.hostname=127.0.0.1
eureka.client.registerWithEureka=false<!-- 是否將自己的服務註冊到eureka服務器-->
eureka.client.fetchRegistry=false<!--是否從eureka服務器獲取註冊信息-->
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

3.在啓動類中添加註解

@EnableEurekaServer

二、註冊服務client客戶端

1.eureka-client客戶端pom文件

<spring-cloud.version>Finchley.RC1</spring-cloud.version>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <!-- 對比2.0.0之前不同-->
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <!--對比2.0.0之前不同 -->
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<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>

2.配置文件

由於是使用yml格式的配置,所以要特別注意格式。
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/

3.啓動類中添加註解

@EnableEurekaClient

三、eureka服務器上服務之間數據的請求

1.這是eureka服務器中的兩個服務,現在要實現他們之間數據的交互。

2.在hello中來提供服務,在test01中使用服務

a.先是hello中的提供類的創建

@EnableEurekaClient // 一定加上
@RestController 
@RequestMapping("/rest/outer")
public class StduentFeign {

    @Autowired
    private TestService people;

    @GetMapping("/findAll")
    public List<Student> findAll() {

        return people.findAll();
    }
}

b.然後是test01中服務的使用,這裏使用接口接可以了。

@FeignClient(name="hello",fallback = StudentFeignImpl.class)
public interface StudentFeign {

    @GetMapping("/rest/outer/findAll")
    public List<Student> findAll();
}
@Component // 加上註解
public class StudentFeignImpl implements  StudentFeign {
    @Override
    public List<Student> findAll() {
        List<Student> students = new ArrayList<>();
        Student student = new Student();
        student.setClassGrade("1");
        student.setId(2);
        student.setName("32");
        student.setSetting("221");
        students.add(student);
        return students;
    }
}

在啓動類中添加@EnableFeignClients,然後服務之間的交互方能成功。

其中fallback的回調類中要起作用,必須在配置中添加

feign:
  hystrix:
    enabled: true

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章