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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值