catalogue
Eureka service registration and discovery
Eureka service registration and discovery
Eureka is divided into server and client: Generally speaking, the server is the registration center of all services, and the client is each service provider. This paper has taken stand-alone deployment as an example.
Server
The server needs to import Eureka server dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
Then mark @ EnableEurekaClient annotation in the main startup class
@SpringBootApplication @EnableEurekaClient public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class, args); } }
Eureka's cluster building means that multiple servers register and monitor each other (for example, a single machine only needs to set the defaultZone as its own)
server: port: 7001 #7001 port start eureka: instance: hostname: euraka7001.com #Instance name of eureka server client: register-with-eureka: false #false means that you do not register yourself with the registry. fetch-registry: false #false means that my client is the registration center. My responsibility is to maintain service instances and I don't need to retrieve services service-url: defaultZone: http://eureka7002.com:7002/eureka / # cluster with 7002
server: port: 7002 #7002 port start eureka: instance: hostname: euraka7002.com #Instance name of eureka server client: register-with-eureka: false #false means that you do not register yourself with the registry. fetch-registry: false #false means that my client is the registration center. My responsibility is to maintain service instances and I don't need to retrieve services service-url: defaultZone: http://eureka7001.com:7001/eureka / # cluster with 7001
(you need to modify the hosts file C:\Windows\System32\drivers\etc\hosts) add the domain name mapping in it.
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com
Then visit http://eureka7001.com:7001 or http://eureka7002.com:7002 You can see all registered services
Client (service provider)
The service provider needs to import Eureka client dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
Then mark @ EnableEurekaServer annotation in the main startup class
@SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class, args); } }
Then configure relevant contents (because Eureka server is a cluster environment, we register as all servers)
server: port: 8001 #8001 port start spring: application: name: cloud-payment-service #The application name is also an alias registered into the server eureka: client: register-with-eureka: true #Indicates whether to register yourself with EurekaServer. The default is true. fetchRegistry: true #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #Register into the cluster
If a single service also needs to be matched with a cluster, you only need to copy the same copy and modify the startup port
consumer
Use RestTemplate to complete the call.
Because the same service, such as the order module, may have multiple servers deployed, we can't call in the way of fixed ip address when calling remotely. We just need to replace the ip: port address with the registered alias of the service, and then mark the @ LoadBalanced annotation on the RestTemplate to enable the load balancing function (polling).
@Configuration public class ApplicationContextConfig { //Put RestTemplate into container @Bean @LoadBalanced//Enable the load balancing function of RestTemplate public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
The java code is as follows
@RestController @Slf4j public class OrderController { //public static final String PAYMENT_URL = "http://localhost:8001"; //Fixed service registration alias public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE"; @Autowired private RestTemplate restTemplate; @GetMapping("/consumer/payment/get/{id}") public CommonResult getPaymentById(@PathVariable Long id) { return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); } }
Our consumers must also register as providers. Just repeat the above provider configuration.
Start sequence
Server - > client