SpringCloud
SpringCloud provides developers with the tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, load balancing , micro broker, event bus, global lock, decision-making campaign, distributed session and so on. It is simple operating environment, it can run on a computer developers. Otherwise spring cloud-based Springboot, so there is need to develop some understanding of Springboot, if not understand, you can see the ants SpringBoot classroom curriculum.
Service providers and consumer relations
Service Provider: service being invoked
Consumers: Service was calling
Registration and service discovery ( Eureka )
Here, the components we need to use the Spring Cloud Netflix's Eureka, eureka is a service registration and discovery module.
What is Eureka
The official presentation here Eureka Wiki . Eureka open source Netflix is a RESTful service, mainly used for registration service discovery. Eureka consists of two components: Eureka Eureka server and the client. Eureka server as a service registration server. Eureka is a client java client, to simplify the interaction with the server load balancer as polling, to provide failover for the service and support. Netflix used in its production environment is another client that weighted based traffic load balancing, resource utilization and error status.
In my opinion, Eureka appeal from the following:
Open source : you can find out the achievement, or even modify the source code.
Reliable : After Netflix years of production test environment, should use more reliable peace of mind
Fully functional : not only provides a complete registration discovery service, as well Ribbon can be used in conjunction with other services.
Based on Java : For Java programmers to use, relatively hearts bottom.
spring cloud can use Spring Cloud, it was well integrated with Eureka, very convenient to use.
Producer Consumer Case
First, create a project springcloud_eureka_service
1 Add to depend pom file
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2 file written application.yml
## Application name the Spring: the Application: name: eurakaserver # statement currently eurakaserver port number Server: Port: 8888 # configure Eureka Eureka: Client: # behalf if they are registered with the registry to the Register the -with-Eureka: false # show their is a registered center FETCH -registry: false # configure address Service - url: defaultzone: HTTP: // localhost: 8888 / Eureka
3 Create Launcher class
@SpringBootApplication @EnableEurekaServer public class StartEurekaService { public static void main(String[] args) { SpringApplication.run(StartEurekaService.class,args); } }
4 operating results
Second, create a project springcloud_eureka_provider producer
Introduction to the dependence file pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2 Create service interfaces
public interface IDoSomeService { public String doSome(); }
3 Create a service interface implementation class
@RestController public class IDoSomeServiceImpl implements IDoSomeService { @RequestMapping("/doSome") @Override public String doSome() { System.out.println("服务提供者"); return "eureka"; } }
4 file written application.yml
## Application name the Spring: the Application: name: euraka - Provider # statement currently eurakaserver port number Server: Port: 8885 # configure Eureka Eureka: Client: # Configure Address Service - url: defaultzone: HTTP: // localhost: 8888 / Eureka
5 Create a launcher class
@SpringBootApplication @EnableEurekaClient public class StartProvider { public static void main(String[] args) { SpringApplication.run(StartProvider.class,args); } }
Run 6 results
Third, create a consumer project springcloud_euraka_consumer
Pom file node to the introduction
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2 创建service接口
@FeignClient("euraka-provider") public interface IDoSomeService { @RequestMapping("/doSome") public String doSome(); }
3 编写application.yml文件
##应用名称 spring: application: name: euraka-comsumer #声明当前 eurakaserver端口号 server: port: 8881 #配置eureka eureka: client: #配置地址 service-url: defaultZone: http://localhost:8888/eureka
4 创建Controller层
@RestController public class IDoSomeController { /* @Resource private RestTemplate restTemplate;*/ @Resource private IDoSomeService iDoSomeService; @RequestMapping("/doSome") public String doSme(){ System.out.println("ConsumerController"); /*return restTemplate.getForObject("http://euraka-provider/doSome",String.class);*/ return iDoSomeService.doSome(); } }
5 编写程序启动类
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class StartProvider { public static void main(String[] args) { SpringApplication.run(StartProvider.class,args); } /* @Bean @LoadBalanced //实现负载均衡 RestTemplate restTemplate(){ return new RestTemplate(); }*/ //指定负载均衡策略随即 @Bean public IRule ribbonRule(){ return new RandomRule(); } }
6 运行效果