技术控

    今日:94| 主题:49531
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Using the Netflix stack with Spring boot: Ribbon

[复制链接]
哆啦不能没有A梦 发表于 2016-10-19 08:21:41
362 12

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Inmy last article I gave a basic introduction into the concepts of a service oriented architecture (SOA) and microservices. I also made a small example about a service registry and how Netflix is involved into microservices with tools like Eureka . However, the story was quite unfinished, since we only made it possible for microservices to register themselves on the registry, but the ones consuming the microservices have nothing to do with the service registry… yet.
  Meet Ribbon

   Ribbon is an interceptor that goes well with Eureka. It allows you to look up the hostname/IP of a service and does stuff like client-side load balancing for you as well. Now the only question is, what does Spring have to do with that? Well, if you followed my article about consuming a REST service , you should be familiar with  RestTemplate  already. When using the Eureka Discovery dependency, you can use the @LoadBalanced annotation to set up a RestTemplate using the Ribbon interceptor.
   
Using the Netflix stack with Spring boot: Ribbon-1 (themselves,concepts,familiar,involved,possible)

  Setting up our application

   In case you didn’t follow my article about Eureka, I suggest you download the code from GitHub . If you have both projects (the service registry and the service itself) imported in your IDE it’s time to create our next project, the consumer.
   Open the Spring Initializr , and add both the Web , Eureka Discovery , Thymeleaf and Actuator dependencies.

Using the Netflix stack with Spring boot: Ribbon-2 (themselves,concepts,familiar,involved,possible)

  Configuring Eureka

   The first step we’re going to configure is how to set up the Eureka discovery. First of all open application.properties (or application.yml ) and add the following properties:
  [code]# application.properties
eureka.client.healthcheck.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.statusPageUrlPath=/info
eureka.instance.healthCheckUrlPath=/health[/code]  [code]# application.yml
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    status-page-url-path: /info
    health-check-url-path: /health[/code]  This should look quite familiar, because this is the same configuration we applied to set up our service itself. The health check and status page URLs are provided by the actuator, so we don’t have to do anything for that (except adding the actuator as a dependency).
  We also have to give the application a name, since it will be used to register itself on Eureka with that name. Yes that’s right, the service registry is not only limited to services, but applications can register themself on Eureka as well.
   To give it a name we use the spring.application.name property:
  [code]# application.properties
spring.application.name=task-app[/code]  Or in YAML:
  [code]# application.yml
spring:
  application:
    name: task-app[/code]   And finally, we also have to enable the Eureka discovery by adding the @EnableEurekaClient annotation to our main class like this:
  [code]@SpringBootApplication
@EnableEurekaClient
public class SpringBootEurekaConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootEurekaConsumerApplication.class, args);
    }
}[/code]  Adding the DTO

  Now, because we will consume the task service we wrote earlier, we’ll have to use the same “format” as the API, so we’ll use the same DTO. To make it easier for myself I’ll just copy the code from the microservice itself.
  [code]public class TaskDTO {
    private Long id;
    private String task;
    private boolean completed;

    public TaskDTO() {
    }

    public TaskDTO(String task, boolean completed) {
        this(null, task, completed);
    }

    public TaskDTO(Long id, String task, boolean completed) {
        this.id = id;
        this.task = task;
        this.completed = completed;
    }

    public Long getId() {
        return id;
    }

    public String getTask() {
        return task;
    }
    public void setTask(String task) {
        this.task = task;
    }

    public boolean isCompleted() {
        return completed;
    }
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }
}[/code]  Creating the HTML

   Out of laziness I will recycle the HTML from an article I made earlier. Create a file called tasks.html inside the src/main/resources/templates folder with the following content:
  [code]


  
  
  
  Task application


  

   

My tasks


   

      
      
   

  


