1, create a new blank project, create a new module, named eureka-server, select eureka server. Create a new Module, named provider-ticket, select the eureka discovery client. Create a new Module, named consumer-user, select the eureka discovery client.
2, set the service registry. Eureka-server disposed in the application.properties:
8761 = server.port # hostname eureka.instance.hostname = eureka- Server # does not register itself to the Eureka eureka.client.register-with-Eureka = false # is not up for registration information and services from Eureka eureka.client. = Registry-FETCH to false eureka.client.service -url.defaultZone = HTTP: // localhost: 8761 / Eureka /
At the same time plus @EnableEurekaServer comment on its boot entry. Start the server enter localhost: 8761
3, registration services
Require reintroduction spring-boot-starter-web initiator. The provider-ticket in the configuration application.properties:
8001 = server.port spring.application.name = provider- Ticket # registered to use the service when the service ip address eureka.instance.prefer-ip-address = to true eureka.client.service -url.defaultZone = HTTP: // localhost : 8761 / eureka /
Create a new controller package:
package com.gong.providerticket.controller; import com.gong.providerticket.service.TicketService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TicketController { @Autowired TicketService ticketService; @GetMapping("/ticket") public String getTicket(){ return ticketService.getTicket(); } }
Create a new service package:
package com.gong.providerticket.service; import org.springframework.stereotype.Service; @Service public class TicketService { public String getTicket(){return "唐人街探案3"; } }
Then use maven packaged. The package lay out on the copy in a folder, and add the suffix 8001. Modify port = 8002, then to package, copy the package lay out on a folder, and add the suffix 8002. Placed into a file folder, each running with java -jar.
Then refresh: http: // localhost: 8761 /
Two services are registered came.
4, found that the service and consumption
Require reintroduction spring-boot- starter-web initiator. The first is the consumer-user in application.properties
server.port=8200 spring.application.name=consumer-user #注册服务的时候使用服务的ip地址 eureka.instance.prefer-ip-address=true eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
然后是启动入口文件中:
package com.gong.consumeruser; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class ConsumerUserApplication { public static void main(String[] args) { SpringApplication.run(ConsumerUserApplication.class, args); } @LoadBalanced//使用负载均衡机制 @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
接着新建一个controller包:
package com.gong.consumeruser.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserController { @Autowired RestTemplate restTemplate; @GetMapping("/buy") public String buyTicket(String name){ return name+"购买了"+restTemplate.getForObject("http://PROVIDER-TICKET/ticket",String.class); } }
启动服务器:输入localhost:8200
说明是启动成功了,再看下http://localhost:8761/
也成功注册到注册中心了。
接着我们去访问:localhost:8200/buy?name=张三
成功的从provider-ticket那里获取到了信息。
由于使用了负载均衡,所以每次请求都会轮流访问8001端口和8002端口。