博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第二篇: 服务消费者(rest+ribbon)(Greenwich版本)
阅读量:4180 次
发布时间:2019-05-26

本文共 4692 字,大约阅读时间需要 15 分钟。

一、在前一章的基础上启动eureka-server和两个eureka-client实例(在application.properties更改下端口号)新增一个模块service-ribbon

 

修改父工程的pom.xml文件在<Modules>节点处增加service-ribbon

eureka-server
eureka-client
service-ribbon

service-ribbon的pom.xml的内容如下:

4.0.0
service-ribbon
0.0.1-SNAPSHOT
jar
service-ribbon
Demo project for Spring Boot
com.example
chapter2
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

配置文件application-properties内容如下:

#server 地址eureka.client.service-url.defaultZone=http://localhost:8001/eureka/#一个server 8001 两个client分别为8002 8003server.port=8004#实例名spring.application.name=service-ribbon

启动类ServiceRibbonApplication的内容如下:

package com.example;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;/** * 通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean * @author xiaobu */@EnableEurekaClient@EnableDiscoveryClient@SpringBootApplicationpublic class ServiceRibbonApplication {    public static void main(String[] args) {        SpringApplication.run(ServiceRibbonApplication.class, args);    }    /***     * @author xiaobu     * @date 2018/11/6 11:32     * @return org.springframework.web.client.RestTemplate     * @descprition restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。     * @version 1.0     */    @Bean    @LoadBalanced    RestTemplate restTemplate(){        return new RestTemplate();    }}

service-ribbon的目录结构如下

定义一个消费接口ClientService

package com.example.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/6 11:34 * @descrption */@Servicepublic class ClientService {    @Autowired    RestTemplate restTemplate;    /***     * @author xiaobu     * @date 2018/11/6 11:42     * @param name 名字     * @return java.lang.String     * @descprition  直接用的程序名替代了具体的url地址,     * 在ribbon中它会根据服务名来选择具体的服务实例,     * 根据服务实例在请求的时候会用具体的url替换掉服务名     * @version 1.0     */    public String clientService(String name){        return restTemplate.getForObject("http://eureka-client/test?name=" + name, String.class);    }}

再定义个controller

package com.example.controller;import com.example.service.ClientService;import com.sun.org.glassfish.gmbal.ParameterNames;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/6 11:36 * @description V1.0 */@RestControllerpublic class ClientController {    @Autowired    ClientService clientService;    @GetMapping("/test")    public String test(@RequestParam String name){        return clientService.clientService(name);    }}

多次访问 出现的效果如下:

 

 

 

转载地址:http://lugai.baihongyu.com/

你可能感兴趣的文章
Android4.2 Input子系统
查看>>
《C++面向对象》结构体继承
查看>>
《tiny6410裸机程序》第二章:LED跑马灯RVDS精简main.c说明
查看>>
指向指针的指针
查看>>
《tiny6410裸机程序》第三章:基础汇编test1
查看>>
《tiny6410裸机程序》第四章:汇编与C混合编程
查看>>
《tiny6410裸机程序》第五章:汇编与C混合编程-LED跑马灯最终说明、myled再次精简
查看>>
《tiny6410裸机程序》第六章:myled通过usb下载至nandflash不能运行
查看>>
《tiny6410裸机程序》第七章:S3C6410外部中断简介
查看>>
《tiny6410裸机程序》第八章:S3C6410外部中断控制寄存器
查看>>
《tiny6410裸机程序》第八章:S3C6410总中断控制寄存器
查看>>
《tiny6410裸机程序》第九章:tiny6410按键控制蜂鸣器程序
查看>>
有关free()函数的一个问题
查看>>
《Android系统学习》之bug定位
查看>>
《Linux内核编程》第七章:USB CORE与USB键鼠驱动
查看>>
《Android系统学习》之JAVA与C混合编程——JNI
查看>>
《C预处理》之#ifndef
查看>>
Android边录边播应用
查看>>
《Linux内核编程》第十三章:Linux对进程内存的二级页式管理
查看>>
ARM协处理器
查看>>