Spring Cloud Eureka: “In-depth understanding of Eureka”
Spring Cloud Ribbon: “In-depth understanding of Ribbon”
Spring Cloud Hystrix: “In-depth understanding of Hystrix”
Spring Cloud Zuul: “In-depth understanding of Zuul”
Spring Cloud Config: “In-depth understanding of Config”
Figure 1 Flowchart of Spring Cloud Netflix
1. Introduce Spring Cloud Netflix parent dependency
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> </dependencyManagement>
2. Build Eureka cluster
1 introduce Eureka-Server dependency
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
2 Write cluster Eureka-Server configuration file
2.1 The first Eureka-Server configuration file
server: port: 8000 spring: application: name: Eureka-Server-8000 eureka: instance: hostname: localhost client: service-url: defaultZone: http://localhost:8001/eureka/ # Registration address
2.2 The second Eureka-Server configuration file
server: port: 8001 spring: application: name: Eureka-Server-8001 eureka: instance: hostname: localhost client: service-url: defaultZone: http://localhost:8000/eureka/ # Registration dependencies
3 Write cluster Eureka-Server startup class
3.1 The first Eureka-Server startup class
@SpringBootApplication @EnableEurekaServer public class EurekaServer8001 { public static void main(String[] args) { SpringApplication.run(EurekaServer8001.class, args); } }
3.2 The second Eureka-Server startup class
@SpringBootApplication @EnableEurekaServer public class EurekaServer8000 { public static void main(String[] args) { SpringApplication.run(EurekaServer8000.class, args); } }
4 registration results
Figure 2 Eureka-Server-8000 service registration result map
Figure 3 The registration result map of Eureka-Server-8001
3. Create a service call public module (Common)
Import interface template TestService
public interface TestService { String testFeign(); }
4. Build a service provider cluster
1 Import dependencies
<dependencies> <!--Eureka server dependency--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>com.lidantao</groupId> <artifactId>Netflix-Common</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
2 Write Provider cluster configuration file
2.1 The first Provider configuration file
server: port: 7000 spring: application: name: Provider eureka: client: service-url: defaultZone: http://localhost:8000/eureka/
2.2 The second Provider configuration file
server: port: 7001 spring: application: name: Provider eureka: client: service-url: defaultZone: http://localhost:8000/eureka/
3 Implement remote service call interface
3.1 Provider-7000 implementation code
@Service public class TestServiceImpl implements TestService { @Override public String testFeign() { return "Provider-7000-testFeign"; } }
3.2 Provider-7001 implementation code
@Component public class TestServiceImpl implements TestService { @Override public String testFeign() { return "Provider-7001-testFeign"; } }
4 Write service Controller interface
4.1 Controller class of the first Provider
@RestController public class TestController { @Autowired private TestService testService; @RequestMapping("/testFeign") public String testFeign(){ return testService. testFeign(); } }
4.2 The Controller class of the second Provider
@RestController public class TestController { @Autowired private TestService testService; @RequestMapping("/testFeign") public String testFeign(){ return testService. testFeign(); } }
5. Build a service consumer module
1 Controller
@RestController @RequestMapping("/consumer") public class TestController { @Autowired private TestService testService; @RequestMapping("/testFeign") public String getTest(){ return testService. testFeign(); } }
2 Service
@Component @FeignClient("Provider") public interface TestService{ @GetMapping("/testFeign") @LoadBalanced String testFeign(); }
3 Realize client load balancing
@Configuration public class CustomRibbonConfig { @Bean public IRule iRule(){ return new RoundRobinRule(); } }
4 startup class
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @RibbonClient(name = "service", configuration = CustomRibbonConfig. class) public class NetflixConsumer6001 { public static void main(String[] args) { SpringApplication.run(NetflixConsumer6001.class, args); } }
5 client load balancing results
Figure 4 Provider-7000 provides results
Figure 5 Provider-7001 provides results
6. Build the gateway module
1 Write configuration file
server: port: 9000 spring: application: name: Netflix-Zuul-9000 eureka: client: service-url: defaultZone: http://localhost:8000/eureka/ zuul: routes: mydept.service-id: Consumer-6001 # Forward to this service mydept.path: /**
2 Write startup class
@SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class NetflixZuul9000 { public static void main(String[] args) { SpringApplication.run(NetflixZuul9000.class, args); } }
3 Forward the result to the client through the gateway
Figure 6 Accessing consumers through the gateway
7. All code addresses
Code Address Warehouse
or
wx public account: cola big data