Eureka注册中心:《使用IntelliJ IDEA创建Spring Cloud服务注册中心》
服务提供者创建:《使用IntelliJ IDEA创建Spring Cloud的Eureka Client》
本文将创建服务集群(即服务提供者的集群),将集群中的服务均注册到Eureka注册中心。然后创建Ribbon项目,使用ribbon+restTemplate这种服务方式来调用Eureka注册中心的服务提供者,并实现负载均衡,具体采用的负载均衡方式为轮询调用服务提供者集群中的服务。
创建服务提供者集群
参考文章《使用IntelliJ IDEA创建Spring Cloud的Eureka Client》,在参考文章中已经创建了acyxstock服务,创建和配置acyxstocktwo(步骤与acyxstock一样。区别只在于项目名称变为acyxstocktwo,application.yml中端口号变为8766。
打开右侧的maven工具栏,双击clean,清理上一次构建生成的文件。
然后,双击install,执行编译、打包和把包安装到maven本地仓库的操作,可以被其他工程作为依赖来使用。
创建完成后,依次启动Eureka注册中心、acyxstock、acyxstocktwo,然后在浏览器中访问:http://127.0.0.1:8761
从上图可以看出,acyx-stock的服务提供者集群已经完成在注册中心的注册。
修改地址栏内容为:http://localhost:8765/home?name=Spring Cloud ,如下图8765端口上的acyxstock服务正常。
修改地址栏内容为:http://localhost:8766/home?name=Spring Cloud ,如下图8766端口上的acyxstock服务正常。
此时,acyxstock的服务集群已经创建完毕,并测试其服务正常。
创建Ribbon项目
File---new---module---Spring Assistant
单击next,进入如下图页面
点击next,在如下图页面中勾选Spring Cloud Discovery、Eureka Server
点击next
点击finish,完成项目创建。
配置application.yml
在上图中的resources目录下新建application.yml,application.yml的功能和application.properties是一样的,但yml文件是树状结构,有更好的层次感,更易于理解。然后,删除原有的application.properties。如下图
修改其内容如下,其中name为注册到注册中心的服务名,可以自定义。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8900
spring:
application:
name: acyx-ribbon
执行Maven的clean、install操作
打开右侧的maven工具栏,双击clean,清理上一次构建生成的文件。
然后,双击install,执行编译、打包和把包安装到maven本地仓库的操作,可以被其他工程作为依赖来使用。
添加@EnableDiscoveryClient 注解,创建RestTemplate ,并添加@LoadBalanced开启负载均衡。
点击上图中箭头指向的椭圆标注内容,会自动下载相关的依赖包并在pom.xml中添加对应的dependency。然后添加对应的import如下图。
AcyxribbonApplication.java
package com.acyx.ribbon;
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 AcyxribbonApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxribbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
创建HomeRibbonService.java
package com.acyx.ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HomeRibbonService {
@Autowired
private RestTemplate restTemplate;
public String homeRibbon(String name) {
return restTemplate.getForObject("http://ACYX-STOCK/home?name=" + name, String.class);
}
}
创建HomeRibbonController.java
package com.acyx.ribbon.controller;
import com.acyx.ribbon.service.HomeRibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeRibbonController {
@Autowired
private HomeRibbonService homeRibbonService;
@RequestMapping("/homeRibbon")
public String homeRibbon(String name){
return homeRibbonService.homeRibbon(name);
}
}
依次启动Eureka注册中心、acyxstock、acyxstocktwo、acyxribbon,然后在浏览器中访问:http://127.0.0.1:8761
如上图所示,ACYX-RIBBON已在注册中心成功注册。
此时,在浏览器中访问:http://127.0.0.1:8900/homeRibbon?name=Ribbon
保持浏览器地址栏内容不变,刷新浏览器后,如下图
再次刷新
欢迎关注个人微信公众号“我爱编程持之以恒”