一:介紹【本文版權歸微信公衆號"代碼藝術"(ID:onblog)全部,如果轉載請務必保留本段原創聲明,違者必究。】html
1)導包web
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
2 )application.yml配置Eureka信息spring
server: port: 8761 eureka: instance: hostname: eureka-server #eureka實例的主機名 client: register-with-eureka: false #不把本身註冊到eureka fetch-registry: false #不從eureka上獲取服務的註冊信息 service-url: defaultZone: http://localhost:8761/eureka/
3)開啓@EnableEurekaServer註解微信
package cn.zyzpp.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * 開啓@EnableEurekaServer註解 */ @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
4)開啓註冊中心架構
1)導入依賴app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
2)application.yml配置Eureka信息負載均衡
server: port: 8001 #8002 spring: application: name: provider-ticket eureka: instance: prefer-ip-address: true #註冊服務的時候使用服務的Ip地址 client: service-url: defaultZone: http://localhost:8761/eureka/
3)Service層方法分佈式
package cn.zyzpp.providerticket.service; import org.springframework.stereotype.Service; /** * Create by yster@foxmail.com 2018/6/4/004 18:37 */ @Service public class TicketService { public String getTicket(){ return "《厲害了,個人國》"; } }
4)暴露HTTP接口ide
/** * Create by yster@foxmail.com 2018/6/4/004 18:39 */ @RestController public class TicketControllert { //輕量級HTTP @Autowired private TicketService ticketService; @GetMapping("/ticket") public String getTicket(){ System.out.println("8001"); //8002 return ticketService.getTicket(); } }
5)而後更改端口,分別打包該模塊爲jar包。運行。微服務
【本文版權歸微信公衆號"代碼藝術"(ID:onblog)全部,如果轉載請務必保留本段原創聲明,違者必究。】
1)application.yml配置Eureka信息
server: port: 8200 spring: application: name: consumer-user eureka: instance: prefer-ip-address: true #註冊服務的時候使用服務的Ip地址 client: service-url: defaultZone: http://localhost:8761/eureka/
2)開啓發現服務的功能,使用負載均衡機制(默認輪詢)
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 getRestTemplate(){ return new RestTemplate(); } }
3)獲取服務
/** * Create by yster@foxmail.com 2018/6/4/004 19:13 */ @RestController public class UserController { @Autowired private RestTemplate restTemplate; @GetMapping("/buy") public String getTicket(){ String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket",String.class); return "購買了 "+s; } }
【本文版權歸微信公衆號"代碼藝術"(ID:onblog)全部,如果轉載請務必保留本段原創聲明,違者必究】