栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud服务治理Eureka(实践)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud服务治理Eureka(实践)

1、搭建Provider(服务端)

导入依赖

  
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            3.1.0
        
    

编写实体类

@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;
    
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/goods/{id}")
    public Goods findOrderByid(@PathVariable("id") Integer id){
        System.out.println("consumer"+id);

        
        List 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();
        
        //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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936317.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号