一、Zuul

原文链接

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

1.配置文件application.yml加上以下的配置代码

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign

2.在入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication { public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication.class, args);
}
}

3.请求接口

http://localhost:8769/api-a/hi?name=forezp

二、ribbon

原文链接

ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。

1.在工程的配置文件指定服务的注册中心地址为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

2.项目代码

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication { public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} }

Controller层

@RestController
public class HelloControler { @Autowired
HelloService helloService; @RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
} }

Service层

@Service
public class HelloService { @Autowired
RestTemplate restTemplate; public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
} }

三、接口项目

原文链接

1.配置

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-hi
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication { public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
} }

2.接口

@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}

四、注册服务中心

原文链接

1.启动类

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication { public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}

2.配置

server:
port: 8761 eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

最新文章

  1. Android开发学习——动画
  2. PHPstorm同步文件时与ftp断开连接
  3. angular文件引入带来的绑定问题
  4. Coding Kata - 挑战你的“底线”
  5. swift学习笔记之-泛型
  6. LINUX 用户’nobody’
  7. jquery uploadify 进入页面请求两次问题解决办法。
  8. Java中的String为什么是不可变的?
  9. C# 3.0 扩展方法[转载]
  10. IO 延迟与Queue Depth
  11. bnuoj 4207 台风(模拟题)
  12. TFS2010升级至TFS2013完全指南
  13. C#复习资料
  14. Codeforces Round #326 (Div. 2) B
  15. API接口通讯参数规范
  16. SQL SELECT INTO
  17. arch/arm/Makefile:382: recipe for target 'kernel.img' failed
  18. [Vuejs] 组件 v-if 和 v-show 切换时生命周期钩子的执行
  19. SQL语句查询年龄分段分组查询
  20. php 中的信号处理

热门文章

  1. java框架面试高频问题(SpringMVC)
  2. Dao、Controller、Service三层的结构划分
  3. CMU Convex Optimization(凸优化)笔记1--凸集和凸函数
  4. CF285D.D. Permutation Sum
  5. 基于EPPlus和NPOI实现的Excel导入导出
  6. [cf1434E]A Convex Game
  7. [LCT学习时的一些笔记]
  8. 【转】NG:垂枝桦基因组图谱构建(2+3组装)及重测序分析
  9. 毕业设计之dns搭建:
  10. R 语言 select函数在org.Hs.eg.db上的运用