About a .Spring Cloud
Simply put, Spring Cloud is committed to a set of distributed systems, such as micro-services in the current framework is very hot. But it is itself a series of ordered set of frames (a plurality of modules).
Compared to Dubbo there are still many differences: for example Dubbo is based on RPC service framework, and Spring Cloud based services framework RESTful API's. Dubbo because it is a binary pass, but Spring Cloud is the http protocol transport, and so on.
Two .Spring Cloud components of Eureka
The role of 1.Eureka
Eureka and similar zookeeper, can play the role of a service registry
2. Use Eureka
@EnableEurekaServer add annotations Spring Boot to open the Eureka main class (note need to introduce related dependencies, dependency below)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
3.Eureka Configuration
In the following configuration application.properties
spring.application.name=spring-cloud-eureka server.port=8060 eureka.instance.hostname=localhost # Configure service center shall present the following property to false eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false # Access path eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
4. Testing Services
Start Spring Boot project, the access port can see the following interface
5. Service Provider
Create a new Module Boot Spring project under the project, and add annotations in the main category @EnableEurekaClient
Add the following configuration in the configuration file
spring.application.name=spring-cloud-provider server.port=8061 eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
After starting the service center project found that more than a service provider, as shown below
6. Consumer Services
Spring Cloud create a service consumer in two ways, namely, the Ribbon and use Feign
Create a service consumer 1.Ribbon way
(1) first introduced related dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
(2) add annotations in the main category @EnableDiscoveryClient
(3) arranged last in the configuration file as follows
spring.application.name=spring-cloud-consumer server.port=8063 eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
(4) customer service call methods in the service provider
RestTemplate create objects and call getForObject method of the object, for example, the following code
service layer
@Service
public class MyService {
@Autowired
private Rest Template rest template;
public String getPort(){ // call the service provider's method
return restTemplate.getForObject("http://spring-cloud-provider:getPort",String.class);
}
}
controller layer
@RestController
public class MyController {
@Autowired
private MyService myService;
@RequestMapping("/getPort")
public String getPort(){
return myService.getPort(); } }
Test results are as follows
If you need to configure load balancing policies, you can add a service consumer class configuration, and then open a port other service providers can
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public Residual Template rest template () {
return new RestTemplate();
}
}
Create a service consumer 2.Feign way
(1) first introduced related dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
(2) was then added and @EnableDiscoveryClient @EnableFeignClients annotations in the main category, and the profile similar manner Ribbon
(3) customer service call methods in the service provider
Layer service code as follows
@FeignClient(value = "spring-cloud-provider")
public interface MyService {
@RequestMapping(value = "getPort" , method = RequestMethod.GET)
public String getPort();
}
Code controller layer below
@RestController
public class MyController {
@Autowired
private MyService myService;
@RequestMapping(value = "getPort" , method = RequestMethod.GET)
public String getPort(){
return myService.getPort(); } }
Because of the way Feign itself contains With Ribbon, also realizes load balancing, interface-oriented programming, the idea to write a more clear and easy, the actual development of wider application
Three .Spring Cloud fuse
Add fuse 1.Ribbon way
(1) introducing related dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
(2) adding annotations @EnableHystrix opened fuses in the main category
(3) modification method of the service layer, the annotation adding @HystrixCommand methods, e.g.
@HystrixCommand (fallbackMethod = "getError") // If getPort method fails, a direct blow to the method and execute getError methods (avoid browser has to load) public String getPort () { // call the service provider method return RestTemplate .getForObject (. "HTTP: // the Spring-Cloud-Provider: getPort", String class ); } public String the getError () { return String.format ( "an acquisition failure" ); }
Add fuse 2.Feign way
(1) Feign own fuse, but is off by default, can be turned on in application.properties
feign.hystrix.enabled=true
(2) adding blowing method callback service layer interface (on the basis of the above code, add the following in @FeignClient callback fuse, the fuse occurs if, in the method of the interface implementation class is executed)
@FeignClient (value = "Spring-Cloud-Provider", fallback = MyServiceImpl. Class ) // if the following method fails, and to perform the method will blow in MyServiceImpl
(3) create an interface implementation class, for example,
@Component public class MyServiceImpl implements MyService { @Override public String the getPort () { return String.format ( "an acquisition failure" ); } }
3 fuse monitoring
If you need to monitor the fuse information, you can do the following steps
(1) into its dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
(2) Add in the main category comment @EnableHystrixDashboard open fuse monitoring, startup items, and access / hystrix, the following page
(3) arranged to add class
@Configuration public class MyConfig { @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/myhystrix"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
(4)在页面中输入路径即(ip+端口号+配置类中的addUrlMappings)可查看熔断信息,例如下图