Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ribbon of SpringCloud

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

[previous words] following the above, some of the knowledge of this article depends on my previous article: Eureka of SpringCloud. If you haven't read it, you can take a look at it first. In addition, in the micro-service architecture, the business is divided into independent services, and the communication between services and services is based on http restful. Spring cloud has two ways to invoke services, one is ribbon+restTemplate, the other is feign. In this article, we will first explain how to use ribbon+rest.

A brief introduction to Ribbon

Ribbon is a load balancing client, which can well control some behaviors of htt and tcp. Feign integrates ribbon by default.

Ribbon has implemented these configuration bean by default:

IClientConfig ribbonClientConfig: DefaultClientConfigImpl

IRule ribbonRule: ZoneAvoidanceRule

IPing ribbonPing: NoOpPing

ServerList ribbonServerList: ConfigurationBasedServerList

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

# II. Preparatory work-create a new ribbon sub-project * * lovin-ribbon-client**, for later operations. Here are the main pom dependencies on ~ ~ pom lovincloud com.eelve.lovincloud 1.0-SNAPSHOT 4.0.0 lovin-ribbon-client jar ribbonclient 0.0.1 ribbon client org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-security Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.boot spring-boot-maven-plugin, it's for safety here. I will add spring-boot-starter-securityserver:port: 8805 # service port number spring:application:name: lovinribbonclient # service name security:basic: enabled: trueuser: name: lovin password: ${REGISTRY_SERVER_PASSWORD:lovin} eureka:client:serviceUrl: defaultZone: http://lovin:lovin@localhost:8881/eureka/ # eureka service address configuration spring-boot-starter-security registered with Here for my convenience, let go of all requests package com.eelve.lovin.cofig.

Import org.springframework.context.annotation.Configuration

Import org.springframework.core.annotation.Order

Import org.springframework.security.config.annotation.web.builders.HttpSecurity

Import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter

/ * *

@ ClassName SecurityConfig@Description TDO@Author zhao.zhilue@Date 14:12 on 2019-8-16

@ Version 1.0 percent /

@ Configurationbr/ > * /

@ Configuration

Public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@ Override

Protected void configure (HttpSecurity http) throws Exception {

Http.authorizeRequests (). AnyRequest (). PermitAll ()

.and () .csrf () .disable ()

}

}

-then inject a bean: restTemplate; into the program's ioc container and use the @ LoadBalanced annotation to indicate that the restRemplate enables load balancing. ~ javapackage com.eelve.lovin

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.web.client.RestTemplate

/ * *

@ ClassName LovinRibbonClientApplication@Description TDO@Author zhao.zhilue@Date 16:59 on 2019-8-15

@ Version 1.0 percent /

@ SpringBootApplicationbr/ > * /

@ SpringBootApplication

Public class LovinRibbonClientApplication {

Public static void main (String [] args) {

SpringApplication.run (LovinRibbonClientApplication.class,args)

}

@ Bean@LoadBalancedbr/ > @ LoadBalanced

Return new RestTemplate ()

}

}

-then write a * * HelloService**~~~javapackage com.eelve.lovin.service

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.stereotype.Service

Import org.springframework.web.client.RestTemplate

/ * *

@ ClassName HelloService@Description TDO@Author zhao.zhilue@Date 17:02 on 2019-8-15

@ Version 1.0 percent /

@ Servicebr/ > * /

@ Servicebr/ > @ Autowired

Public String getHello () {

/ / the lovineurkaclient here is the name of the new eureka client I created in my last article

Return restTemplate.getForObject ("http://lovineurkaclient/hello",String.class);"

}

}

-write another * * HelloController**~~~javapackage com.eelve.lovin.controller

Import com.eelve.lovin.service.HelloService

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.web.bind.annotation.RequestMapping

Import org.springframework.web.bind.annotation.RestController

/ * *

@ ClassName HelloController@Description TDO@Author zhao.zhilue@Date 17:05 on 2019-8-15

@ Version 1.0 percent /

@ RestControllerbr/ > * /

@ RestController

@ Autowired

HelloService helloService

@ RequestMapping ("hello")

Public String hello () {

Return helloService.getHello ()

}

}

# 3. Start the test-start the server and two clients of eureka, and then start the new lovin-ribbon-client! [we can see that the services have all started successfully] (https://i.loli.net/2019/08/23/pa1X8eByNhltP4C.png) We can see that the services have all started successfully-and then visit http://localhost:8805/hello! [we can see that the eureka client we established can be transferred to the eureka client we established through ribbon] (https://i.loli.net/2019/08/23/TXoNiuvYhGO9Hc3.png) we can see that the eureka client we established can be transferred to the eureka client we established through ribbon-request the API to observe and return! [we can see that we have transferred to another interface through the ribbon load] (https://i.loli.net/2019/08/23/o9Cm2LgBXb4qjF5.png) we can see that we have transferred to another interface through the ribbon load, and here we have a simple ribbon load. We can see that the services we call are no longer directly accessed to the corresponding services as in the previous article, but are called through Ribbon's load balancer, and it is explained here that the default mechanism of Ribbon is polling.

The last part is the source code of this blog. Welcome to follow this set of SpringCloud practice.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report