In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use Ribbon and Zuul to achieve gateway grayscale in Spring Cloud". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Grayscale generally refers to grayscale publishing. In micro-services, when a new service is online, it is generally chosen to assign a small number of requests to the new service, and other requests are assigned to the service. Wait until the test of the new service passes, and then distribute the service uniformly.
Eureka integrates Ribbon by default, so the grayscale implementation of Ribbon is matched by the content of the eureka.instance.metadata-map registered with the service in Eureka.
The grayscale implementation of Zuul gateway is also realized with the help of a plug-in of Ribbon, which is relatively simple.
Project environment description: there are two eureka servers (eureka-server), two identical back-end services (service-sms), and one gateway service (cloud-zuul).
1 、 Gateway dependency: 4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.9.RELEASE com.kevin cloud-zuul 0.0.1-SNAPSHOT cloud-zuul Demo project for Spring Boot 1.8 Hoxton.SR10 org.springframework.boot spring-boot-starter-web Org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-zuul 2.2.7.RELEASE io.jmnarloch ribbon-discovery-filter-spring-cloud-starter 2.1.0 Org.springframework.boot spring-boot-starter-data-redis 2.2.7.RELEASE org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test Org.junit.vintage junit-vintage-engine org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.2 com.alibaba druid 1.1.21 Mysql mysql-connector-java 5.1.48 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import Org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
What we use here is the plug-in package io.jmnarloch.ribbon-discovery-filter-spring-cloud-starter. It should be noted that after the introduction of this package, many of the ribbon features of eureka-client itself have been overwritten, and IRule rules can no longer be customized. Because an IRule of metadataAwareRule will be generated in this package, and because spring is a singleton by default, a conflicting runtime error will occur if you customize IRule again.
2 、 Configure application.yamlserver: port: 9100spring: application: name: cloud-zuul datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: root dbcp2: initial-size: 5 min-idle: 5 max-total: 10 max-wait-millis: 1000 validation-query: Select 1 test-while-idle: true test-on-borrow: false test-on-return: false eureka: client: service-url: defaultZone: http://eureka-7900:7900/eureka/mybatis: mapper-locations: classpath:mapper/*.xml
Description: my eureka server is deployed in the domain name eureka-7900 port 7900.
3. Add @ EnableZuulProxy@SpringBootApplication@EnableZuulProxypublic class CloudZuulApplication {public static void main (String [] args) {SpringApplication.run (CloudZuulApplication.class, args);}} 4 to the springboot startup class. Add grayscale filter import com.kevin.dao.CommonGrayRuleDao;import com.kevin.entity.CommonGrayRule;import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import io.jmnarloch.spring.cloud.ribbon.support.RibbonFilterContextHolder. Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest; / / Management @ Componentpublic class GrayFilter extends ZuulFilter {@ Override public String filterType () {return FilterConstants.ROUTE_TYPE;} @ Override public int filterOrder () {return 0 for joining spring } @ Override public boolean shouldFilter () {/ / must return true, otherwise the filter will not work return true;} @ SuppressWarnings ("SpringJavaInjectionPointsAutowiringInspection") @ Autowired private CommonGrayRuleDao commonGrayRuleDao @ Override public Object run () throws ZuulException {/ / simulates the configuration of grayscale rules from the database. According to the data obtained by id, in the actual project, CommonGrayRule commonGrayRule = commonGrayRuleDao.selectByPrimaryKey (1); RequestContext currentContext = RequestContext.getCurrentContext (); HttpServletRequest request = currentContext.getRequest (); int userId = Integer.parseInt (request.getHeader ("userId")) / / take the userid from the head and match it according to the rules in the database. If you have this user in the grayscale rule, you will go to the grayscale v1 version. Here, the simulation is based on / / the value obtained by a specific id, which is actually determined according to the situation. In practice, the grayscale rule is not necessarily based on the user to if (commonGrayRule.getId () = = userId) {/ / core code to achieve grayscale, where the version corresponds to the key and value in the metadata-map of the service to be accessed by RibbonFilterContextHolder.getCurrentContext (). Add ("version", "v1") } return null;}}
At this point, it is OK to access the service-sms service through the gateway. If you add version to head, you can specify it to a specific service.
Note in advance: the application.yaml files of the two service-sms services are as follows:
Spring: profiles: sms1 application: name: service-smsserver: port: 8001eureka: client: service-url: defaultZone: http://eureka-7900:7900/eureka/ instance: metadata-map: version: V1-- spring: profiles: sms2 application: name: service-smsserver: port: 8002eureka: client: service-url: defaultZone: http://eureka-7900:7900/eureka/ instance: metadata-map: version: v2
Because of native testing, profiles is used to distinguish.
You can see that one metadata-map is set to version: v1, and the other is version: v2.
When we add a data with a user id of 1 in our database, when we access the service through the gateway, the content with userId of 1 is added to the head, then the request with the user of 1 will always go through the sms1 service, otherwise the switch will be polled in sms1 and sms2.
This is the end of the introduction of "how to use Ribbon and Zuul to achieve gateway grayscale in Spring Cloud". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.