Eureka registry
1. Recognizes Eureka
First we solve the first problem, the management services.
problem analysis
In the case just now, user-service to provide services, the need of external exposure to its own address. The consumer (the caller) need to record the address of the service provider. Future address changes occur, you need to update. This is less service time does not feel anything, but now increasingly complex Internet environment, a project will certainly split a dozen or even dozens of micro-services. At this point if it is man-management addresses not only difficult to develop future testing, release the line will be very troublesome.
Eureka What to do?
Eureka is responsible for management, records information service providers. Service the caller without having to find their own services, but to their own needs to tell Eureka, Eureka will then meet your service needs to tell you.
At the same time, between the service provider and the Eureka by “心跳”
mechanisms for monitoring, a service provider when problems arise, it will naturally Eureka removed from the service list.
This enables automatic registration services, discovery, status monitoring.
2. Schematic
Load balancing Ribbon
But the actual environment, we tend to turn a number of user-service clusters. At this point there is a list of services that we acquired will be more, in the end of the visit, which one do?
Under normal circumstances we would need to write such a load balancing algorithm to choose multiple instances list.
Eureka has helped us integrate load balancing components Ribbon, simply modify the code to use.
What is the Ribbon:
Coding
-
Service registry
Eureka's server applications, service registration and discovery (Eureka Server Cluster)
-
service providers
Applications and services, may be SpringBoot application, it can be any other technology, as long as the outside is provided Rest style service can be. (Multiple services to improve the same cluster)
-
Consumer Services
Consumer applications to obtain service from the registry list, so that each service side, know where to call the service side.
1. Preparation of EurekaServer
POM.XML:
<?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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>spring-cloud-demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
Start categories:
@SpringBootApplication
@EnableEurekaServer // 告知spring使用EurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class);
}
}
Configuration yml: the same machine can achieve more than if occupied several ports, a multi-start several services for clustering
Note: idea in one application can not be started twice. Check the box Allow parallel run in edit configuration.
When using a start we need, and then modify the port, start another
server:
port: 10000 # 端口
spring:
application:
name: eureka-server # 应用名称,会在Eureka中显示
eureka:
client:
service-url: # 配置其他Eureka服务的地址;
defaultZone: http://127.0.0.1:10001/eureka,http://127.0.0.1:10002/eureka
2. The user-service registration to Eureka
POM.XML
<?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">
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>spring-cloud-parent</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../spring-cloud-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
application.yml configuration. Only one machine, then, to open a few ports to open multiple provider server, you can implement a feature cluster (the same server name, application name below).
server:
port: 9000
spring:
# 应用名称
application:
name: user-service
eureka:
client:
service-url:
# EurekaServer地址:有几台注册几台
defaultZone: http://127.0.0.1:10000/eureka,http://127.0.0.1:10001/eureka,http://127.0.0.1:10002/eureka
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
Startup class
@SpringBootApplication
@EnableDiscoveryClient // spring自动查找可用的注册中心(Eureka、zookeeper。。。)
public class UserServiceApp {
public static void main(String[] args) {
SpringApplication.run(UserServiceApp.class);
}
}
Controller layer: services provided by Custom
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("user/{id}")
public User queryById(@PathVariable("id") Integer id) {
return userService.findById(id);
}
}
3. consumers to obtain services from Eureka
POM.XML
<?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">
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>spring-cloud-parent</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../spring-cloud-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//http客户端工具
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
</dependency>
//Eureka客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
Startup class
@SpringBootApplication
@EnableDiscoveryClient //开启注册中心客户端
public class CustomerMain {
public static void main(String[] args) {
SpringApplication.run(CustomerMain.class);
}
//也可以在一个Configure类配置该bean
@Bean
@LoadBalanced //开启负载均衡
public RestTemplate restTemplate() {
// 使用okHttp客户端
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}
application.xml configuration
server:
port: 8080
spring:
application:
# 应用名称
name: user-consumer
eureka:
client:
service-url:
# EurekaServer地址:有几台注册几台
defaultZone: http://127.0.0.1:10000/eureka,http://127.0.0.1:10001/eureka
instance:
prefer-ip-address: true # 当其它服务获取地址时提供ip而不是hostname
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
user-service:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Service layer way calling services: direct calls through the service name
@Service
public class UserCustomerService {
@Resource
private RestTemplate restTemplate;
//这里user-service是服务提供者的注册名称
//后面的/user/id 是调用服务提供方的provider调用数据的真正uri
//类似于 String url = "http://localhost:8081/user/" + id;
public User findById(Integer id) {
String url = "http://user-service/user/" + id;
return restTemplate.getForObject(url, User.class);
}
}
Configured, directly open to all Eureka server and service provider, using the client's path to url call.
Finally I said, here server, provider, consumer's pom.xml are inherited from a parent pom: through a maven module built from empty project, and then build. The empty project for only a succession of pom, nothing else.
<?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>spring-cloud-parent</groupId>
<artifactId>spring-cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../user-service</module>
<module>../user-consumer</module>
<module>../eureka-server</module>
</modules>
<packaging>pom</packaging>
<!-- 继承 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<properties>
</properties>
<dependencyManagement>
<dependencies>
<!-- 组合 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
To be continued. . . . . . . . . . . . . . . . . . To sleep