Spring Cloud简单项目创建
2024-09-06 02:17:55
一、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/
最新文章
- Android开发学习——动画
- PHPstorm同步文件时与ftp断开连接
- angular文件引入带来的绑定问题
- Coding Kata - 挑战你的“底线”
- swift学习笔记之-泛型
- LINUX 用户’nobody’
- jquery uploadify 进入页面请求两次问题解决办法。
- Java中的String为什么是不可变的?
- C# 3.0 扩展方法[转载]
- IO 延迟与Queue Depth
- bnuoj 4207 台风(模拟题)
- TFS2010升级至TFS2013完全指南
- C#复习资料
- Codeforces Round #326 (Div. 2) B
- API接口通讯参数规范
- SQL SELECT INTO
- arch/arm/Makefile:382: recipe for target 'kernel.img' failed
- [Vuejs] 组件 v-if 和 v-show 切换时生命周期钩子的执行
- SQL语句查询年龄分段分组查询
- php 中的信号处理
热门文章
- java框架面试高频问题(SpringMVC)
- Dao、Controller、Service三层的结构划分
- CMU Convex Optimization(凸优化)笔记1--凸集和凸函数
- CF285D.D. Permutation Sum
- 基于EPPlus和NPOI实现的Excel导入导出
- [cf1434E]A Convex Game
- [LCT学习时的一些笔记]
- 【转】NG:垂枝桦基因组图谱构建(2+3组装)及重测序分析
- 毕业设计之dns搭建:
- R 语言 select函数在org.Hs.eg.db上的运用