Spring Cloud Eureka 服务治理(一)
来源:互联网 发布:js分页思路 编辑:程序博客网 时间:2024/05/20 22:40
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件中的一部分, 它基于NetflixEureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。Spring Cloud 通过为Eureka增加了 Spring Boot 风格的自动化配置,我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建的微服务应用轻松地与Eureka 服务治理体系进行整合。
使用的boot版本是 1.4.2.RELEASE cloud版本是 Camden.SR3
直接上代码 :
首先需要注册中心 结合 actuator 监控
application.yml 这里使用yml配置
server: #tomcat: # uri-encoding: UTF-8 # max-threads: 1000 # min-spare-threads: 30 port: 8888spring: application: name: eureka-server management: contextPath: /helloendpoints: info: path: /appinfo health: path: /checkHealtheureka: instance: statusPageUrlPath: ${management.contextPath}/${endpoints.info.path} healthCheckUrlPath: ${management.contextPath}/${endpoints.health.path} client: #不向注册中心注册自己 registerWithEureka: false #检索服务 这里是注册中心 fetchRegistry: false serviceUrl: defaultZone: http://localhost:8888/eureka/
注册中心只需要加入注解 @EnableEurekaServer
/** * 注册中心 * @author Administrator * */@EnableEurekaServer@SpringBootApplicationpublic class ServerApplication {public static void main(String[] args) {//SpringApplication.run(DemoApplication.class, args);new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);}}
pom文件
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR3</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 监控 --> <dependency><groupId> org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency><!-- 服务中心 --></dependencies>
服务提供者
新建一个项目做为服务提供者,需要注册到注册者中去 注意端口
配置 application.properties
spring.application.name=springcloud-registereureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/启动类 提供者 @EnableDiscoveryClient
@EnableDiscoveryClient@SpringBootApplicationpublic class DemoRegisterApplication {public static void main(String[] args) {SpringApplication.run(DemoRegisterApplication.class, args);}}
@RestControllerpublic class HelloController {private final Logger logger = Logger.getLogger(getClass());@Autowiredprivate DiscoveryClient client;@RequestMapping(value = "/hello", method = RequestMethod.GET)public String index() {ServiceInstance instance = client.getLocalServiceInstance();logger.info("/hello, host:" + instance.getHost() + " service id:" + instance.getServiceId());return "Hello World";}
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies>
消费者
application.properties 需要注意的事 端口不要冲突
spring.application.name=springcloud-consumerserver.port=9000eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
/** * 消费者 * @author Administrator * */@EnableDiscoveryClient@SpringBootApplicationpublic class DemoConsumerApplication {@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(DemoConsumerApplication.class, args);}}
重点来了
@RestControllerpublic class ConsumerController {@AutowiredRestTemplate restTemplate;/** * 调用服务 * @return */@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)public String helloConsumer() {//与配置一致return restTemplate.getForEntity("http://springcloud-register/hello", String.class).getBody();}}
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency></dependencies>
先启动注册中心,查看服务 访问地址http://localhost:8888/ 会发现还没有服务注册进来
然后分别启动提供者和消费者后会发现多了两个服务
服务注册进来后,消费者就可以访问提供者的服务了
服务就完成 代码地址: 代码地址
如有疑问加群联系我 :278947305
阅读全文
0 0
- Spring Cloud 入门教程(一):服务治理(Eureka)
- Spring Cloud Eureka 服务治理(一)
- 服务治理 Spring Cloud Eureka
- spring cloud 服务自治理 Eureka(一)
- spring cloud Eureka 1 (服务治理)
- spring-cloud中eureka进行服务治理
- Spring Cloud初探——服务治理Spring Cloud Eureka
- Spring Cloud 服务治理
- Spring Cloud学习笔记1——服务治理(Eureka)
- spring cloud:eureka服务发现
- Spring Cloud Eureka--服务发现
- Spring Cloud Eureka-服务注册
- Spring Cloud系列一 之 eureka服务注册中心
- Spring Cloud 组件搭建(一)Eureka服务发现
- Spring Cloud Eureka 入门 (一)服务注册中心详解
- 【Spring Cloud】一、Eureka Server 服务注册中心服务端启动
- Spring Cloud Eureka 服务注册与发现中心(一)
- Spring Cloud Eureka 入门 (一)服务注册中心详解 「Spring Cloud Eureka 入门系列」 Spring Cloud Eureka 入门 (一)服务注册中心详解 Spr
- jQuery中的$.ajax()方法参数详解
- Git-Bug分支的处理
- C++,vs中pow和宏的使用
- Html&css刷题知识记录
- Bootstrap常用类名总结
- Spring Cloud Eureka 服务治理(一)
- 设计模式之观察者模式
- hdu 6199 gems gems gems
- poj-3070 skiing
- Capacity Scheduler配置说明
- 微信小程序全局变量
- Spring 框架
- 归并排序 --C语言实现
- mysql常用函数汇总(转载)