springcloud Eureka(续)

不了解Eureka的可以看我以前的文章 springcloud Eureka

使用Eureka

需要先写一个Eureka的服务中心
创建一个module为例
EurekaService

导入依赖

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.rpf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7001</artifactId>
    <!--导包-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

依赖了一些父工程的配置

编写配置文件 properties.yml

设置服务中心的端口号

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #是否向Eureka注册中心注册自己
    fetch-registry: false  #fetch-registry如果为false ,表示自己为注册中心
    service-url:   #监控页面地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

编写一个启动类

@SpringBootApplication
@EnableEurekaServer//开启Eureka服务   服务端的启动类 可以接收别人注册进来
public class EurekaServer_7001{
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

注册中心就编写好了

把我们之前写的服务提供者注册进来

在服务提供者中加入Eureka依赖

    <!--加入Eureka依赖-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

编写配置文件

#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

启动类上开启Eureka使用的是Client

//启动类
@EnableEurekaClient//在服务启动后自动注册到Eureka服务端
@SpringBootApplication
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}

启动测试

访问7001端口 查看注册信息
在这里插入图片描述
注册成功

消费者拉取服务
在这里插入图片描述
导入依赖

<!--需要Eureka发现服务-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

yml配置

配置取服务的地址

server:
  port: 8005
#Eureka配置
eureka:
  client:
    register-with-eureka: false  #  不向Eureka中注册自己  消费者只需要拿不用注册自己
    service-url: #去哪取服务
      defaultZone: http://127.0.0.1:7001/eureka/

创建一个config 注入Rest模板

@Configuration//@Configuration 相当于spring中的 application.xml
public class ConfigBean {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }


}

controller 调用接口

@RestController
public class DeptConsumerController {
   //RestTemplate   供我们直接调用 使用需要注册到Spring中

    //三个参数(url,实体:map,Class<T>responseType,)
    @Autowired
    RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法,简单的restful服务模板~

    //前缀是固定的   http://localhost:8001
    private static final String REST_URL_PREFIX="http://localhost:8001";

    @RequestMapping("consumer/dept/get/{id}")
    public Dept get(@PathVariable("id")Long id){//Dept.class响应的类型
         return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }
    @RequestMapping("/consumer/dept/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }
}

启动类开启Eureka

@EnableEurekaClient//开启Eureka
@SpringBootApplication
public class DeptConsumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer_80.class,args);
    }
}

测试访问
在这里插入图片描述

使用功能的基本步骤

导入依赖
编写配置文件
开启功能EnableXXX
可能写配置类
在这里插入图片描述
更改之后
在这里插入图片描述
修改描述信息在生产者yml配置

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.1:7003/eureka/,http://127.0.0.1:7002/eureka/
  instance:
    instance-id: springcloud-provider-dept8001   #修改eureka上的默认描述信息
info:
  app.name: feifei-springcloud  #项目名字
  company.name: www.badu.com  #公司名字

重启就可以实现

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值