Introduction : previous project mostly single project, at compile time, these items will be packaged into one JAR package, and eventually merge together to form a WAR package. Next, we need to upload the WAR package to the Web container, extract the WAR package, and restart the server. Then compile and deploy the
project will increase after the following problems
① difficult to compile, deploy, difficult, difficult test
② technology selection difficult
③ Expansion difficult
A, springCloud is valid
Spring cloud-based service governance is a tool to achieve Spring Boot package for micro-service architecture, management and coordination of services.
Spring Cloud is an ordered collection of a series of frames. It uses facilitate the development of Spring Boot cleverly simplify the development of distributed systems infrastructure, such as service discovery registration, distribution center, message bus, load balancing, circuit breakers, monitoring and other data, can be done with a style of development of Spring Boot a key to start and deploy
Five animal:
Service Registration discovery --Netflix Eureka: registration of all micro-communication service address
customer service side load balancing --Netflix Ribbon \ Feign: calling the issue between the service
breaker --Netflix Hystrix: failure to solve the problem of micro-services, micro-service isolation
Services gateway --Netflix Zuul: unified entrance of micro-services
distributed configuration --Spring Cloud Config: micro-manage unified service configuration file
Micro-services scenario simulation - Getting Started
1) creating a normal maven project, as the parent module
2) the leader packet
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<springboot.version>2.0.5.RELEASE</springboot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3) Create a service provider module
<dependencies>
<!--公共代码依赖-->
<dependency>
<groupId>cn.itsource.springcloud</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<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>
</dependency>
</dependencies>
application.yml
server:
port: 8001
spring:
application:
name: USER-PROVIDER #不要使用下划线
4) an inlet class
@SpringBootApplication
public class UserProviderApplication_8001 {
public static void main(String[] args) {
SpringApplication.run(UserProviderApplication_8001.class);
}
}
5) service code controller
@RestController
@RequestMapping("/provider")
public class UserController {
// @Autowired
// private IUserService userService;
@GetMapping("/user/{id}") //user/1
public User getUser(@PathVariable("id") Long id) {
// 正常应该调用service获取用户,现在模拟一下
return new User(id, "zs");
}
}
6) the service consumer
guide package
<dependency>
<groupId>cn.itsource.springcloud</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<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>
</dependency>
application.yml
server:
port: 9001
spring:
application:
name: USER-CONSUMER
Entrance class
package cn.itsource.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserConsumerAppliction_9001 {
public static void main(String[] args) {
SpringApplication.run(UserConsumerAppliction_9001.class);
}
}
Call Interface
@Configuration // <beans></beans>
public class CfgBean {
@Bean //<bean class="org.springframework.web.client.RestTemplate"></bean>
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
@RestController
@RequestMapping("/consumer")
public class UserController {
//多个方法调用只需改一处就ok
public static final String URL_PREFIX = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/user/{id}")
public User getUser(@PathVariable("id")Long id){
//调用远程服务 http请求
String url = URL_PREFIX+"/provider/user/"+id;
return restTemplate.getForObject(url,User.class );
}
}
Eureka registry
why?
Because the problem of distributed services is bound to be faced:
- Service Management
- How automatic registration and discovery
- How to implement state regulation
- How to achieve dynamic routing
- How to achieve load balancing service
- How disaster recovery services to solve the problem
- How to achieve a unified service configuration
What is
Netflix Eureka is a sub-module is one of the core modules, Eureka is a REST-based services, for location services, the intermediate layer in order to achieve the cloud service discovery and failover. Service registration and discovery services for micro-architecture is very important, with service discovery and registration, just use the identifier of the service, you can access to the service without the need to modify the service
Eureka registration center set up
Create a new general maven project eureka_server_7001
POM guide package
<!--springboot支持-->
<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>
</dependency>
<!--Eureka服务端支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #是否要注册到eureka
fetchRegistry: false #表示是否从Eureka Server获取注册信息
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机配置
Run the Class
server:
port: 7001
eureka:
instance:
# hostname: localhost
hostname: eureka-7001.com #集群
client:
register-with-eureka: false
fetch-registry: false
service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机配置
defaultZone: http://eureka-7002.com:7002/eureka/ #集群配置
Test : Start and access localhost: 7001
The service provider registered to Eureka
In the service provider in the lead pack
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Modify application.yml
server:
port: 8001
spring:
application:
name: PRODUCT-SERVICE #服务名
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
Then enable startup class
@SpringBootApplication
@EnableEurekaClient //表示是eureka的客户端
public class UserProviderApplication_8001 {
public static void main(String[] args) {
SpringApplication.run(UserProviderApplication_8001.class);
}
}
The service consumer calls the service from Eureka
Guide package
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Modify application.yml
server:
port: 9001
spring:
application:
name: ORDER-SERVICE #服务名
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
Start classes with the @EnableEurekaClient
package cn.itsource.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEurekaClient
public class UserConsumerAppliction_9001 {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
public static void main(String[] args) {
SpringApplication.run(UserConsumerAppliction_9001.class);
}
}
Rectification service call
@Autowired
private DiscoveryClient discoveryClient;// Eureka客户端,可以获取到服务实例信息
// String baseUrl = "http://localhost:8081/user/";
// 根据服务名称,获取服务实例
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
// 因为只有一个UserService,因此我们直接get(0)获取
ServiceInstance instance = instances.get(0);
// 获取ip和端口信息
String baseUrl = "http://"+instance.getHost() + ":" + instance.getPort()+"/user/";
this.restTemplate.getForObject(baseUrl + id, User.class)
Then solve the problem url hardcoded
but only one EurekaServer, hung up to die
Eureka cluster registry
Cluster: multiple servers coordinate the completion of a service.
Eureka production environment to deploy multiple servers ok, but now is the development stage of the same host different port numbers instead of the server
How to do it
先拷贝一份 eureka_server_7002
在system32下的hosts文件里添加端口号:
127.0.0.1 eureka-7001.com
127.0.0.1 eureka-7002.com
Placed Eureka_server_7002 Application.Yml
server:
port: 7002
eureka:
instance:
# hostname: localhost
hostname: eureka-7002.com #集群
client:
register-with-eureka: false
fetch-registry: false
service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机配置
defaultZone: http://eureka-7001.com:7001/eureka/ #集群配置
Personal reformation Eureka_server_7001 Application.Yml
server:
port: 7001
eureka:
instance:
# hostname: localhost
hostname: eureka-7001.com #集群
client:
register-with-eureka: false
fetch-registry: false
service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机配置
defaultZone: http://eureka-7002.com:7002/eureka/ #集群配置
Then test: eureka-7001.com:7001 , eureka-7002.com:7002