springcloud(第七篇)springcloud ribbon with eureka
2016-05-21 12:24
651 查看
spring cloud ribbon with eureka
introduction
在上一篇中阐述了ribbon的基本用法,但是可以发现服务列表是通过配置得来的,实际
情况通常是由负载均衡+服务发现来实现的,通过服务发现获取服务列表,负载均衡通过rule选择要调用的服务。服务发现可以通过
eureka来实现,后期会讲解利用
consul做服务发现。
eureka discovery service
eureka服务发现在前面的文章中已经提供到,这里直接给出代码首先是 eureka server
package com.lkl.springcloud.ribbon; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EurekaServerApplication.class).properties( "spring.config.name:eureka", "logging.level.com.netflix.discovery:OFF") .run(args); } }
对应配置文件:
server.port=8761 spring.application.name= eureka eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false
在eureka上进行服务注册
SimpleApplication.java
package com.lkl.springcloud.ribbon; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Configuration @EnableAutoConfiguration @EnableDiscoveryClient @RestController public class SimpleApplication { @RequestMapping("/") public String hello() { return "Hello"; } public static void main(String[] args) { new SpringApplicationBuilder(SimpleApplication.class).properties( "spring.config.name:simple").run(args); } }
对应client配置
server.port= 8081 spring.application.name= simple eureka.instance.leaseRenewalIntervalInSeconds= 10 eureka.client.registryFetchIntervalSeconds= 5 eureka.client.serviceUrl.defaultZone= http://localhost:${eureka.port:8761}/eureka/
ribbon + eureka
Application.java
package com.lkl.springcloud.ribbon; import org.springframework.beans.factory.annotation.Autowired; 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.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by liaokailin on 16/5/9. */ @SpringBootApplication @EnableDiscoveryClient @RestController public class Application { @Autowired RestTemplate client; /** * LoadBalanced 注解表明restTemplate使用LoadBalancerClient执行请求 * * @return */ @Bean @LoadBalanced public RestTemplate restTemplate() { RestTemplate template = new RestTemplate(); SimpleClientHttpRequestFactory factory = (SimpleClientHttpRequestFactory) template.getRequestFactory(); factory.setConnectTimeout(3000); factory.setReadTimeout(3000); return template; } @RequestMapping("/") public String helloWorld() { return client.getForObject("http://simple/", String.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
EnableDiscoveryClient表明为eureka client 通过可以进行服务发现
RestTemplate构建
RestTemplate对应的bean,在method上使用注解
LoadBalanced表示restTemplate使用
LoadBalancerClient执行请求
对应配置文件
spring.application.name=RibbionClient eureka.instance.leaseRenewalIntervalInSeconds= 10 eureka.client.registryFetchIntervalSeconds= 5 eureka.client.serviceUrl.defaultZone= http://localhost:${eureka.port:8761}/eureka/[/code]
runApplication.java后访问http://localhost:8080/调用helloWorld()方法,该方法访问http://simple/其中的simple为SimpleApplication.java
在eureka上注册的service id,即实际调用@RequestMapping("/") public String hello() { return "Hello"; }
因此得到返回结果Hello.
ok ~ it’s work ! more about is here
相关文章推荐
- 关于struts2中Struts2.xml的配置小问题
- springboot数据库连接池使用策略
- 在Eclipse中在线安装插件SVN和Android studio中配置SVN的一些坑
- java验证码Captcha
- JAVA——Date类、Calendar类
- 基于注解的struts action跳转 @Result 跳转到另一个action
- Struts框架中,namespace什么意思?
- Eclipse中java项目引用dll库的路径设置
- JAVA-反射-getGenericSuperclass()
- Maven那点事儿(Eclipse版)
- 【codeforces】- Petya and Java(给不同的数,判断大小,选择不同数据类型)
- JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
- Java多线程(九)之ReentrantLock与Condition
- Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
- java 大数操作(加减乘除及取余)
- jni调用java数组导致VM aborting,安卓程序莫名闪退
- 关于:[Struts2]Caused by: com.opensymphony.xwork2.inject.DependencyException:
- spring-mybatis整合测试
- java遍历给定目录,树形结构输出所有文件,包括子目录中的文件
- Java静态的应用