把上一个文章的项目重新复制一份 修改下yml中端口(文章地址: https://blog.csdn.net/yuzhiqiang_1/article/details/84580665)
一: application.yml
server:
port: 12347 # 你的端口
eureka:
client:
serviceUrl:
defaultZone: http://localhost:12345/eureka/ # 服务中心地址
spring:
application:
name: clientName # 客户端的名字
二:创建一个ribbon项目(不会创建项目的可以看第一篇Cloud文章https://blog.csdn.net/yuzhiqiang_1/article/details/84578506)
新增application.yml文件,端口要与之前的项目区分开
eureka:
client:
serviceUrl:
defaultZone: http://localhost:12345/eureka/
server:
port: 12348
spring:
application:
name: ribbon
修改RibbonApplication
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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient // 定义为客户端
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
/**
* @LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
新增个service类(HelloService) 调用复制出的项目与被复制的项目 我这里叫discSystem-4、discSystem-4-1
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* 问题一: 什么是REST REST(RepresentationalState Transfer)是Roy Fielding
* 提出的一个描述互联系统架构风格的名词。REST定义了一组体系架构原则,您可以根据这些原则设计以系统资源为中心的Web
* 服务,包括使用不同语言编写的客户端如何通过 HTTP处理和传输资源状态。
*
* 为什么称为 REST?Web本质上由各种各样的资源组成,资源由URI
* 唯一标识。浏览器(或者任何其它类似于浏览器的应用程序)将展示出该资源的一种表现方式,或者一种表现状态。
* 如果用户在该页面中定向到指向其它资源的链接,则将访问该资源,并表现出它的状态。
* 这意味着客户端应用程序随着每个资源表现状态的不同而发生状态转移,也即所谓REST。
*
* 问题二: RestTemplate Spring'scentral class for synchronous client-side HTTP
* access.It simplifies communication with HTTPservers, and enforces RESTful
* principles. Ithandles HTTP connections, leaving application code to provide
* URLs(with possible template variables) andextract results.
* 简单说就是:简化了发起HTTP请求以及处理响应的过程,并且支持REST
*
* @author 于志强
*
* 2018年11月28日 上午11:10:45
*/
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate; //
public String hiService(String name) {
return restTemplate.getForObject("http://CLIENTNAME/test", String.class);
}
}
新增Controller类(HelloController) 调用server
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/demo") // 随便起个自己喜欢的访问名字
public String hi(@RequestParam String name) {
return helloService.hiService(name);
}
}
注:
启动
第一篇的discSystem-3项目(server)
第二篇的discSystem-4项目(Client)
本篇被复制出来的discSystem-4-1项目(Client)
本篇新创建的ribbon项目(Client + Ribbon)
访问(http://localhost:12348/demo) 会自动分配请求的服务