新建一個項目user-ribbon,源代碼以下java
pom.xmlweb
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zgy.cloud</groupId> <artifactId>user-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>user-ribbon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.14.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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>com.netflix.ribbon</groupId> <artifactId>ribbon-core</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-httpclient</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-loadbalancer</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-core</artifactId> <version>0.7.4</version> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-core</artifactId> <version>0.7.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
UserRibbonApplication.javaspring
package com.zgy.cloud.userribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UserRibbonApplication { public static void main(String[] args) { SpringApplication.run(UserRibbonApplication.class, args); } }
LBMain.javaapache
package com.zgy.cloud.userribbon; import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.Server; import java.util.ArrayList; import java.util.List; public class LBMain { public static void main(String[] args) { //建立一個負載均衡器的對象 ILoadBalancer lb = new BaseLoadBalancer(); //定義一個服務器列表 List<Server> servers = new ArrayList<>(); servers.add(new Server("localhost",8080)); servers.add(new Server("localhost",8081)); //將服務器列表加入負載均衡器中 lb.addServers(servers); //測試 for(int i=0; i<10; i++){ //根據指定規則來獲取服務器 Server server = lb.chooseServer(null); System.out.println("server:"+server); } } }
運行main方法,運行結果如圖json
MyRule.java 自定義負載均衡規則服務器
package com.zgy.cloud.userribbon; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.Server; import java.util.List; import java.util.Random; public class MyRule implements IRule{ private ILoadBalancer lb; @Override public Server choose(Object o) { //自定義負載均衡規則 Random r = new Random(); int i = r.nextInt(10); //獲取到所有的服務器 List<Server> servers = lb.getAllServers(); if(i > 7){ return getServerByPort(servers,8080); }else{ return getServerByPort(servers,8081); } } @Override public void setLoadBalancer(ILoadBalancer iLoadBalancer) { this.lb = iLoadBalancer; } @Override public ILoadBalancer getLoadBalancer() { return this.lb; } private Server getServerByPort(List<Server> servers, int port){ for(Server server : servers){ if(server.getPort() == port){ return server; } } return null; } }
TestMyRule.javaapp
package com.zgy.cloud.userribbon; import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.Server; import java.util.ArrayList; import java.util.List; public class TestMyRule { public static void main(String[] args) { //建立一個負載均衡器的對象 BaseLoadBalancer lb = new BaseLoadBalancer(); //建立一個MyRule對象 MyRule myRule = new MyRule(); myRule.setLoadBalancer(lb); //設置負載均衡規則 lb.setRule(myRule); //定義一個服務器列表 List<Server> servers = new ArrayList<>(); servers.add(new Server("localhost",8080)); servers.add(new Server("localhost",8081)); //將服務器列表加入負載均衡器中 lb.addServers(servers); //測試 for(int i=0; i<10; i++){ //根據指定規則來獲取服務器 Server server = lb.chooseServer(null); System.out.println("server:"+server); } } }
運行main方法,運行結果如圖負載均衡
建立一個項目ribbon-service,源代碼以下dom
pom.xmlmaven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zgy.cloud</groupId> <artifactId>ribbon-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ribbon-service</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.14.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
RibbonServiceApplication.java
package com.zgy.cloud.ribbonservice; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import java.util.Scanner; @SpringBootApplication public class RibbonServiceApplication { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String port = scanner.next(); new SpringApplicationBuilder(RibbonServiceApplication.class).properties("server.port="+port).run(args); } }
Person.java
package com.zgy.cloud.ribbonservice; public class Person { private Integer id; private String name; private String message; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
MyController.java
package com.zgy.cloud.ribbonservice; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @RestController public class MyController { @GetMapping("/person") public Person getPerson(HttpServletRequest request){ Person person = new Person(); person.setId(1); person.setName("ZGY"); person.setMessage(request.getRequestURL().toString()); return person; } }
在項目user-ribbon中添加TestRibbon.java,源代碼以下
TestRibbon.java
package com.zgy.cloud.userribbon; import com.netflix.client.ClientFactory; import com.netflix.client.http.HttpRequest; import com.netflix.client.http.HttpResponse; import com.netflix.config.ConfigurationManager; import com.netflix.niws.client.http.RestClient; import java.net.URI; public class TestRibbon { public static void main(String[] args) throws Exception{ //設置負載均衡器服務列表 ConfigurationManager.getConfigInstance().setProperty( "person-client.ribbon.listOfServers", "localhost:8080,localhost:8081"); //設置負載均衡器規則 ConfigurationManager.getConfigInstance().setProperty( "person-client.ribbon.NFLoadBalancerRuleClassName", MyRule.class.getName()); RestClient client = (RestClient) ClientFactory.getNamedClient("person-client"); HttpRequest request = HttpRequest.newBuilder().uri(new URI("/person")).build(); for (int i = 0; i < 10; i++) { HttpResponse response = client.executeWithLoadBalancer(request); String json = response.getEntity(String.class); System.out.println(json); } } }
運行mian方法,獲得如圖所示的結果
由控制檯打印的信息,咱們能夠看出咱們本身定義的負載均衡規則已經生效,而且能正常訪問服務
以上的規則,能夠百度一下
以上的配置,能夠百度一下