SpringCloud服务治理Eureka(实践)

1、搭建Provider(服务端)

导入依赖

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

编写实体类

/*服务端的实体类*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Goods {
    private  Integer id;
    private String title; //标题
    private Double price; //价格
    private int count ; //商品库存
}

编写dao

@Repository
public class GoodsDao {

    public Goods findOne (Integer id){
        return new Goods(1,"华为手机",12.3,6);
    }
}

编写service

@Service
public class goodsService {
    @Autowired
    private GoodsDao goodsDao;

    public Goods findOne (Integer id){
        return goodsDao.findOne(id);
    }
}

编写controller

/*
* *  服务的提供方*/
@RestController
@RequestMapping("/goods")
public class goodsController {
    @Autowired
    private com.xiaokai.service.goodsService goodsService;
    @GetMapping("/findOne/{id}")
    public Goods findOne (@PathVariable("id") Integer id){
        Goods one = goodsService.findOne(id);
        return one;
    }
}

编写配置文件

server:
  port: 8081
  #主机名
eureka:
  instance:
    hostname: localhost
  #eureka服务端的地址
  client:
   service-url:
     #eureka服务端地址 将来客户端使用该地址和eureka进行通信
    defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: eureka-provider #设置当前应用的名称 将来在eureka中Application中显示 将来需要使用该名称来获取路径

2、搭建consumer(消费者)

导入依赖,跟服务端依赖相同

配置RestTemplam模板

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplam {
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }
}

编写controller

/*消费者调用服务端的接口 模拟
*  服务的调用方
*
* */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    /*Could包里的*/
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/goods/{id}")
    public Goods findOrderByid(@PathVariable("id") Integer id){
        System.out.println("consumer"+id);

        /*动态获取Eureka中 provider的IP地址和端口号
        * 注入 DiscoveryClient对象
        * 调用方法
        * */
        List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
        if (instances == null || instances.size()==0){
            return null;
        }
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        /*远程掉头provide里面的goods的方法*/
        //1、定义Bean RestTemplate
        //2、注入bean
        //3、调用方法 (注意是org.springframework.web.client.RestTemplate包里的)

        String url="http://"+host+":"+port+"/goods/findOne/"+id;
        Goods forObject = restTemplate.getForObject(url, Goods.class);
        return forObject;
    }
}

编写实体类(工作中不会这样写) 实体类和服务端一致  复制过来就行

编写配置文件

server:
  port: 8082
eureka:
  instance:
    hostname: localhost
    #eureka服务端的地址
  client:
    service-url:
        #eureka服务端地址 将来客户端使用该地址和eureka进行通信
        defaultZone: http://localhost:8761/eureka
spring:
  application:
     name: eureka-consumer #设置当前应用的名称 将来在eureka中Application中显示 将来需要使用该名称来获取路径

3、配置注册中心 Eureka

server.port=8761
#eurake配置  分为4个部分
# 1、eureka.service 服务端配置
# 2、eureka.client.客户端配置
# 3、eureka.instance 实例配置
# 4、eureka.dashboard web页面控制台配置

#主机名
eureka.instance.hostname=localhost
#eureka服务端的地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
#是否将自己的路径 注册到eruake  上  eureka.service不需要 eureka.client才需要
eureka.client.register-with-eureka=false 
#是否需要从eureka抓取路径    eureka.service不需要 eureka.client才需要
eureka.client.fetch-registry=false

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值