eureak

一:编写eureak服务

1.jar包依赖

web:web 

Colud  Dicovery:Eureka Server 

2.编写application.properties

#设置端口号
server.port=8761
#主机的名字
eureka.instance.hostname=eureak-service
#不希望将自己这个工程注册到注册中心(如果考虑到eureka的高可用)
eureka.client.register-with-eureka=false
#不从eureka上获取到货物的注册信息(本身eureka服务,不是消费者)
eureka.client.fetch-registry=false
#注定注册中心
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

3.启动类中设置该项目为一个eureka服务

    @SpringBootApplication
    @EnableEurekaServer			//将该项目表示为一个eureka服务

二:编写服务提供者

1.jar包依赖

Colud Dicovery:Eureka Discovery

2.编写application.properties

#设置启动端口号
server.port=8081
#设置注册到注册中心的应用程序名称
spring.application.name=item-service
#指定注册中心的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#注册的时候使用的服务的ip地址进行注册
eureka.instance.prefer-ip-address=true

3.编写service

@Service 
public class ItemService{

    public String getItemById(String id){
            return "您查询的是**"+id+"**号商品";
    }
}

4.编写controller

@RestController
public class  controller{
    
    @Autowired
    private  ItemService   itemService;
    @GetMapping("/item/{id}")
    public String  item(@PathVariable("id") String id){
        return  itemService.getItemById(id);
        }

}

三:服务的消费者

1.依赖jar包

WEB:web
Colud Dicovery:Eureka Server

2.编写配置文件

#端口
server.port=8888
#应用名
spring.application.name=item-web
#指定注册中心的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#注册的时候也采用ip地址注册
eureka.instance.prefer-ip-address=true

3.编写启动类

@SpringBootApplication
@EnableDiscoveryClient    //开启发现服务的功能
public   class  ItemWebApplication{
    
    public static void main(String[] args){
            SpringApplication.run(ItemWebApplication.class,args);
        }
        //eureka是通过发送http请求json数据来实现通讯
       @Bean
       public RestTemplate  restTemplate(){
           return  new   RestTemplate();
       }

}

4.编写controller

@RestController
public class UserContorller {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/buy/{id}")
    public String BuyItem(@PathVariable("id") String id){
        //第一个表示请求的url:
        //返回的数据类型String
        String str = restTemplate.getForObject("http://192.168.25.1:8081/item/" + id, String.class);
        return str;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

brid_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值