Eureka(F版本)教程二 服务消费者(rest+ribbon)

访问localhost:8761如图所示: 如何一个工程启动多个实例,请看这篇文章:https://blog.csdn.net/forezp/article/details/76408139

三、建一个服务消费者


重新新建一个spring-boot工程,取名为:service-ribbon; 在它的pom.xml继承了父pom文件,并引入了以下依赖:

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

com.loren

spring-cloud-demo

1.0-SNAPSHOT

com.loren

service-ribbon

0.0.1-SNAPSHOT

service-ribbon

jar

Demo project for Spring Boot

1.8

Greenwich.SR2

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-ribbon

org.springframework.boot

spring-boot-maven-plugin

在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764。配置文件application.yml如下:

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

server:

port: 8764

spring:

application:

name: service-ribbon

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

package com.loren.serviceribbon;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

/**

  • @author

  • 在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;

  • 并且向程序的ioc注入一个bean: restTemplate;

  • 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

    */

    @SpringBootApplication

    @EnableEurekaClient

    @EnableDiscoveryClient

    public class ServiceRibbonApplication {

    public static void main(String[] args) {

    SpringApplication.run(ServiceRibbonApplication.class, args);

    }

    @Bean

    @LoadBalanced

    RestTemplate restTemplate() {

    return new RestTemplate();

    }

    }

    写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

    package com.loren.serviceribbon.service;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.stereotype.Service;

    import org.springframework.web.client.RestTemplate;

    @Service

    public class HelloService {

    @Autowired

    RestTemplate restTemplate;

    public String hiService(String name) {

    return restTemplate.getForObject(“http://SERVICE-CLIENT/hi?name=”+name,String.class);

    }

    }

    写一个controller,在controller中用调用HelloService 的方法,代码如下:

    package com.loren.serviceribbon.controller;

    自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

    深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

    因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

    既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

    由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

    如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

    最后

    本人也收藏了一份Java面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们

    目录:

    Java面试核心知识点

    一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!

    Java面试核心知识点

    已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了

    《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

    知识点

    一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!

    [外链图片转存中…(img-kHH7IEWB-1713375204361)]

    Java面试核心知识点

    已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了

    [外链图片转存中…(img-xqduduEg-1713375204361)]

    《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!