[/code]   I’m using checkboxes here but disabled them because I didn’t implement the API calls yet to update tasks. Anyhow, I’m using Thymeleaf here as a template engine. However, the default settings are a bit strict, so open application.properties (or application.yml ) and set the following properties:
  [code]# application.properties
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5[/code]  [code]# application.yml
spring:
  thymeleaf:
    cache: false
    mode: LEGACYHTML5[/code]   The LEGACYHTML5 mode allows you to write things like
without properly closing the elements. So if you didn’t use this mode, you would have to write
. The same applies to attributes. In the HTML above I’ve disabled all checkboxes using the disabled property. Without LEGACYHTML5 you would have to write disabled="disabled" .
   However, choosing this mode requires you to add an additional dependency to your Maven descriptor called nekohtml :
  [code]
    net.sourceforge.nekohtml
    nekohtml
    1.9.22
    runtime
[/code]  Calling the REST API

   Before we start writing our service, we have to set up a RestTemplate . If you used Spring cloud before, you might know that a RestTemplate was automatically created for you, however, since the Brixton release of Spring cloud, this is no longer true. When using Eureka Discovery, you’ll still have to create a RestTemplate .
  So let’s do that by opening the main class and creating a bean:
  [code]# application.yml
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    status-page-url-path: /info
    health-check-url-path: /health0[/code]   Now the next step is to create our service and autowire the RestTemplate :
  [code]# application.yml
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    status-page-url-path: /info
    health-check-url-path: /health1[/code]   Now, to use RestTemplate with generic types like List , you’ll have to use ParameterizedTypeReference . For our example it doesn’t really matter whether or not you use an array or a list, but for demonstration I’ll use ParameterizedTypeReference . To use it, create an anonymous inner class and extend from this class like this:
  [code]# application.yml
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    status-page-url-path: /info
    health-check-url-path: /health2[/code]   Now you can use it with RestTemplate like this:
  [code]# application.yml
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    status-page-url-path: /info
    health-check-url-path: /health3[/code]   As you can see, I’m not using a real URL reference here, but I’m using the task-service name to refer to the service that is registered on Eureka with this name. This makes it very easy since nobody really needs to know where the microservice is located except the service registry.
  Using the service

  Now, the final step is to create a new controller and use the service within it:
  [code]# application.yml
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    status-page-url-path: /info
    health-check-url-path: /health4[/code]   Now you can run the application, but make sure you run the registry and the service as well. Also make sure they all have a different port so they can be ran simultaneous. You can change the port by overriding the server.port property, or by setting the SERVER_PORT environment variable.
  Testing it out

   If you open your application (I hosted it on port 8082, so for me it’s http://localhost:8082 ), you’ll see our task service is successfully being called:
123下一页
友荐云推荐




上一篇:Android CardView的使用细节
下一篇:【译】前端是 ???? 而 JavaScript 是 ????
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

何鑫 发表于 2016-10-24 11:37:20
我就算是一只癞蛤蟆,我也决不娶母癞蛤蟆.
回复 支持 反对

使用道具 举报

神经范 发表于 2016-10-25 19:50:20
不要为那些不愿在你身上花费时间的人而浪费你的时间.
回复 支持 反对

使用道具 举报

春枫 发表于 2016-10-26 08:46:10
大声告诉我这是几楼?
回复 支持 反对

使用道具 举报

1065639161 发表于 2016-10-26 09:28:18
站位支持
回复 支持 反对

使用道具 举报

抢红包 发表于 2016-10-29 05:36:24
有空一起交流一下
回复 支持 反对

使用道具 举报

呜呜 发表于 2016-11-1 16:43:32
呜呜和小伙伴们都惊呆了
回复 支持 反对

使用道具 举报

liluo1991 发表于 2016-11-2 12:16:33
楼上的很有激情啊!
回复 支持 反对

使用道具 举报

帅美俊 发表于 2016-11-3 12:04:12
为潜心回复,本人决定暂时不和异性朋友接触,谢谢合作.
回复 支持 反对

使用道具 举报

兰晓娟 发表于 2016-11-16 01:09:10
撸过 可以么
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表